Converting Other Languages to Perl
- 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
forloop that runs forever; this loop gets exited by thelastoperator inside, however. Each time through the loop, a new value for$user(a username) and$home(their home directory) is fetched using thegetpwentoperator.If the value of
$useris empty, theforloop 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 theforloop is triggered.The
whileloop reads a line at a time from the newsrc file. If the line begins with comp.lang.perl.announce:, theprintstatement says so, and thewhileloop is exited early.