Bug: Your get_data() returns only the last hit (e.g. for james).

FWIW, see also:

Maybe it is easier to read /etc/passwd (root: /etc/shadow too) into a hash once and then access the hash by user-id?

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

...or find a CPAN module that suits your requirements.

Update: This example covers the get_data() part of your question.

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]; my $pw_line = join(':', @pwdata); # reconstruct passwd-line $user_data{$name} = { map{ $_ => shift @pwdata } @items } +; $user_data{$name}{pw_line} = $pw_line; } # 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"; # find users where regex matches sub get_data { my $regex = shift; return sort grep { $user_data{$_}{pw_line} =~ /$regex/ } keys %user_ +data; } print "Bash-Users: ", join(", ", get_data( qr{/bin/bash} )) , "\n"; __END__ === root === $VAR1 = { 'quota' => '', 'uid' => 0, 'name' => 'root', 'dir' => '/root', 'passwd' => 'x', 'pw_line' => 'root:x:0:0:::root:/root:/bin/bash', 'comment' => '', 'shell' => '/bin/bash', 'expire' => undef, 'gid' => 0, 'gcos' => 'root' }; shell: /bin/bash Bash-Users: at, bin, daemon, ftp, lp, man, news, nobody, root, uucp </readmore>


In reply to Re: Passing Regex Pattern in Subroutine by Perlbotics
in thread Passing Regex Pattern in Subroutine by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.