Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: [stonehenge] deep copy what's the function getpwent()

by Anonymous Monk
on Nov 29, 2013 at 07:50 UTC ( [id://1064907]=note: print w/replies, xml ) Need Help??


in reply to [stonehenge] deep copy what's the function getpwent()

They call those things iterators. An iterator is a function that returns items from a list until it runs out of them. When the list is exhausted, it returns a false value.

In Perl, they're usually written using a closure:

sub make_iterator { my (@list) = @_; return sub { # return a false value if exhausted return unless @list; # return next item in list return shift @list; }; } my $iter = make_iterator(1..5); while ( my $val = $iter->() ) { print $val, "\n"; }

Replies are listed 'Best First'.
Re^2: [stonehenge] deep copy what's the function getpwent()
by Anonymous Monk on Nov 29, 2013 at 07:53 UTC
    Oh, and each is an iterator too.
    while (($key, $value) = each %hash) { print $key, "\n"; }
                   After "each" has returned all entries from the hash or array,
                   the next call to "each" returns the empty list in list context
                   and "undef" in scalar context.  The next call following that
                   one restarts iteration.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-18 20:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found