为什么我的getpwent()调用返回错误的用户?

Red*_*ket 3 perl

我总是忘记在Perl中如何做到这一点.这是我的脚本:

#!/usr/local/bin/perl -w
use strict;
use Data::Dumper;

my @got = getpwent();
my $username    = ${[getpwent()]}[0];

print Dumper( @got );
print "username is [$username]\n";
Run Code Online (Sandbox Code Playgroud)

......这是它产生的输出......

$VAR1 = 'root';
$VAR2 = 'xxxxxxxxxxxxxxxxxx';
$VAR3 = 0; 
$VAR4 = 0;
$VAR5 = ''; 
$VAR6 = '';
$VAR7 = 'myhost root';
$VAR8 = '/root';
$VAR9 = '/bin/bash';
username is [bin]
Run Code Online (Sandbox Code Playgroud)

...我的问题是,为什么用户名等于'bin'而不是'root'?

Jim*_*vis 5

它遍历用户; 你正在调用它两次,并为两个用户获取信息.

$ perl -E 'say $foo while $foo = getpwent()'
root
daemon
bin
sys
sync
...
Run Code Online (Sandbox Code Playgroud)