File Coverage

blib/lib/Devel/StrictMode.pm
Criterion Covered Total %
statement 15 15 100.0
branch n/a
condition 2 2 100.0
subroutine 5 5 100.0
pod n/a
total 22 22 100.0


line stmt bran cond sub pod time code
1 2     2   63317 use 5.006;
  2         7  
  2         87  
2 2     2   12 use strict;
  2         3  
  2         69  
3 2     2   10 use warnings;
  2         8  
  2         57  
4              
5 2     2   9 use Exporter ();
  2         4  
  2         308  
6              
7             package Devel::StrictMode;
8              
9             our $AUTHORITY = 'cpan:TOBYINK';
10             our $VERSION = '0.002';
11             our @ISA = qw( Exporter );
12             our @EXPORT = qw( STRICT );
13             our @EXPORT_OK = qw( LAX );
14              
15             BEGIN {
16 2     2   4 my $strict = 0;
17             $ENV{$_} && $strict++
18 2   100     30 for qw(
19             EXTENDED_TESTING
20             AUTHOR_TESTING
21             RELEASE_TESTING
22             PERL_STRICT
23             );
24            
25 2         207 eval "
26             sub STRICT () { !! $strict }
27             sub LAX () { ! $strict }
28             ";
29             };
30              
31             1;
32              
33             __END__
34              
35             =pod
36              
37             =encoding utf-8
38              
39             =for stopwords pragmata
40              
41             =head1 NAME
42              
43             Devel::StrictMode - determine whether strict (but slow) tests should be enabled
44              
45             =head1 SYNOPSIS
46              
47             package MyClass;
48            
49             use Moose;
50             use Devel::StrictMode;
51            
52             has input_data => (
53             is => 'ro',
54             isa => STRICT ? "HashRef[ArrayRef[Str]]" : "HashRef",
55             required => 1,
56             );
57              
58             =head1 DESCRIPTION
59              
60             This module provides you with a constant C<STRICT> which you can use to
61             determine whether additional strict (but slow) runtime tests are
62             executed by your code.
63              
64             C<STRICT> is true if any of the following environment variables have
65             been set to true:
66              
67             PERL_STRICT
68             EXTENDED_TESTING
69             AUTHOR_TESTING
70             RELEASE_TESTING
71              
72             C<STRICT> is false otherwise.
73              
74             It is anticipated that you might set one or more of the above variables
75             to true while running your test suite, but leave them all false in your
76             production scenario.
77              
78             Although not exported by default, a constant C<LAX> is also provided,
79             which returns the opposite of C<STRICT>.
80              
81             =head2 Using STRICT with Moose/Moo/Mouse attributes
82              
83             Type constraint checks (C<isa>) are conducted at run time. Slow checks
84             can slow down your constructor and accessors. As shown above, C<STRICT>
85             can be used to alternate between a slower by stricter type constraint
86             check, and a faster but looser one.
87              
88             Don't try this if your attribute coerces. It will subtly break things.
89              
90             =head2 Using STRICT to perform assertions in function and method calls
91              
92             You may protect blocks of assertions with an C<< if (STRICT) { ... } >>
93             conditional to ensure that they only run in your testing environment.
94              
95             sub fibonacci
96             {
97             my $n = $_[0];
98            
99             if (STRICT)
100             {
101             die "expected exactly one argument"
102             unless @_ == 1;
103             die "expected argument to be a natural number"
104             unless $n =~ /\A[0-9]+\z/;
105             }
106            
107             $n < 2 ? $n : fibonacci($n-1)+fibonacci($n-2);
108             }
109              
110             Because C<STRICT> is a constant, the Perl compiler will completely
111             optimize away the C<if> block when running in your production
112             environment.
113              
114             =head2 Using STRICT with pragmata
115              
116             Thanks to L<if> it's easy to use C<STRICT> to conditionally load
117             pragmata.
118              
119             use Devel::StrictMode;
120              
121             use strict;
122             use warnings STRICT ? qw(FATAL all) : qw(all);
123            
124             no if STRICT, "bareword::filehandles";
125             no if STRICT, "autovivification";
126              
127             See also L<autovivification>, L<bareword::filehandles>, L<indirect>,
128             L<multidimensional>, etc.
129              
130             =head1 BUGS
131              
132             Please report any bugs to
133             L<http://rt.cpan.org/Dist/Display.html?Queue=Devel-StrictMode>.
134              
135             =head1 SEE ALSO
136              
137             L<strictures>.
138              
139             =head1 AUTHOR
140              
141             Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
142              
143             =head1 COPYRIGHT AND LICENCE
144              
145             This software is copyright (c) 2014 by Toby Inkster.
146              
147             This is free software; you can redistribute it and/or modify it under
148             the same terms as the Perl 5 programming language system itself.
149              
150             =head1 DISCLAIMER OF WARRANTIES
151              
152             THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
153             WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
154             MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
155              
156             =begin trustme
157              
158             =item LAX
159              
160             =end trustme
161              
162             =cut