With a little tweaking of the code it can be done. I was playing around with it so there is more code in Splacker then needed.
#!/usr/bin/perl
use warnings;
use strict;
BEGIN {
# control eval's bypass of die
$SIG{__DIE__} = \&Splacker;
}
my $error_log = '';
print $], "\n";
{ package My::Obj;
sub new {
my $class = shift;
bless {@_}, $class
}
sub DESTROY {
my $self = shift;
eval { 1 } if ! $self->{finished};
}
}
my $o1 = 'My::Obj'->new(finished => 1);
undef $o1;
# Exception Found.
eval {
my $o2 = 'My::Obj'->new;
die "Exception 0!";
};
# And Caught
if (Splacker()) {
warn 'Caught with Splacker: '.Splacker();
}
# Give back die control if you want...
$SIG{__DIE__} = \&CORE::die;
# Exception overlooked.
eval {
my $o2 = 'My::Obj'->new;
die "Exception 1!";
};
if ($@) {
warn "Caught with \$\@: $@";
}
# Exception details lost.
eval {
my $o2 = 'My::Obj'->new;
die "Exception 2!";
1 } or do {
warn "Caught with or: $@";
};
# Same as above.
use Try::Tiny;
try {
my $o3 = 'My::Obj'->new;
die "Exception 3!";
} catch {
warn "Caught with Try::Tiny: $_";
};
sub Splacker {
my $error = shift || '';
if ($error eq 'DIE') {
print STDERR $error.' '.$error_log;
CORE::exit(1);
}
elsif ($error) {
$error_log = $error;
# print STDERR $error;
}
else {
return $error_log;
}
}
Output:
Caught with Splacker: Exception 0! at C:\xampp\cgi-bin\Test\tester.pl
+line 33.
Caught with or: at C:\xampp\cgi-bin\Test\tester.pl line 56.
Caught with Try::Tiny: at C:\xampp\cgi-bin\Test\tester.pl line 67.
5.008008
|