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 / RequireInitializationForLocalVars.pm
1 ##############################################################################
2 #      $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/Variables/RequireInitializationForLocalVars.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::RequireInitializationForLocalVars;
9
10 use 5.006001;
11 use strict;
12 use warnings;
13 use Readonly;
14
15 use Perl::Critic::Utils qw{ :severities };
16 use base 'Perl::Critic::Policy';
17
18 our $VERSION = '1.088';
19
20 #-----------------------------------------------------------------------------
21
22 Readonly::Scalar my $DESC => q{"local" variable not initialized};
23 Readonly::Scalar my $EXPL => [ 78 ];
24
25 #-----------------------------------------------------------------------------
26
27 sub supported_parameters { return ()                         }
28 sub default_severity     { return $SEVERITY_MEDIUM           }
29 sub default_themes       { return qw(core pbp bugs)          }
30 sub applies_to           { return 'PPI::Statement::Variable' }
31
32 #-----------------------------------------------------------------------------
33
34 sub violates {
35     my ( $self, $elem, undef ) = @_;
36     if ( $elem->type() eq 'local' && !_is_initialized($elem) ) {
37         return $self->violation( $DESC, $EXPL, $elem );
38     }
39     return;    #ok!
40 }
41
42 #-----------------------------------------------------------------------------
43
44 sub _is_initialized {
45     my $elem = shift;
46     my $wanted = sub { $_[1]->isa('PPI::Token::Operator') && $_[1] eq q{=} };
47     return $elem->find( $wanted ) ? 1 : 0;
48 }
49
50 1;
51
52 __END__
53
54 #-----------------------------------------------------------------------------
55
56 =pod
57
58 =head1 NAME
59
60 Perl::Critic::Policy::Variables::RequireInitializationForLocalVars - Write C<local $foo = $bar;> instead of just C<local $foo;>.
61
62 =head1 AFFILIATION
63
64 This Policy is part of the core L<Perl::Critic> distribution.
65
66
67 =head1 DESCRIPTION
68
69 Most people don't realize that a localized copy of a variable does not
70 retain its original value.  Unless you initialize the variable when
71 you C<local>-ize it, it defaults to C<undef>.  If you want the
72 variable to retain its original value, just initialize it to itself.
73 If you really do want the localized copy to be undef, then make it
74 explicit.
75
76   package Foo;
77   $Bar = '42';
78
79   package Baz;
80
81   sub frobulate {
82
83       local $Foo::Bar;              #not ok, local $Foo::Bar is 'undef'
84       local $Foo::Bar = undef;      #ok, local $Foo::Bar is obviously 'undef'
85       local $Foo::Bar = $Foo::Bar;  #ok, local $Foo::Bar still equals '42'
86
87   }
88
89
90 =head1 CONFIGURATION
91
92 This Policy is not configurable except for the standard options.
93
94
95 =head1 AUTHOR
96
97 Jeffrey Ryan Thalhammer <thaljef@cpan.org>
98
99 =head1 COPYRIGHT
100
101 Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer.  All rights reserved.
102
103 This program is free software; you can redistribute it and/or modify
104 it under the same terms as Perl itself.  The full text of this license
105 can be found in the LICENSE file included with this module.
106
107 =cut
108
109 # Local Variables:
110 #   mode: cperl
111 #   cperl-indent-level: 4
112 #   fill-column: 78
113 #   indent-tabs-mode: nil
114 #   c-indentation-style: bsd
115 # End:
116 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :