Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

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

by Thai Heng (Beadle)
on Nov 28, 2013 at 21:23 UTC ( #1064869=perlquestion: print w/replies, xml ) Need Help??
Thai Heng has asked for the wisdom of the Perl Monks concerning the following question:

I am a newbie to perl, and have no teacher. When reading the stonehenge article deep copy, there are some code as follows:
while (@x = getpwent()) { $info{$x[0]} = \@x; print "$info{$x[0]}\n"; }
I can't understand the function getpWent(). This function can be called many times and how to transfer input argument to it, then get different output argumeng or return array? Who can give a simple example for this function or this question?

Replies are listed 'Best First'.
Re: [stonehenge] deep copy what's the function getpwent()
by GrandFather (Sage) on Nov 28, 2013 at 21:37 UTC

    getpwent (note that it is all lower case - Perl considers case important) is a built in Perl function. See the link for details.

    True laziness is hard work
Re: [stonehenge] deep copy what's the function getpwent()
by hdb (Monsignor) on Nov 28, 2013 at 21:45 UTC

    Well, if you read the documentation, it says about getpwent:

    These routines are the same as their counterparts in the system C library.

    If this does not help, google it: getpwent, first find is and says:

    The first time getpwent() is called, it returns the first entry; thereafter, it returns successive entries.

    This little piece of code shows what happens:

    while(@x=getpwent()){ print "@x\n"; }
Re: [stonehenge] deep copy what's the function getpwent()
by Anonymous Monk on Nov 29, 2013 at 07:50 UTC
    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"; }
      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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1064869]
Approved by Corion
and a kettle whistles...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2017-11-25 10:20 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (355 votes). Check out past polls.