ronix has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, What would be the most concise way to load all existing uids on a Unix system into an array using the getpwent function? My problem more precisely is: I can't seem to figure out the syntax to load an array with an iterating function like getpwent? thanks!

Replies are listed 'Best First'.
Re: simple getpwent/array question
by Limbic~Region (Chancellor) on Nov 06, 2008 at 21:09 UTC
    ronix,
    I am surprised the documentation on getpwent is not more clear. I am pretty sure it acts as an iterator but I can't test at the moment.
    while (my @entry = getpwent) { # ... }

    Cheers - L~R

      Tested. It works.
      From perldoc getpwent ...
      These routines perform the same functions as their counterparts in the system library.

      Processing /etc/passwd is *nix specific and perldoc kind of assumes we know what we're doing when it comes to *nix. The description is really in "man getpwent" with perldoc to explain the perlish differences. Yes, getpwent is an iterator.

      Also look at endpwent so you can close the database when you're done and setpwent will let you rewind to the beginning.

        rowdog,
        Exactly. I expected my answer just to be a pointer to an example in perldoc. I was on a Win32 box at the time so I couldn't confirm but I was pretty sure it worked as I expected it to. I think this would be a good documentation patch.

        Cheers - L~R

Re: simple getpwent/array question
by ronix (Novice) on Nov 11, 2008 at 14:40 UTC
    My problem more precisely is: I haven't yet figured out the syntax to load an array with an iterating function like getpwent?