in reply to Re: Conditional DESTROY
in thread Conditional DESTROY

Reduced the code, something is truly messing with me, because this thing runs perfectly:

package Foo; sub new { my ($class, $params) = @_; my $self = {}; $self->{no_DESTROY} = $params->{no_DESTROY} // 0; bless($self, $class); return $self; } sub DESTROY { my $self = shift; print "in DESTROY, no_DESTROY is $self->{no_DESTROY}\n"; return if $self->{no_DESTROY}; print "DESTROY body is running!\n"; } package main; my $foo = Foo->new({ no_DESTROY => 1 });

Argh. I messed with this most of Friday and it wouldn’t budge, now the mock-up code Just Works (as actually expected, too).

Replies are listed 'Best First'.
Re^3: Conditional DESTROY
by Corion (Patriarch) on Jun 10, 2013 at 10:43 UTC

    To track this down better, maybe you can dump the object(s) in question at strategic places of the lifetime of your program. Maybe you get lucky and find that no_DESTROY gets reset/lost somewhere or a place where another object holds on too long to the object in question.

    Note that closures are notorious for holding on surprisingly long to objects:

    sub make_frobnitzer { my( $self )= @_; $self->{on_error}= sub { $self->error_on_frobnitz( @_ ); } }

    In the above case, $self will live until global destruction, because the on_error subroutine closes over $self.

      It was PEBKAC, the thing works as expected — I explained to BrowserUk below how I managed to mess things up.

      But thanks for your help :)