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

Originally published at dev.to
Perl 5.30 deprecates use of my() in a false conditional
Assigning a variable in a false conditional has been deprecated. I’ll have to update some old code. Perhaps you will too.

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.

Deprecated use of my() in false conditional.

I have been using this deprecated syntax liberally like so:

# This syntax generates a fatal error in perl 5.30
#
# Initialize $foo
#   If $bar tests true, $foo = 'foobar'
#   If $bar tests false, $foo = undef
my $foo = 'foobar' if $bar;
There has been a long-standing bug in Perl that causes a lexical variable not to be cleared at scope exit when its declaration includes a false conditional.

This could make for interesting bugs.  The scoped variable declared this way may unexpectedly persist data!

# Use this instead
my $foo;
if ( $bar ) {
  $foo = 'foobar';
}

# Or this
my $foo;
$foo = 'foobar' if $bar;

According to RT# 133543, the deprecation will not always be triggered with this syntax!  Therefore, I feel I can't trust a run of a test suite under 5.30 to identify all the offending code statements.

Is anybody else surprised to learn this declaration pattern is problematic?