User Tools

Site Tools


Sidebar


Site Information


Interactive Maps

Sidebar

scode:perl:die_gracefully

Die Gracefully

Had a situation where we needed to do useful things before dying when opening files, like send SMS text messages to people on call in the dead of night. Here's a possible solution.

eval {
    open PAPERIN, "<", $sPaperIn or
        die "Can't open file $sPaperIn: $!";
        # "or die" triggers eval
};
if ($@) {
    # do something useful before actually dying, here,
    # like send an SMS text message.
 
    print "We failed to open the file.\n";
    print "$@\n";           # display "or die" error contents
 
} else {
    # do something else very useful, here,
    # like process the data.
    print "\nProcessing ::" . $sPaperIn . "::\n";
    @aInput = <PAPERIN>;    # slup input file
    close PAPERIN;
 
    # [...]
 
}
scode/perl/die_gracefully.txt · Last modified: 2017/07/03 06:54 by terrill