I get real nervous wading into these more subtle issues, so flame if you must but I am just trying to understand.

My question is, based on the code below, is the problem in how you test for an empty list ??. Because if I am reading the output from below correctly an undefined @x and a @x = () will both return 0 when @x is tested for length.

I do find it curious that a an undefined array returns 0 when asked scalar @x.

So in conclusion in order to test if @x or an empty list one must test for both definedness and length ??

Of course I may have just wasted your time and mine beating a dead horse if I have I will meditate on perldoc:perldata for an extra hour tonight.

# x not defined print "X not defiined:\t ",scalar @x,"\n" if (!defined(@x)); # given an empty list @x = x(); print "X empty list:\t ",scalar @x,"\n"; @x = (1,2,3); # x given a populated list @x = x(); print "X populated:\t ",scalar @x,"\n"; sub x () { return @x if defined(@x) and warn "\@x was already defined"; return @x if (scalar @x==0) and warn "\@x is 0"; return wantarray ? () : undef; } Output: X not defiined: 0 @x is 0 at wa.pl line 14. X empty list: 0 @x was already defined at wa.pl line 13. X populated: 3

MitD -- Made in the Dark
'My favourite colour appears to be grey.'


In reply to Re: Empty list as a return value by mitd
in thread Empty list as a return value by princepawn

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.