princepawn has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's minimum standard of quality and will not be displayed.

Replies are listed 'Best First'.
(tye)Re: Empty list as a return value
by tye (Sage) on Oct 23, 2000 at 19:42 UTC

    There is no way to return an "undefined list" as distinct from an "empty list". Please go read the replies to your last posting. You can only return an empty list. For the following code:

    sub empty { return; } @x= empty(); print defined(@x) ? "defined" : "not", "\n";
    what is printed depends entirely on whether @x was "defined" before any of this happened. Stop using defined() on arrays.

            - tye (but my friends call me "Tye")
Re: Empty list as a return value
by merlyn (Sage) on Oct 23, 2000 at 19:47 UTC
    Not only what tye said, but your code undef my @x certainly smacks of voodoo programming. Leave the undef off, and you get exactly the same result, in a much more idiomatic way.

    -- Randal L. Schwartz, Perl hacker

Re: Empty list as a return value
by mitd (Curate) on Oct 23, 2000 at 23:40 UTC

    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.'

    A reply falls below the community's threshold of quality. You may see it by logging in.
(c-era)Re: Empty list as a return value
by c-era (Curate) on Oct 23, 2000 at 19:46 UTC
    Undefined and an empty list both have a length of -1, so for what you need to use it for <bold>THEY ARE THE SAME THING</bold>. If you want you can return (""), but then you will have a single element array that contains nothing.
      Well, they both have a length of 0. Perhaps you are confusing the length with what $#ARRAYTHINGY returns, which is one less than the length, but therefore suitable for use with upper index bounds.

      -- Randal L. Schwartz, Perl hacker

A reply falls below the community's threshold of quality. You may see it by logging in.