Risk-Aware AI Code Review for Business-Critical Systems
LiveReview LiveReview Explore LiveReview

Perl::Critic::Policy::Community::ConditionalDeclarations - Don't declare variables conditionally

Affiliation

       This policy is part of Perl::Critic::Community.

Author

       Dan Book, "dbook@cpan.org"

Configuration

       This policy is not configurable except for the standard options.

Description

       It is possible to add a postfix condition to a variable declaration, like "my $foo = $bar if $baz".
       However, it is unclear (and undefined) if the variable will be declared when the condition is not met.
       Instead, declare the variable and then assign to it conditionally, or use the ternary operator to assign
       a value conditionally.

         my $foo = $bar if $baz;           # not ok
         my ($foo, $bar) = @_ unless $baz; # not ok
         our $bar = $_ for 0..10;          # not ok
         my $foo; $foo = $bar if $baz;     # ok
         my $foo = $baz ? $bar : undef;    # ok

       This policy is a subclass of the core policy
       Perl::Critic::Policy::Variables::ProhibitConditionalDeclarations, and performs the same function but in
       the "community" theme.

Name

       Perl::Critic::Policy::Community::ConditionalDeclarations - Don't declare variables conditionally

See Also

       Perl::Critic

perl v5.40.1                                       2025-03-22             Perl::Critic::P...nalDeclarations(3pm)

See Also