[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]
Re: __DIE__ for mere mortals
>How about us who only want to know if the current death occured
>inside an eval, no matter if it's compiling or executing ?
Isn't this just a way of asking whether the currently raised exception
has been caught? If you are within an eval $string or eval BLOCK,
then all exceptions are caught. If you are not within either sort
of eval, then none are.
It's not always obvious to everyone how one goes about asking for
exception handling during compilation but not during execution of
an eval string, although it's sometimes convenient. For example:
# variation on Larry's rename script
($op = shift) || die "Usage: $0 expr [files]\n";
$code = eval "sub { $op }" || die "$0: Bad operation $op: $@\n";
chomp(@ARGV = <STDIN>) unless @ARGV;
for (@ARGV) {
$was = $_;
&$code(); # no exception handling during execution
if ($was ne $_) {
rename($was,$_) || die "can't rename $was to $_: $!"
}
}
If all you want to do detect whether the currently raised exception
has been caught, do this
eval { main_program() };
if ($@) {
# uncaught exception propagated
}
You can do this without altering your main program's code to wrap
it in a function in two ways.
1. Use the oft-ignored do-FILE construct:
do 'programfile';
if ($@) {
# uncaught exception propagated
}
2. Install a $SIG{__DIE__} handler, perhaps from a used module or even
via a -Mmodule command line switch:
$SIG{__DIE__} = sub {
# uncaught (read: fatal) exception propagated
};
The second solution reasonably assumes that the current egregious
bugs affiliated with $SIG{__DIE__} have been fixed so this facility
can be used for the purpose it was originally designed for, and so
that its current bugset stop needlessly messing everybody up. This
seems a reasonable assumption, however. :-)
--tom
[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]