use strict; use warnings; use Data::Dumper; my @items = qw(name passwd uid gid quota comment gcos dir shell expire); my %user_data; # Slurp /etc/passwd once and create hash. # When executed as root, entry 'passwd' will be visible (not 'x'). while ( my @pwdata = getpwent ) { my $name = $pwdata[0]; $user_data{$name} = { map{ $_ => shift @pwdata } @items }; } # DONE! The rest is example code. # see full datastructure # print "Dump: \n", Dumper(\%user_data), "\n"; # sample output my $name = 'root'; print "=== $name ===\n"; print Dumper( $user_data{ $name } ),"\n"; print "shell: $user_data{$name}{shell}\n"; __END__ === root === $VAR1 = { 'quota' => '', 'uid' => 0, 'name' => 'root', 'dir' => '/root', 'passwd' => 'x', 'comment' => '', 'shell' => '/bin/bash', 'expire' => undef, 'gid' => 0, 'gcos' => 'root' }; shell: /bin/bash