develooper Front page | perl.perl5.porters | Postings from April 2017

Unexpected DESTROY outside of RUN phase?

Thread Next
From:
Todd Rinaldo
Date:
April 26, 2017 18:11
Subject:
Unexpected DESTROY outside of RUN phase?
Message ID:
DBB73866-F229-4353-BE91-C6C63D92AD2F@cpanel.net
While discussing the nature of stashes and GV structures, we ran into what I felt was anomaly. My assumption was that when no stash points to a GV, the things pointed to by the GV have their refcount decremented and are destroyed where appropriate.

Example: by re-assigning $Foo::Bar to undef, the original object goes out of scope and DESTROY is invoked.

$>cat foo.pl
#!/usr/bin/perl
package Baz;

package Foo;
our $Bar;
BEGIN {
    $Bar = bless {}, 'Foo';
}

sub DESTROY {
    print "I'm destroying in ${^GLOBAL_PHASE} -- $_[0]\n";
}

package main;
{
    $Foo::Bar = undef;
}
print "Happened before now\n";

END { print qq{=== end\n} }
INIT { print qq{=== init\n} }

$> perl foo.pl
=== init
I'm destroying in RUN -- Foo=HASH(0x16911c8)
Happened before now
=== end 


DESTROY for $Foo::Bar happens at runtime as expected. 

However when I delete the key Bar in the Foo:: stash, destroy doesn't happen until END.

$>cat foo.pl 
#!/usr/bin/perl
package Baz;

package Foo;
our $Bar;
BEGIN {
    $Bar = bless {}, 'Foo';
}

sub DESTROY {
    print "I'm destroying in ${^GLOBAL_PHASE} -- $_[0]\n";
}

package main;
{
    delete $Foo::{'Bar'};
    eval 'use Devel::Peek; Dump $Foo::Bar';
}
print "Happened before now\n";

END { print qq{=== end\n} }
INIT { print qq{=== init\n} }

$>perl foo.pl 
=== init
SV = NULL(0x0) at 0x896378
  REFCNT = 1
  FLAGS = ()
Happened before now
=== end
I'm destroying in END -- Foo=HASH(0x8961f8)

What it looks like is the refcount for the GV isn't decremented when the key/value is removed from a hash (stash). I can come up with reasons why it might have to be this way but I thought I'd start with: 

Is this known? Is this by design?

Thanks,
Todd

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About