DEV Community

Perl 5.30 deprecates use of my() in a false conditional

Mitch Jackson on June 10, 2019

Perl 5.30.0 is released (changelog). With it comes a breaking change for me. It has been a long time since I've faced a breaking change with perl...
Collapse
 
maxatome profile image
Maxime Soulé

Other alternatives:

# same length, but if $bar is false, $foo equals $bar, so not necessarily undef:
my $foo = $bar && 'foobar';

# otherwise, less perlish and longer:
my $foo = $bar ? 'foobar' : undef;
Collapse
 
mjac profile image
Mitch Jackson

Thanks, also good alternatives.

Collapse
 
dams profile image
Damien Krotkine

To people wondering why this syntax persisted so long: the bug produced by this syntax was actually the only way to have state variables, before the keyword state was introduced. This is why the bug was not fixed. Some people actually used it as a short syntax to implement a state variable:

$ perl -E 'sub flip_flop { my $t if 0; $t = !$t; say $t ? "flip" : "flop"} flip_flop() for 1..4'
flip
flop
flip
flop
Collapse
 
jrw profile image
jrw

I was surprised when I first learned of this. I'm not sure why they didn't just fix the bug, so that

my $foo = "foobar" if $bar;

would be equivalent to

my $foo;
$foo = "foobar" if $bar;

Or, I'm not sure why they didn't fatalize the syntax, regardless of the value of $bar. Today $bar == 1 and your code works; tomorrow $bar == 0 and you get a fatal error?

I'm sure there's lots of code out there using the deprecated syntax.

Collapse
 
mjac profile image
Mitch Jackson

This syntax had a bug. The variable did not get garbage collected when it fell out of scope. Perhaps the decision to fatalize it was a practical decision to fix that bug.

Collapse
 
jrw profile image
jrw

Probably it was too hard to just fix it, so fatalizing it was the next best thing? But it seems very risky/buggy to fatalize only in the cases where the conditional expression is false, since that expression is not a constant.

Collapse
 
ribugent profile image
Gerard Ribugent Navarro

I saw this pattern on old code at the company I work. Fortunately, there is a perl critic policy to detect this mistakes.

metacpan.org/pod/Perl::Critic::Pol...

Collapse
 
mjac profile image
Mitch Jackson

Thanks for this! I will start using this policy.

Collapse
 
thorstenhirsch profile image
Thorsten Hirsch • Edited

Uhm... no. I can't believe a lot of people used this pattern - why would you want a conditional declaration? You can't even call 'if($foo)' when using strict. I've always written code like '# Or this'.

Collapse
 
mjac profile image
Mitch Jackson

I'm not convinced a lot of people use this pattern. I picked it up so long ago, I can't tell you where I learned it. I've preferred it for it's brevity.

Collapse
 
davorg profile image
Dave Cross • Edited

This syntax has been generating deprecation warnings since Perl 5.10 in 2007 (see metacpan.org/pod/release/XSAWYERX/...). And it's been telling you that it would become fatal in 5.30 since the release of 5.26 in 2017.

Surely that's enough time to fix this "bug" in your code? :-)

And, to be clear, this is not deprecation. This is fatalising a deprecation warning.

Collapse
 
mjac profile image
Mitch Jackson

Dave, I've never seen a deprecation warning. If I had, of course this fatalising would not have been surprising. Probably because I was not using the syntax to hack a persistent state variable.

I found this puzzling, until reading comment history on RT#133543. It's mentioned the deprecation warning may not be displayed for some instances of the syntax.

Collapse
 
billruppert profile image
Bill Ruppert

I am quite surprised. A quick scan found an instance of this syntax in my production code.

Collapse
 
kcaran profile image
Keith Carangelo

After reading your post I thought, "maybe I should check...", and sure enough I found multiple instances. Thanks!