http://www.perlmonks.org?node_id=861880


in reply to Re: Can't understand function returning undefs
in thread Can't understand function returning undefs

Thanks. Not that I like this behavior but I think I'll have to live with it.

But if we are at it. My problem is that I have a function that returns two values when data is available to it and when no data remained it returns undef.

sub recv_data{ if(read_some_data){ return($stuff1, $stuff2); } else { return; } }
What I would like to do is to call this function in a while until all data is consumed. I tried to do this first by
while(($x, $y) = recv_data){ ... }
which failed due to the above reasons (i.e., the while block always executed since ($x, $y) = recv_data always evaluates to true even if the function returns undef).

I still have the option to write

while(1){ my($x, $y) = recv_data; last unless defined $x; ... }
but I fail to see this as elegant.

How to organize this code so that I obtain the required behavior in an elegant and readable way?