The or-trick can't be used with multiple (list-) assignments. The code
my ($price, $floor) = get_price_floor() || 0; sub get_price_floor { return (10, 8); }
is parsed as
my ($price, $floor) = (get_price_floor() || 0);
which means that get_price_floor() is called in scalar context. Consequently, the return statement happens in scalar context which means that (10, 8) evaluates to 8 (the scalar comma operator). That is assigned to $price while $floor comes out undefined.

Since you have defined get_price_floor() yourself, it isn't quite clear why you want to prepare for the case it doesn't return anything -- you *know* what it returns. If you have to do that, one way would be the not particularly attractive

my ($price, $floor); (($price, $floor) = get_price_floor()) || (($price, $floor) = ( 0, 0));
Also note I have removed the ampersand from the calls to get_price_floor(). It isn't necessary and, in fact, has side effects you normally don't want.

Anno


In reply to Re: In What basis the subroutine return values assign to variables ? by Anno
in thread In What basis the subroutine return values assign to variables ? by gube

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.