Losing value of $@ ($EVAL_ERROR) on DESTROY



I'm working on a system that uses Net::Telnet to run commands on a
remote system (among other things, allocating and freeing resources).

Stripping out the irrelevant stuff and paraphrasing, my problem stems
from this:

package TrackedResource;

sub new {
my ($class, $cleanup_sub) = @_;
my $self = { cleanup => $cleanup_sub };
return bless $self, $class;
}

sub DESTROY { shift->{cleanup}->() }

package main;

sub testcase {
my ($telnet) = @_;
my $resource = $telnet->cmd('allocate');
my $cleanup = new TrackedResource(sub {
$telnet->cmd("deallocate $resource"); # calls waitfor
});
# use resource for stuff
# blah blah
die "argh, this is bad"; # some exception happens here
undef->callSomething; # or here
# blah blah
return 'Success';
}

my $telnet = new Net::Telnet($host);
my $rc = eval { testcase($telnet); }
print qq{died with '$@'\n};
__END__

The script always displays a blank value for $@.
Having used a watch in the debugger, the problem seems to be
that the contents of the exception are lost when
Net::Telnet::waitfor does an eval while DESTROYing my
TrackedResource, which clears $@.

Are there any good strategies for dealing with this? What I really
want is to be able to see the content of the first exception that
occurs within my eval at the top-level, regardless of what happens
in any DESTROY blocks.

When I looked at the perlvar entry for $SIG{__DIE__}, there was
some verbiage about overriding CORE::GLOBAL::die. Are there
any better approaches to this problem than something like

my $global_exception;
$SIG{__DIE__} = sub {
$global_exception = @_ == 1 ? $_[0] : "@_";
delete $SIG{__DIE__};
}

# now run the test
__END__

Thanks in advance for any ideas.
Colin DeVilbiss

.



Relevant Pages

  • Invalied_Viewstate exception question
    ... I will provide the stack trace below. ... are changing the hidden field _VIEWSTATE which in turn causes this exception. ... ("blah blah blah" a bunch of encrypted data or something that I ...
    (microsoft.public.vsnet.general)
  • Invalid_Viewstate exception with Invalid character in a Base-64 st
    ... I will provide the stack trace below. ... are changing the hidden field _VIEWSTATE which in turn causes this exception. ... ("blah blah blah" a bunch of encrypted data or something that I ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Invalid_Viewstate exception with Invalid character in a Base-64 st
    ... Invalid_Viewstate exception with Invalid character in a Base-64 ... ("blah blah blah" a bunch of encrypted data or something that I ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: thoughts on the school/grades/self-esteem thread.....
    ... it's amazing how in general(and this group is no exception) ... virtually everyone loves to talk about how school is just so "easy" ... for them and they are so brilliant and blah blah blah.......it's like ... You, like most med students I've known, put yourself in an ...
    (rec.sport.football.college)
  • RE: How to get rid of Bad Coding Habits /// return status
    ... Sloan. ... And just explain why they should not populate some return "string" with the ... exception message property and instead, ... msg = "The blah blah blah failed" ...
    (microsoft.public.dotnet.languages.csharp)