[Appendix A] A.17 Chapter 18, Converting Other Languages to Perl
Author: Randal Schwartz, Tom Christiansen & Larry Wall
Date: 1999-07-02T01:37:08Z

#

| Appendix A Exercise Answers |

## 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.

A.16 Chapter 17, User Database Manipulation |  | A.18 Chapter 19, CGI Programming