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

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

Thread Previous
From:
Dave Mitchell
Date:
August 20, 2003 13:25
Subject:
Re: [perl #10030] DESTROY not called on code reference objects
Message ID:
20030820184429.GA27719@fdgroup.com
On Wed, Aug 20, 2003 at 07:12:22PM +0100, Nick Ing-Simmons wrote:
> Dave Mitchell <davem@fdgroup.com> writes:
> >But named subs also capture their lexical state at creation time,
> 
> Things get confusing:
> 
> 
> sub harry
> {                 
>  my $foo = shift;
>  sub fred { print "$foo\n" }
> }                
> 
> harry('Ouch');
> harry('Weird');
>            
> 
> fred();

Run it with -w and you get:

Variable "$foo" will not stay shared at /tmp/p line 6.

Subs capture their lexical environment at creation time. When fred is
created, the instance of $foo it captures happens to be the first one.
A bit later, 'Ouch' is assigned to this instance. Even later, 'Weird' is
assigned to the second instance of $foo, but fred still only sees the
first instance.

D.

-- 
Justice is when you get what you deserve.
Law is when you get what you pay for.

Thread Previous


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