C.DeRykus wrote:
Dr.Ruud:
C.DeRykus:
eval { asub() };
die $@ if $@;
You need to test the return of eval itself to be sure.
Example:
perl -wle '
die "An error: ", $@ || "whoopy"
if !eval{ asub(); 1 };
sub asub{ my $x = bless {}, "main"; 1 / 0 }
sub DESTROY{ $@ = "bad mojo" }
'
Good reminder... particularly if the eval block gets long.
Check these:
perl -wle '
die "An error: ", $@ || "whoopy"
if !eval{ asub(); 1 };
sub asub{ my $x = bless {}, "main"; my $y = 0; 1 / $y }
sub DESTROY{ $@ = "bad mojo" }
'
An error: bad mojo at -e line 4.
perl -wle '
die "An error: ", $@ || "whoopy"
if !eval{ asub(); 1 };
sub asub{ my $x = bless {}, "main"; my $y = 0; 1 / $y }
sub DESTROY{ undef $@ }
'
An error: whoopy at -e line 4.
perl -wle '
die "An error: ", $@ || "whoopy"
if !eval{ asub(); 1 };
sub asub{ my $x = bless {}, "main"; my $y = 0; 1 / $y }
sub DESTROY{ my $z = 0; 1 / $z }
'
(in cleanup) Illegal division by zero at -e line 8.
An error: Illegal division by zero at -e line 6.
(in cleanup) Illegal division by zero at -e line 8.
The size of the eval block has nothing to do with it.
There is just no other sound way to handle exceptions in Perl.
Never rely on a global variable, it can get changed.
Localizing $@ is another way, but that hides errors.
Code that might change $@ inside a DESTROY,
can politely push it to @@ first:
push @@, $@ if $@;
because then you can also check $...@}, $...@[-1], etc.
--
Ruud
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/