File Coverage

blib/lib/Perl/Critic/Policy/Variables/ProhibitLocalVars.pm
Criterion Covered Total %
statement 31 31 100.0
branch 7 8 87.5
condition 3 3 100.0
subroutine 12 12 100.0
pod 4 5 80.0
total 57 59 96.6


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::Variables::ProhibitLocalVars;
2              
3 40     40   27315 use 5.010001;
  40         177  
4 40     40   286 use strict;
  40         137  
  40         857  
5 40     40   255 use warnings;
  40         122  
  40         941  
6 40     40   218 use Readonly;
  40         117  
  40         2076  
7              
8 40     40   302 use Perl::Critic::Utils qw{ :severities :classification };
  40         115  
  40         2032  
9 40     40   13073 use parent 'Perl::Critic::Policy';
  40         131  
  40         251  
10              
11             our $VERSION = '1.146';
12              
13             #-----------------------------------------------------------------------------
14              
15             Readonly::Scalar my $PACKAGE_RX => qr/::/xms;
16             Readonly::Scalar my $DESC => q{Variable declared as "local"};
17             Readonly::Scalar my $EXPL => [ 77, 78, 79 ];
18              
19             #-----------------------------------------------------------------------------
20              
21 91     91 0 1595 sub supported_parameters { return () }
22 77     77 1 336 sub default_severity { return $SEVERITY_LOW }
23 86     86 1 459 sub default_themes { return qw(core pbp maintenance) }
24 32     32 1 111 sub applies_to { return 'PPI::Statement::Variable' }
25              
26             #-----------------------------------------------------------------------------
27              
28             sub violates {
29 102     102 1 305 my ( $self, $elem, undef ) = @_;
30 102 100 100     260 if ( $elem->type() eq 'local' && !_all_global_vars($elem) ) {
31 3         49 return $self->violation( $DESC, $EXPL, $elem );
32             }
33 99         3229 return; #ok!
34             }
35              
36             #-----------------------------------------------------------------------------
37              
38             sub _all_global_vars {
39              
40 15     15   597 my $elem = shift;
41 15         40 for my $variable_name ( $elem->variables() ) {
42 18 100       2069 next if $variable_name =~ $PACKAGE_RX;
43             # special exception for Test::More
44 16 50       45 next if $variable_name eq '$TODO'; ## no critic (InterpolationOfMetachars)
45 16 100       46 return if ! is_perl_global( $variable_name );
46             }
47 12         131 return 1;
48             }
49              
50             1;
51              
52             __END__
53              
54             #-----------------------------------------------------------------------------
55              
56             =pod
57              
58             =head1 NAME
59              
60             Perl::Critic::Policy::Variables::ProhibitLocalVars - Use C<my> instead of C<local>, except when you have to.
61              
62              
63             =head1 AFFILIATION
64              
65             This Policy is part of the core L<Perl::Critic|Perl::Critic>
66             distribution.
67              
68              
69             =head1 DESCRIPTION
70              
71             Since Perl 5, there are very few reasons to declare C<local>
72             variables. The most common exceptions are Perl's magical global
73             variables. If you do need to modify one of those global variables,
74             you should localize it first. You should also use the
75             L<English|English> module to give those variables more meaningful
76             names.
77              
78             local $foo; #not ok
79             my $foo; #ok
80              
81             use English qw(-no_match_vars);
82             local $INPUT_RECORD_SEPARATOR #ok
83             local $RS #ok
84             local $/; #not ok
85              
86              
87             =head1 CONFIGURATION
88              
89             This Policy is not configurable except for the standard options.
90              
91              
92             =head1 NOTES
93              
94             If an external module uses package variables as its interface, then
95             using C<local> is actually a pretty sensible thing to do. So
96             Perl::Critic will not complain if you C<local>-ize variables with a
97             fully qualified name such as C<$Some::Package::foo>. However, if
98             you're in a position to dictate the module's interface, I strongly
99             suggest using accessor methods instead.
100              
101             =head1 SEE ALSO
102              
103             L<Perl::Critic::Policy::Variables::ProhibitPunctuationVars|Perl::Critic::Policy::Variables::ProhibitPunctuationVars>
104              
105             =head1 AUTHOR
106              
107             Jeffrey Ryan Thalhammer <jeff@imaginative-software.com>
108              
109             =head1 COPYRIGHT
110              
111             Copyright (c) 2005-2011 Imaginative Software Systems. All rights reserved.
112              
113             This program is free software; you can redistribute it and/or modify
114             it under the same terms as Perl itself. The full text of this license
115             can be found in the LICENSE file included with this module.
116              
117             =cut
118              
119             # Local Variables:
120             # mode: cperl
121             # cperl-indent-level: 4
122             # fill-column: 78
123             # indent-tabs-mode: nil
124             # c-indentation-style: bsd
125             # End:
126             # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :