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 schwern@pobox.com http://www.pobox.com/~schwern/ Remember, any tool can be the right tool. -- Red GreenThread Previous | Thread Next