I've had it come it in code reviews. In order to effect the minimum amount of changes to prevent the problem, I use the following change:
my $p = $q if condition(); # becomes my $p = condition() ? $q : undef;
This avoids introducing a second statement, such as instantiating and then assigning:
my $p; $p = $q if condition();For the risk in this latter piece of code is that someone might "helpfully" refactor it back again. The ternary operator forces people to think about both sides of the condition.
• another intruder with the mooring in the heart of the Perl
In reply to Re: "my $p = value if condition()" as bad|deprecated as "my $p if 0" (Naughty indeed)
by grinder
in thread "my $p = value if condition()" as bad|deprecated as "my $p if 0" (Naughty indeed)
by parv
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |