Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

[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 http://www.stonehenge.com/merlyn/UnixReview/col30.html, 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?

Comment on [stonehenge] deep copy what's the function getpwent()
Download Code
Re: [stonehenge] deep copy what's the function getpwent()
by GrandFather (Cardinal) 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 (Prior) 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 http://man7.org/linux/man-pages/man3/getpwent.3.html 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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1064869]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (10)
As of 2014-12-19 22:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (92 votes), past polls