1 ##############################################################################
2 # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/lib/Perl/Critic/Policy/Variables/ProhibitConditionalDeclarations.pm $
3 # $Date: 2008-07-03 10:19:10 -0500 (Thu, 03 Jul 2008) $
6 ##############################################################################
8 package Perl::Critic::Policy::Variables::ProhibitConditionalDeclarations;
15 use Perl::Critic::Utils qw{ :severities :classification :data_conversion };
16 use base 'Perl::Critic::Policy';
18 our $VERSION = '1.088';
20 #-----------------------------------------------------------------------------
22 Readonly::Scalar my $DESC => q{Variable declared in conditional statement};
23 Readonly::Scalar my $EXPL => q{Declare variables outside of the condition};
25 #-----------------------------------------------------------------------------
27 sub supported_parameters { return () }
28 sub default_severity { return $SEVERITY_HIGHEST }
29 sub default_themes { return qw( core bugs ) }
30 sub applies_to { return 'PPI::Statement::Variable' }
32 #-----------------------------------------------------------------------------
35 my ( $self, $elem, undef ) = @_;
36 return if $elem->type() eq 'local';
38 if ( $elem->find(\&_is_conditional) ) {
39 return $self->violation( $DESC, $EXPL, $elem );
44 my @conditionals = qw( if while foreach for until unless );
45 my %conditionals = hashify( @conditionals );
48 my (undef, $elem) = @_;
50 return if !$conditionals{$elem};
51 return if ! $elem->isa('PPI::Token::Word');
52 return if is_hash_key($elem);
53 return if is_method_call($elem);
62 #-----------------------------------------------------------------------------
68 Perl::Critic::Policy::Variables::ProhibitConditionalDeclarations - Do not write C< my $foo = $bar if $baz; >.
72 This Policy is part of the core L<Perl::Critic> distribution.
77 Declaring a variable with a postfix conditional is really confusing.
78 If the conditional is false, its not clear if the variable will
79 be false, undefined, undeclared, or what. It's much more straightforward
80 to make variable declarations separately.
82 my $foo = $baz if $bar; #not ok
83 my $foo = $baz unless $bar; #not ok
84 our $foo = $baz for @list; #not ok
85 local $foo = $baz foreach @list; #not ok
90 This Policy is not configurable except for the standard options.
95 Jeffrey R. Thalhammer <thaljef@cpan.org>
99 Copyright (c) 2006-2008 Chris Dolan. All rights reserved.
101 This program is free software; you can redistribute it and/or modify
102 it under the same terms as Perl itself. The full text of this license
103 can be found in the LICENSE file included with this module.
109 # cperl-indent-level: 4
111 # indent-tabs-mode: nil
112 # c-indentation-style: bsd
114 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :