This is only my second go at writing something that works. It was my idea to have a program look up usernames and return info on them them including a harsh email address. Beware if you run it in a NIS environment, the csv-file could get large. I am aware of the global variable issue.
#!/usr/bin/perl -w ####################################### #you need to create a file called # #users.csv in the same dir as # #you run the script in. # # no strict 'vars' isn't that a good # #idea to use. # ####################################### use strict; no strict 'vars'; $userfile = "users.csv"; $yourhost = "\@yourdomain.??"; main(); sub main { create(); my $input = input(); print "You are looking for $input\n"; match(); exit; } sub match { open FILE, "<$userfile" or die "Can't read $userfile; $! \n"; my ( $name, $uid, $dir ); my $found; search: while (<FILE>) { chop; ( $name, $uid, $dir) = split( /:/, $_ ); if ($name eq $youare) { $found = $.; print "Located person: $youare on line $found\n\n"; print "Person: $youare\n"; print "UID: $uid\n"; print "Homedir: $dir\n"; print "Email: $name$yourhost\n"; } } exit; } sub input { print "Enter username to look for\n"; $youare = <STDIN>; chomp $youare; return $youare; } sub create { if ((-M $userfile > 3) or (-z $userfile)) { while (($name, undef, $uid, undef, undef, undef, undef, $dir,) + = getpwent) { open USERDATA, ">>$userfile" or die "Can't open $userfile for Appending: $!\n"; my $record = join( ":", $name, $uid, $dir ); print(USERDATA "$record\n") or die "Error writing to $userfile: $!\n"; } } }

In reply to Fiddeling with getpwent in csv by bubble

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.