Many of us know we can do this:

if (my @f = some_code()) { # use @f here } # but not here
In my case, however, I want to have a fall-back. So I thought I'd just do:
if ((my @f = some_code()) == 1 || (@f = some_other_code()) == 1) { # I only expect one item back. Use $f[0] 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 || (my @f = some_other_code()) == 1)
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.

I realise there are alternatives. Many of them. Including wrapping it in a small block to keep my variable localised:

{ my @f; if ((@f = some_code()) == 1 || (@f = some_other_code()) == 1) { #... } }
but the question is - can I do this all in a single expression?


In reply to creating a variable in an if statement by Tanktalus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.