File Coverage

blib/lib/Perl/Critic/Policy/Variables/ProhibitLocalVars.pm
Criterion Covered Total %
statement 24 31 77.4
branch 1 8 12.5
condition 1 3 33.3
subroutine 11 12 91.6
pod 4 5 80.0
total 41 59 69.4


line stmt bran cond sub pod time code
1             package Perl::Critic::Policy::Variables::ProhibitLocalVars;
2              
3 40     40   26900 use 5.010001;
  40         179  
4 40     40   251 use strict;
  40         132  
  40         886  
5 40     40   231 use warnings;
  40         121  
  40         936  
6 40     40   223 use Readonly;
  40         132  
  40         1969  
7              
8 40     40   285 use Perl::Critic::Utils qw{ :severities :classification };
  40         115  
  40         1949  
9 40     40   12934 use parent 'Perl::Critic::Policy';
  40         193  
  40         375  
10              
11             our $VERSION = '1.150';
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 89     89 0 1972 sub supported_parameters { return () }
22 74     74 1 333 sub default_severity { return $SEVERITY_LOW }
23 86     86 1 380 sub default_themes { return qw(core pbp maintenance) }
24 30     30 1 97 sub applies_to { return 'PPI::Statement::Variable' }
25              
26             #-----------------------------------------------------------------------------
27              
28             sub violates {
29 85     85 1 178 my ( $self, $elem, undef ) = @_;
30 85 50 33     187 if ( $elem->type() eq 'local' && !_all_global_vars($elem) ) {
31 0         0 return $self->violation( $DESC, $EXPL, $elem );
32             }
33 85         2424 return; #ok!
34             }
35              
36             #-----------------------------------------------------------------------------
37              
38             sub _all_global_vars {
39              
40 0     0     my $elem = shift;
41 0           for my $variable_name ( $elem->variables() ) {
42 0 0         next if $variable_name =~ $PACKAGE_RX;
43             # special exception for Test::More
44 0 0         next if $variable_name eq '$TODO'; ## no critic (InterpolationOfMetachars)
45 0 0         return if ! is_perl_global( $variable_name );
46             }
47 0           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 :