Learning Perl

Learning PerlSearch this book
Previous: A.16 Chapter 17, User Database ManipulationAppendix A
Exercise Answers
Next: A.18 Chapter 19, CGI Programming
 

A.17 Chapter 18, Converting Other Languages to Perl

  1. Here's one way to do it:

    for (;;) {
        ($user,$home) = (getpwent)[0,7];
        last unless $user;
        next unless open(N,"$home/.newsrc");
        next unless -M N < 30; ## added value :-)
        while (<N>) {
            if (/^comp\.lang\.perl\.announce:/) {
                print "$user is a good person, ",
                "and reads comp.lang.perl.announce!\n";
                last;
            }
        }
    }

    The outermost loop is a for loop that runs forever; this loop gets exited by the last operator inside, however. Each time through the loop, a new value for $user (a username) and $home (their home directory) is fetched using the getpwent operator.

    If the value of $user is empty, the for loop exits. The next two lines look for a recent .newsrc file in the user's home directory. If the file cannot be opened, or the modification time of the file is too distant, the next iteration of the for loop is triggered.

    The while loop reads a line at a time from the .newsrc file. If the line begins with comp.lang.perl.announce:, the print statement says so, and the while loop is exited early.


Previous: A.16 Chapter 17, User Database ManipulationLearning PerlNext: A.18 Chapter 19, CGI Programming
A.16 Chapter 17, User Database ManipulationBook IndexA.18 Chapter 19, CGI Programming