Subject: | Garbage collection might break TryCatch |
If an object goes out of scope while leaving the try-block, the objects
DESTROY method might clear $@ and thus breaking TryCatch's exception
handling. See the attached example.
Subject: | try_trycatch.pl |
#!/usr/bin/perl
use warnings;
use strict;
package Foo;
sub new {
my $class = shift;
return bless {}, $class;
}
sub DESTROY {
my $self = shift;
eval { 1; };
}
package main;
use TryCatch;
sub throw {
$TryCatch::Exception = shift;
die $TryCatch::Exception;
}
try {
my $foo = Foo->new();
open my $fh, ">", "/"
or throw( "Could not open /: $!" );
} catch ($err) {
print STDERR "Something bad happened: $err\n";
};
print "Done\n";