develooper Front page | perl.perl5.porters | Postings from August 2003

Re: [perl #10030] DESTROY not called on code reference objects

Thread Previous | Thread Next
Michael G Schwern
August 17, 2003 17:28
Re: [perl #10030] DESTROY not called on code reference objects
Message ID:
On Sun, Aug 17, 2003 at 11:07:15PM +0100, Dave Mitchell wrote:
> About a year ago, Simon Cozens reported a bug to the effect that
>     { my $x = bless sub { }, 'X'; }
>     sub X::DESTROY { print "DESTROYED\n" }
> Doesn't call the destructor.
> Having looked at it, the reason is that the anon sub isn't a closure
> (ie doesn't refer to any outer lexicals. Thus, the CV is shared rather
> than cloned, so its refcnt is >1 when $x is freed.
> Turning it into a closure makes it work:
>     my $y;
>     { my $x = bless sub { $y }, 'X'; }
>     sub X::DESTROY { print "DESTROYED\n" }
> Simon mentions that "either the documentation or the code are wrong".
> Since it would be inefficient to fix, and since closureless anon subs
> are more common that blessed coderefs (I would speculate), I think it
> should be documented as a misfeature.

Since it is unnecessarily surprising and inconsistent behavior, and since
the reason for it happening is purely for internal and not language reasons, 
and since we don't want people to start relying on it, and since it would 
be nice if someone could fix it sometime in the future despite predictions 
that it will be inefficient to fix, it should be documented not as a feature 
but as a bug.

Don't document bugs as features.  You will hate yourself in the morning.

Michael G Schwern
Remember, any tool can be the right tool.
    -- Red Green

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About