Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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 ...

In reply to Re^3: Can't understand function returning undefs by AnomalousMonk
in thread Can't understand function returning undefs by rg0now

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2024-04-19 19:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found