http://www.perlmonks.org?node_id=495598


in reply to die in a DESTROY

Ok. I don't think  exit is right for me; I am writing a module and would like the code which uses it to have the option to trap it with eval.

The gist of the module is a series of pushes onto a stack, which then get sent to a DB upon request. The send-to-DB is an explicit call. I've got a DESTROY that checks the stack is empty (either never was pushed, or was pushed but then properly sent off to DB) when the object goes out of scope.

I want the DESTROY do something like this

# untested sub DESTROY { my ($self) = @_; die "destroy called with non-empty stack" if $self->stack_count; }
Now, the caller could trap this die if they knew what they were doing, or if they didn't, they'd get a fatal die if they used the module improperly (eg not calling send-to-DB).

So that's what I want to do:

die from a DESTROY in such a way isn't caught by the DESTROY itself, but could be caught (eg eval) by a module user.

Do I have any shot at getting this behavior?

water }

Replies are listed 'Best First'.
Re^2: die in a DESTROY
by sauoq (Abbot) on Sep 28, 2005 at 05:30 UTC

    Now that you've explained what you want, it seems this is a difficult problem. I played with it a bit, trying some obvious things with caller and such to no avail. I'm guessing you'd have to resort to using B to cobble together a workaround.

    -sauoq
    "My two cents aren't worth a dime.";