To process exceptions rather than automatically dying:
eval {
# my code here
# for example, a call to whatever subroutine is calling
# $ftp->put($my_file) or die
return 1;
} or do {
my $err=$@;
# my exception handling code here, uses $err, not $@
}
And some explanatory notes:
- eval {...} is an eval block. It prevents exceptions from killing your script.
- return 1; or do {...} will be triggered if (a) something within eval {...} dies or (b) nothing dies, but the final statement evaluates to any "false" value: 0, undef, or the empty string. Since we only want to get into the or do {...} when something dies, we have to make sure that the last line is always true. Returning 1 does that. Note: returning within an eval block only exits the eval block, not any containing sub.
- do {....} Anything more than a single statement after "or" needs to be enclosed in a do block.
- my $err=$@ $@ holds the exception describing why we died. However, it is rather volatile and gets reassigned the next time we do something wrong. So we save it to another variable before we do anything that could change its value.
Best, beth
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.