1 ##############################################################################
2 # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/Variables/ProhibitLocalVars.pm $
3 # $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
6 ##############################################################################
8 package Perl::Critic::Policy::Variables::ProhibitLocalVars;
15 use Perl::Critic::Utils qw{ :severities :classification };
16 use base 'Perl::Critic::Policy';
18 our $VERSION = '1.088';
20 #-----------------------------------------------------------------------------
22 Readonly::Scalar my $PACKAGE_RX => qr/::/mx;
23 Readonly::Scalar my $DESC => q{Variable declared as "local"};
24 Readonly::Scalar my $EXPL => [ 77, 78, 79 ];
26 #-----------------------------------------------------------------------------
28 sub supported_parameters { return () }
29 sub default_severity { return $SEVERITY_LOW }
30 sub default_themes { return qw(core pbp maintenance) }
31 sub applies_to { return 'PPI::Statement::Variable' }
33 #-----------------------------------------------------------------------------
36 my ( $self, $elem, undef ) = @_;
37 if ( $elem->type() eq 'local' && !_all_global_vars($elem) ) {
38 return $self->violation( $DESC, $EXPL, $elem );
43 #-----------------------------------------------------------------------------
45 sub _all_global_vars {
48 for my $variable_name ( $elem->variables() ) {
49 next if $variable_name =~ $PACKAGE_RX;
50 # special exception for Test::More
51 next if $variable_name eq '$TODO'; ##no critic(Interpolat)
52 return if ! is_perl_global( $variable_name );
61 #-----------------------------------------------------------------------------
67 Perl::Critic::Policy::Variables::ProhibitLocalVars - Use C<my> instead of C<local>, except when you have to.
71 This Policy is part of the core L<Perl::Critic> distribution.
76 Since Perl 5, there are very few reasons to declare C<local>
77 variables. The most common exceptions are Perl's magical global
78 variables. If you do need to modify one of those global variables,
79 you should localize it first. You should also use the L<English>
80 module to give those variables more meaningful names.
85 use English qw(-no_match_vars);
86 local $INPUT_RECORD_SEPARATOR #ok
93 This Policy is not configurable except for the standard options.
98 If an external module uses package variables as its interface, then
99 using C<local> is actually a pretty sensible thing to do. So
100 Perl::Critic will not complain if you C<local>-ize variables with a
101 fully qualified name such as C<$Some::Package::foo>. However, if
102 you're in a position to dictate the module's interface, I strongly
103 suggest using accessor methods instead.
107 L<Perl::Critic::Policy::Variables::ProhibitPunctuationVars>
111 Jeffrey Ryan Thalhammer <thaljef@cpan.org>
115 Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer. All rights reserved.
117 This program is free software; you can redistribute it and/or modify
118 it under the same terms as Perl itself. The full text of this license
119 can be found in the LICENSE file included with this module.
125 # cperl-indent-level: 4
127 # indent-tabs-mode: nil
128 # c-indentation-style: bsd
130 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :