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) $
6 ##############################################################################
8 package Perl::Critic::Policy::Variables::RequireInitializationForLocalVars;
15 use Perl::Critic::Utils qw{ :severities };
16 use base 'Perl::Critic::Policy';
18 our $VERSION = '1.088';
20 #-----------------------------------------------------------------------------
22 Readonly::Scalar my $DESC => q{"local" variable not initialized};
23 Readonly::Scalar my $EXPL => [ 78 ];
25 #-----------------------------------------------------------------------------
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' }
32 #-----------------------------------------------------------------------------
35 my ( $self, $elem, undef ) = @_;
36 if ( $elem->type() eq 'local' && !_is_initialized($elem) ) {
37 return $self->violation( $DESC, $EXPL, $elem );
42 #-----------------------------------------------------------------------------
46 my $wanted = sub { $_[1]->isa('PPI::Token::Operator') && $_[1] eq q{=} };
47 return $elem->find( $wanted ) ? 1 : 0;
54 #-----------------------------------------------------------------------------
60 Perl::Critic::Policy::Variables::RequireInitializationForLocalVars - Write C<local $foo = $bar;> instead of just C<local $foo;>.
64 This Policy is part of the core L<Perl::Critic> distribution.
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
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'
92 This Policy is not configurable except for the standard options.
97 Jeffrey Ryan Thalhammer <thaljef@cpan.org>
101 Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer. All rights reserved.
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.
111 # cperl-indent-level: 4
113 # indent-tabs-mode: nil
114 # c-indentation-style: bsd
116 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :