Perl::Critic::Policy::Community::ConditionalDeclarations3pm
Don't declare variables conditionally
Overview
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 okmy ($foo, $bar) = @_ unless $baz; # not okour $bar = $_ for 0..10; # not okmy $foo; $foo = $bar if $baz; # okmy $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.
Affiliation
This policy is part of Perl::Critic::Community.
Configuration
This policy is not configurable except for the standard options.
Copyright and license
Copyright 2015, Dan Book.
This library is free software; you may redistribute it and/or modify it under the terms of the Artistic License version 2.0.
Releases
2 releases since Debian 12 (Bookworm); the text changed 1 time.