Many of us know we can do this:
In my case, however, I want to have a fall-back. So I thought I'd just do:if (my @f = some_code()) { # use @f here } # but not here
However, that gives me Global symbol "@f" requires explicit package name at ./t.pl line 15. ... ok, fine. So just insert a 'my' in there...if ((my @f = some_code()) == 1 || (@f = some_other_code()) == 1) { # I only expect one item back. Use $f[0] here. }
Except that I now get "my" variable @f masks earlier declaration in same statement at ./t.pl line 15. - that confuses me. Either the variable doesn't exist, or I'm masking it. Perhaps I need a better error message ("plonk!"), but that doesn't seem like it makes sense as is.if ((my @f = some_code()) == 1 || (my @f = some_other_code()) == 1)
I realise there are alternatives. Many of them. Including wrapping it in a small block to keep my variable localised:
but the question is - can I do this all in a single expression?{ my @f; if ((@f = some_code()) == 1 || (@f = some_other_code()) == 1) { #... } }
In reply to creating a variable in an if statement by Tanktalus
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |