Perl Advent Calendar 2007-12-06

He's making a list, checking it twice

by Josh McAdams & staff

Many people don't know this, but when Santa checks his list and then checks it twice, he actually reads the list from bottom to top the first time through. That's right, you heard it here first. Now, some would say that Santa is going about things backwards and that giving gifts to those who have been on the Nice List for a shorter amount of time isn't fair. What they don't understand is that Santa is actually checking to see who should be off the Nice List and those closer to the end are just more likely to fall off the wagon... it's just human nature.

Anyway, to help him read the Nice List backwards, Santa has to pull in a Perl module called File::ReadBackwards. This module doesn't have to load the entire file into memory in order to read it backwards. It is fast and memory efficient. After all, the nice list is a big list and it has to be checked twice on Christmas night. Santa needs something that can really get the job done. We'll begin with the Nice List sorted by seniority; possibly by PerlMonks or use.perl id?

Addy
Ella
Ginger
Heather
Josh
Kevin
Larry
Merlyn
Nora
Oscar
Penelope
Rusty
Sam
Tom
Uri

…which suspiciously happens to be in alphabetical order; test data or sociology breakthrough?!. Santa then could check his list twice ... backwards and forwards.

#First pass
$ wc -l nice.txt
15 nice.txt
$ mod5.pl | head -5
Uri
Tom
Sam
Rusty
Penelope
$ mod5.pl | tail -5
Josh
Heather
Ginger
Ella
Addy
$ mod5.pl | check_gui -i --news=net -o oops.txt
Searching for news on 'Uri'
News found.
Marked.
Searching for news on 'Tom'
Searching for news on 'Sam'
Stopped by user
<broken pipe>
You checked 3 records and marked 1.
Writing 1 record to "oops.txt"
$ cat nice.txt oops.txt | sort | uniq -u > nice2.txt
#Second pass
$ wc -l nice.txt
14 nice2.txt
$ head -5 nice2.txt 
Addy
Ella
Ginger
Heather
Josh
$ tail -5 nice2.txt
Oscar
Penelope
Rusty
Sam
Tom
$ packing_gui --nice nice2.txt --wants wants.db
LOAD FINISHED
Space for  1.7E6 Presents remaining
           0.0E0 Nice children not on manifest
Writing Manifest...
Writing Flight Plan...
Transmitting Manifest to SleighComp2000.......
 checksum ok
Transmitting Flight Plan to SleighComp2000.......
 checksum ok
Done

Obviously this is just a test run, I hope Santa uses the production data when they load the sleigh for real.

Below you'll see how Santa works this little bit of magic. File::ReadBackwards can be used as a tied filehandle or an object. Since all the polar craftselves deal with material objects the rest of the year, Santa prefers the OO version.

mod5.pl


   1 use File::ReadBackwards;
   2 
   3 $bw = File::ReadBackwards->new('nice.txt')
   4   or die "can't read nice file $!";
   5 
   6 while ( defined( $log_line = $bw->readline ) ) {
   7     print $log_line;
   8 }
Josh volunteered his own name for the tail end of a very short Nice List. I decided the module author, our own Uri of Boston.pm, deserved some attention too. &mdash Bill