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   26593 use 5.010001;
  40         206  
4 40     40   259 use strict;
  40         109  
  40         922  
5 40     40   232 use warnings;
  40         110  
  40         991  
6 40     40   216 use Readonly;
  40         103  
  40         2478  
7              
8 40     40   276 use Perl::Critic::Utils qw{ :severities :classification };
  40         112  
  40         1957  
9 40     40   13230 use parent 'Perl::Critic::Policy';
  40         123  
  40         269  
10              
11             our $VERSION = '1.148';
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 1706 sub supported_parameters { return () }
22 77     77 1 352 sub default_severity { return $SEVERITY_LOW }
23 86     86 1 411 sub default_themes { return qw(core pbp maintenance) }
24 32     32 1 102 sub applies_to { return 'PPI::Statement::Variable' }
25              
26             #-----------------------------------------------------------------------------
27              
28             sub violates {
29 102     102 1 290 my ( $self, $elem, undef ) = @_;
30 102 100 100     291 if ( $elem->type() eq 'local' && !_all_global_vars($elem) ) {
31 3         45 return $self->violation( $DESC, $EXPL, $elem );
32             }
33 99         3390 return; #ok!
34             }
35              
36             #-----------------------------------------------------------------------------
37              
38             sub _all_global_vars {
39              
40 15     15   576 my $elem = shift;
41 15         39 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       53 return if ! is_perl_global( $variable_name );
46             }
47 12         135 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 :