Perl Advent Calendar 2006-12-10

Grandma Got Run Over by a Reindeer

by Jerrad Pierce

Santa may be jolly, but he's also mighty strict. The jolly old elf was quite pleased when he recently discovered a module to help him cope with code that does not comply with best practices. Better yet, it's part of the core distribution. Through the addition of a thin wrapper, Fatal induces subroutines and functions that would otherwise return false when an error occurs to instead throw an exception. This has several advantages including error propagation and the ability to compensate for nonexistent error-handling in code you do not control. Unfortunately, the current version (1.03) thwarts this latter benefit as noted here on p5p. The patch offered in the message is available below, and required by the sample code. - Requires this patch

   1 package main;
   2 use Fatal qw/CORE::GLOBAL::open CORE::GLOBAL::close/;
   4 eval{ Note::thankYou(qw/Alvin Bobby Cindy David Erica Freddie/) };
   5 warn("Naughty package! $@\n") if $@;
   8 package Note;
   9 use File::Temp 'tmpnam';
  11 sub thankYou{
  12   foreach my $child (@_ ){
  13     my($fh, $file) = tmpnam();
  15     #Compose thank you for exceptional cookies and milk...
  17     #strict-less example using typo to force failure in closing unopened handle
  18     close($Fh);
  20     #Do something with the message $file
  21   }
  22 }