in reply to Re^2: Can't understand function returning undefs
in thread Can't understand function returning undefs
sub recv_data{ if(read_some_data){ return($stuff1, $stuff2); } else { return; } }
The function in your example returns an empty list, not undef, in list context when there is no more data available.
If your real function actually returns undef in list context (which is 'supplied' to the function by the ($x, $y) sub-expression in your example code), then the list will always have at least a single item (i.e., undef) and will always evaluate as true; only the empty list or array is false. See the code below for the difference between
return;
and
return undef;
>perl -le "use warnings FATAL => 'all'; use strict; ; my @pairs1 = qw(1 2 3 4); ; sub S1 { return if not @pairs1; return pop @pairs1, pop @pairs1; } ; my ($x, $y); while (($x, $y) = S1()) { print qq{$x $y}; } ; my @pairs2 = qw(5 6 7 8); ; sub S2 { return undef if not @pairs2; return pop @pairs2, pop @pairs2; } ; while (($x, $y) = S2()) { print qq{$x $y}; } " 4 3 2 1 8 7 6 5 Use of uninitialized value $x in concatenation (.) or string at ...
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^4: Can't understand function returning undefs
by rg0now (Chaplain) on Sep 24, 2010 at 21:32 UTC |