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