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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.