O'Reilly logo

Learning Perl, Second Edition by Randal L. Schwartz, Tom Christiansen

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

A.17. Chapter 18

  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.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required