Dru has asked for the wisdom of the Perl Monks concerning the following question:
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):
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):
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 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]; ... }
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.use Carp; opendir(DIR, $dir) or ($error = carp("Could not open $dir"); (mail($error) and croak) if $error; sub mail { my $error = $_[0]; ... }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Improving the Error Handling of my Scripts
by Corion (Patriarch) on Mar 10, 2010 at 19:01 UTC | |
by almut (Canon) on Mar 11, 2010 at 00:00 UTC | |
Re: Improving the Error Handling of my Scripts
by elTriberium (Friar) on Mar 10, 2010 at 19:44 UTC | |
Re: Improving the Error Handling of my Scripts
by almut (Canon) on Mar 10, 2010 at 20:16 UTC | |
by toolic (Bishop) on Mar 11, 2010 at 14:37 UTC | |
Re: Improving the Error Handling of my Scripts
by Anonymous Monk on Mar 10, 2010 at 19:39 UTC | |
Re: Improving the Error Handling of my Scripts
by Hue-Bond (Priest) on Mar 11, 2010 at 08:38 UTC | |
Re: Improving the Error Handling of my Scripts
by cpbills (Initiate) on Mar 11, 2010 at 04:33 UTC | |
Re: Improving the Error Handling of my Scripts
by Dru (Hermit) on Mar 11, 2010 at 17:00 UTC |
Back to
Seekers of Perl Wisdom