develooper Front page | perl.perl5.porters | Postings from July 2001

Re: [PATCH B::Deparse] Make warnings handling more robust

Thread Previous | Thread Next
From:
Graham Barr
Date:
July 5, 2001 14:49
Subject:
Re: [PATCH B::Deparse] Make warnings handling more robust
Message ID:
20010705224757.F52398@pobox.com
On Thu, Jul 05, 2001 at 10:10:49PM +0100, Mike Guy wrote:
> Graham Barr <gbarr@pobox.com> wrote
> > It will be optimized away, because there is nothing else that can reference the
> > variable. ie the sub is the only thing holding a reference count on the variable,
> > so we know it cannot be changed bu anything else
> 
> Sadly, no.   At the time the subroutine is compiled, within the scope
> of the lexical variable, there is a second reference, from the pad.
> How do you know we aren't going to write

It did originally optimize this, so something has changed.

> 
> { my $x = 10;
>   sub const_x { $x };
>   $x = 11;
> };

The check is not done when the sub is compiled, but when it is used.

So id const_x is used anywhere in the same file it will not be optimized because
a reference exists in the optree.

> 
> And we can demonstrate directly:
> 
> perl -wMO+Deparse
> { my $y = 17;
> sub yyy () { $y }
> sub xxx () { 17 }
> }


Try

BEGIN
{ my $y = 17;
 *yyy = sub () { $y };
 sub xxx () { 17 }
}

The reason is because my has a run time part, so $y
exists in the optree.

Graham.

> print xxx + yyy
> __END__
> sub yyy () {
>     $y;
> }
> sub xxx () {
>     17;
> }
> {
>     my $y = 17;
> }
> print 17 + yyy;		# <====  xxx optimised, yyy isn't
> - syntax OK
> 
> 
> Mike Guy

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