Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^2: Return a list or an iterator

by Anonymous Monk
on Apr 17, 2006 at 18:56 UTC ( [id://543890]=note: print w/replies, xml ) Need Help??


in reply to Re: Return a list or an iterator
in thread Return a list or an iterator

The safe way to handle undefs as a valid element is to have the iterator return one element in list context and an empty list if there's no elements, and then always call it in list context when iterating over it.
my $iter = sub { @foo ? shift @foo : () }; while (my ($elem) = $iter->()) { # $elem may be undefined. }
This way you don't need it to be an object and you don't need any special end-of-list elements.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://543890]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (None)
    As of 2024-04-25 00:47 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found