in reply to Empty list as a return value
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.'
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
| A reply falls below the community's threshold of quality. You may see it by logging in. |