Greetings Monks,
Almost all of my scripts run non-interactively via cron. Sometimes they have problems and I do not find out for awhile. My error handling up to this point has been shoddy and I would like to improve this. I would like to have the scripts email me with a descriptive message of where they died, but also continue to have them die gracefully (and tell why) if I ever have to run them interactively (which I occasionally do). This is how I accomplish this task now (when it gets done):
use English '-no_match_vars';
opendir(DIR, $dir)
or mail("Line 11: Could not open $dir at $PROGRAM_NAME: $OS_ERROR\n")
and die("Line 11: Could not open $dir at $PROGRAM_NAME: $OS_ERROR\n")
sub mail {
my $error = $_[0];
...
}
And this is ok, but I do not like the fact that the line number is static and I have to remember to update it if I ever modify the script (I won't remember). Also, after reading Perl Best Practices, I have been converted to using the Carp module. It provides everything I need in the error message, but I can't figure out how to redirect the carp output to my mail subroutine. I would like to do something like this (which doesn't work of course, but I think you can
get the gist of what I am attempting to accomplish):
use Carp;
opendir(DIR, $dir) or ($error = carp("Could not open $dir");
(mail($error) and croak) if $error;
sub mail {
my $error = $_[0];
...
}
Is it possible to do something like this? I know croak dies immediately, which is why I am using carp first to catch the error.
Thanks,
Dru
Perl, the Leatherman of Programming languages. -
qazwart