You knew that Perl had to have a rename function, didn't you! Here's how the
init_words()
subroutine looks with the modification to rename an "old" file:
sub init_words {
while ( defined($filename = glob("*.secret")) ) {
open (WORDSLIST, $filename) || die "can't open $filename: $!";
if (-M WORDSLIST < 7.0) {
while ($name = ) {
chomp ($name);
$word = ;
chomp ($word);
$words{$name} = $word;
}
} else { # rename the file so it gets noticed
rename ($filename,"old.$filename") || die "can't rename $filename to old.$filename: $!";
}
close (WORDSLIST) || die "couldn't close $filename: $!";
}
}
Notice the new else
part of the file age check. If the file is
older than seven days, it gets renamed with the rename
function. This
function takes two parameters, renaming the file named by the first parameter to the name given in
the second parameter.
Perl has a complete range of file manipulation operators that we'll learn about in future lessons in this series.
Wow, we're getting close to completing this stroll through Perl - looks like four (maybe, five) lessons left (including the final summary lesson). The next two lessons are going to be most interesting to those of you waiting to hear about Perl and database management! For the next lesson, we're going to want to keep track of when the most recent correct guess has been made by each user.
What programming element(s) do you suppose is(are) required to complete this task?
See you next lesson!