Quoth (e-mail address removed):
I am having a hard time understanding this perl scripting code:
while (<STDIN>) {
chomp;
($user, $gcos) = (split /:/) [0, 4];
This line first calls the split function: perldoc -f split. It's in list
context (we'll see why in a minute), and it defaults to splitting the
string in $_ that we've just read from STDIN.
The (...)[0,4] is a list slice (perldoc perldata, section "Slices"),
which selects the first and fifth of the items returned by split.
These two values are then assigned to the variables $user and $gecos
respectively.
($real) = split(/,/, $gcos);
This is another split, this time specifying what string to split. The
first result is assigned to the variable $real.
print "$user is $real \n";
}
i do not understand line 3 and 4. could someone please help me out.
much appreciated.
The program looks like a rather poor attempt at parsing a Unix passwd
file. There are at least two bugs: the GECOS field should be split on
the pattern /\s*,\s*/ instead, and the real name needs any instance of
'&' replaced with an initial-caps version of $user. The program is not
'strict'-safe: have you read the Posting Guidelines for this group yet?
Also, if it is intended to parse the real /etc/passwd file, it would be
much better to use the getpwent functions and the User:
went module, so
it works properly on systems that use other passwd facilities such as
NIS.
Ben