[OT] blessed sub-refs in the symbol table

Matt Lawrence matt.lawrence at virgin.net
Thu Jan 31 11:46:37 GMT 2013


On 31/01/13 11:12, David Cantrell wrote:
> I am most disappointed to find that if you put a blessed sub-ref in the
> symbol table and then replace it, its DESTROY method doesn't get called
> straight away:
>
> package Immortal;
>
> sub new { return bless sub { print "called the object\n" }, shift; }
> sub DESTROY { print "called DESTROY\n"; }
> 1;
>
> $ perl -MImmortal -e '*Foo::bar = Immortal->new(); Foo::bar(); *Foo::bar = sub {print "blargh\n"}; Foo::bar();'
> called the object
> blargh
> called DESTROY
>
> looks like the blessed sub continues to exist right to the end of the
> process, even though all references to it have gone away.  Is this a bug?
>
It seems to work the same way with SCALAR refs, but not with HASH refs. 
It implies a memory leak, which should certainly be considered a bug.

$ perl -Mstrict -wE 'sub Foo::DESTROY { say "Goodbye, cruel world" }; 
*foo = do { bless \(my $msg = "Message one"), "Foo" }; say $::foo; *foo 
= do { bless \(my $msg = "Message two"), "Foo" }; say $::foo;'
Message one
Message two
Goodbye, cruel world
Goodbye, cruel world

$ perl -Mstrict -wE 'sub Foo::DESTROY { say "Goodbye, cruel world" }; 
*foo = bless {msg => "Message one" }, "Foo"; say $::foo{msg}; *foo = 
bless { msg => "Message two" }, "Foo"; say $::foo{msg}'
Message one
Goodbye, cruel world
Message two
Goodbye, cruel world

Matt


More information about the london.pm mailing list