| 1 |
NAME |
| 2 |
Devel::StrictMode - determine whether strict (but slow) tests should be |
| 3 |
enabled |
| 4 |
|
| 5 |
SYNOPSIS |
| 6 |
package MyClass; |
| 7 |
|
| 8 |
use Moose; |
| 9 |
use Devel::StrictMode; |
| 10 |
|
| 11 |
has input_data => ( |
| 12 |
is => 'ro', |
| 13 |
isa => STRICT ? "HashRef[ArrayRef[Str]]" : "HashRef", |
| 14 |
required => 1, |
| 15 |
); |
| 16 |
|
| 17 |
DESCRIPTION |
| 18 |
This module provides you with a constant `STRICT` which you can use to |
| 19 |
determine whether additional strict (but slow) runtime tests are executed |
| 20 |
by your code. |
| 21 |
|
| 22 |
`STRICT` is true if any of the following environment variables have been |
| 23 |
set to true: |
| 24 |
|
| 25 |
PERL_STRICT |
| 26 |
EXTENDED_TESTING |
| 27 |
AUTHOR_TESTING |
| 28 |
RELEASE_TESTING |
| 29 |
|
| 30 |
`STRICT` is false otherwise. |
| 31 |
|
| 32 |
It is anticipated that you might set one or more of the above variables to |
| 33 |
true while running your test suite, but leave them all false in your |
| 34 |
production scenario. |
| 35 |
|
| 36 |
Although not exported by default, a constant `LAX` is also provided, which |
| 37 |
returns the opposite of `STRICT`. |
| 38 |
|
| 39 |
Using STRICT with Moose/Moo/Mouse attributes |
| 40 |
Type constraint checks (`isa`) are conducted at run time. Slow checks can |
| 41 |
slow down your constructor and accessors. As shown above, `STRICT` can be |
| 42 |
used to alternate between a slower by stricter type constraint check, and |
| 43 |
a faster but looser one. |
| 44 |
|
| 45 |
Don't try this if your attribute coerces. It will subtly break things. |
| 46 |
|
| 47 |
Using STRICT to perform assertions in function and method calls |
| 48 |
You may protect blocks of assertions with an `if (STRICT) { ... }` |
| 49 |
conditional to ensure that they only run in your testing environment. |
| 50 |
|
| 51 |
sub fibonacci |
| 52 |
{ |
| 53 |
my $n = $_[0]; |
| 54 |
|
| 55 |
if (STRICT) |
| 56 |
{ |
| 57 |
die "expected exactly one argument" |
| 58 |
unless @_ == 1; |
| 59 |
die "expected argument to be a natural number" |
| 60 |
unless $n =~ /\A[0-9]+\z/; |
| 61 |
} |
| 62 |
|
| 63 |
$n < 2 ? $n : fibonacci($n-1)+fibonacci($n-2); |
| 64 |
} |
| 65 |
|
| 66 |
Because `STRICT` is a constant, the Perl compiler will completely optimize |
| 67 |
away the `if` block when running in your production environment. |
| 68 |
|
| 69 |
Using STRICT with pragmata |
| 70 |
Thanks to if it's easy to use `STRICT` to conditionally load pragmata. |
| 71 |
|
| 72 |
use Devel::StrictMode; |
| 73 |
|
| 74 |
use strict; |
| 75 |
use warnings STRICT ? qw(FATAL all) : qw(all); |
| 76 |
|
| 77 |
no if STRICT, "bareword::filehandles"; |
| 78 |
no if STRICT, "autovivification"; |
| 79 |
|
| 80 |
See also autovivification, bareword::filehandles, indirect, |
| 81 |
multidimensional, etc. |
| 82 |
|
| 83 |
BUGS |
| 84 |
Please report any bugs to |
| 85 |
<http://rt.cpan.org/Dist/Display.html?Queue=Devel-StrictMode>. |
| 86 |
|
| 87 |
SEE ALSO |
| 88 |
strictures. |
| 89 |
|
| 90 |
AUTHOR |
| 91 |
Toby Inkster <[email protected]>. |
| 92 |
|
| 93 |
COPYRIGHT AND LICENCE |
| 94 |
This software is copyright (c) 2014 by Toby Inkster. |
| 95 |
|
| 96 |
This is free software; you can redistribute it and/or modify it under the |
| 97 |
same terms as the Perl 5 programming language system itself. |
| 98 |
|
| 99 |
DISCLAIMER OF WARRANTIES |
| 100 |
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED |
| 101 |
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF |
| 102 |
MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 103 |
|