Modified source files and compiled any and armel versions of packages
[pkg-perl] / deb-src / libperl-critic-perl / libperl-critic-perl-1.088 / lib / Perl / Critic / Policy / Variables / ProhibitLocalVars.pm
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) $
4 #   $Author: clonezone $
5 # $Revision: 2489 $
6 ##############################################################################
7
8 package Perl::Critic::Policy::Variables::ProhibitLocalVars;
9
10 use 5.006001;
11 use strict;
12 use warnings;
13 use Readonly;
14
15 use Perl::Critic::Utils qw{ :severities :classification };
16 use base 'Perl::Critic::Policy';
17
18 our $VERSION = '1.088';
19
20 #-----------------------------------------------------------------------------
21
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 ];
25
26 #-----------------------------------------------------------------------------
27
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' }
32
33 #-----------------------------------------------------------------------------
34
35 sub violates {
36     my ( $self, $elem, undef ) = @_;
37     if ( $elem->type() eq 'local' && !_all_global_vars($elem) ) {
38         return $self->violation( $DESC, $EXPL, $elem );
39     }
40     return;    #ok!
41 }
42
43 #-----------------------------------------------------------------------------
44
45 sub _all_global_vars {
46
47     my $elem = shift;
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 );
53     }
54     return 1;
55 }
56
57 1;
58
59 __END__
60
61 #-----------------------------------------------------------------------------
62
63 =pod
64
65 =head1 NAME
66
67 Perl::Critic::Policy::Variables::ProhibitLocalVars - Use C<my> instead of C<local>, except when you have to.
68
69 =head1 AFFILIATION
70
71 This Policy is part of the core L<Perl::Critic> distribution.
72
73
74 =head1 DESCRIPTION
75
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.
81
82   local $foo;   #not ok
83   my $foo;      #ok
84
85   use English qw(-no_match_vars);
86   local $INPUT_RECORD_SEPARATOR    #ok
87   local $RS                        #ok
88   local $/;                        #not ok
89
90
91 =head1 CONFIGURATION
92
93 This Policy is not configurable except for the standard options.
94
95
96 =head1 NOTES
97
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.
104
105 =head1 SEE ALSO
106
107 L<Perl::Critic::Policy::Variables::ProhibitPunctuationVars>
108
109 =head1 AUTHOR
110
111 Jeffrey Ryan Thalhammer <thaljef@cpan.org>
112
113 =head1 COPYRIGHT
114
115 Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer.  All rights reserved.
116
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.
120
121 =cut
122
123 # Local Variables:
124 #   mode: cperl
125 #   cperl-indent-level: 4
126 #   fill-column: 78
127 #   indent-tabs-mode: nil
128 #   c-indentation-style: bsd
129 # End:
130 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :