Variables are only available (by name) in the statement following the one in which they are declared. It allows the following to work.
my $var; ... { my $var = $var; ... }
So the key is to not refer to the new var by name or to put multiple statements in the if expression.
The only think that comes to mind is
if ( sub { our @f; local *f = $_[0]; (@f = some_code()) == 1 || (@f = some_other_code()) == 1 }->(\my @f) ) { #... }
Yuck.
This isn't a general purpose solution, but it works here:
if (my @f = do { my @f; ((@f = some_code()) == 1 || (@f = some_other_code()) == 1) ? @f : ( +) }) { #... }
Yuck.
Update: Added a missing pair of parens to the last snippet.
In reply to Re: creating a variable in an if statement
by ikegami
in thread creating a variable in an if statement
by Tanktalus
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |