develooper Front page | perl.perl5.porters | Postings from May 2006

Re: [perl #3306] DESTROY doesn't get called.

Thread Previous | Thread Next
From:
Dave Mitchell
Date:
May 1, 2006 10:00
Subject:
Re: [perl #3306] DESTROY doesn't get called.
Message ID:
20060501170105.GC2758@iabyn.com
On Mon, May 01, 2006 at 07:32:44PM +0300, Yuval Kogman wrote:
> On Mon, May 01, 2006 at 17:38:13 +0100, Dave Mitchell wrote:
> 
> > One possible fix would be a COW scheme: when bless tries to bless a code
> > ref that happens to be an anon sub prototype, a new prototype is made and
> > stuck in the pad for future use. But that doesn't completely solve the
> > problem: any existing refs to the shared protoype will get to see the
> > blessed version of the sub.
> 
> Why is there a distinction between prototype and non prototype?

When an anonymous sub is *compiled*, a sub prototype is created.
Normally each time you execute "sub {...}", a copy of that prototype is
created (which includes capturing any outer lexicals), and a reference to
it is returned. In the optimisation case, no lexicals are captured,a dn
the returend ref just points to the prototype rather than to a copy.

> sub foo { sub { } };
> my $sub = foo();
> my $copy = foo();
> 
> aren't $sub and $copy different ref addrs?

$sub and $copy are two distinct RVs that point to the same CV:

$ perl588 -we 'sub foo { sub { } } my $sub = foo(); my $copy = foo(); print "$sub $copy\n"'
CODE(0x9696bdc) CODE(0x9696bdc)

> and don't they have 'bless' handled by simply copying the sub to a
> "normal" copy?

bless currently just updates the pointed-top CV.

> traditionally the optimization is that if the copy count is 1 then
> this allocation can be written in place - this is more like shared
> constants.

I don't understand what point you're trying to make.

-- 
In my day, we used to edit the inodes by hand.  With magnets.

Thread Previous | 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