Losing value of $@ ($EVAL_ERROR) on DESTROY

Discussion in 'Perl Misc' started by CDeVilbiss@gmail.com, Mar 13, 2006.

  1. Guest

    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
     
    , Mar 13, 2006
    #1
    1. Advertising

  2. Guest

    wrote:
    > 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}->() }


    untested:

    sub DESTROY {
    my @save = ($@,$?,$!); #maybe others?
    shift->{cleanup}->();
    ($@,$?,$!) = @save;
    }

    ....
    >
    > 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 $@.


    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Mar 13, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nate Spillson

    Session variable losing value

    Nate Spillson, Aug 6, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    2,169
    Marina
    Aug 6, 2003
  2. bruce barker
    Replies:
    0
    Views:
    941
    bruce barker
    Aug 4, 2004
  3. Ken McCrory

    Text box losing most recent Text value

    Ken McCrory, Mar 9, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    463
    Patrick Olurotimi Ige
    Mar 10, 2005
  4. Ook
    Replies:
    2
    Views:
    352
  5. Jason C
    Replies:
    4
    Views:
    703
    Morty Abzug
    Jun 26, 2012
Loading...

Share This Page