develooper Front page | perl.perl5.porters | Postings from January 2012

Re: [perl #78194] Referencing a PADTMP twice produces two copies

Thread Previous | Thread Next
From:
Dave Mitchell
Date:
January 13, 2012 05:59
Subject:
Re: [perl #78194] Referencing a PADTMP twice produces two copies
Message ID:
20120113135914.GL2962@iabyn.com
On Sun, Jan 08, 2012 at 02:21:34PM -0800, Father Chrysostomos via RT wrote:
> On Sun Jun 05 16:04:53 2011, sprout wrote:
> > On Sun Oct 03 14:08:37 2010, sprout wrote:
> > > $ perl  -le' for( "$y" ){print \$_.\$_}'
> > > SCALAR(0x8039f0)SCALAR(0x8038d0)
> > > 
> > > (The same address should be printed twice.)
> > > 
> > > It looks as though the TARG ought to be stolen by S_refto, and then
> > > replaced via pad_swipe or something similar.
> > 
> > It might be cleaner for not only S_refto, but also any op that aliases a
> > PADTMP to copy it. That would include for, map, grep, arguments to
> > subroutines, and the lhs of (...)x...
> 
> Here is a difficult case:
> 
> $ ./perl -Ilib -e 'use Devel::Peek; "$_" =~ /(?{ Dump $_ })/;'
> SV = PVMG(0x824b78) at 0x826ba0
>   REFCNT = 1
>   FLAGS = (PADTMP,SMG,POK,pPOK)
> ...
> 
> Fixing the operators I mentioned above is easy.  Fixing patterns without
> a speed hit (since regexps are used so often) is not so easy.
> 
> Are regular expressions with code blocks in them flagged somehow, so we
> can avoid making a copy for every PADTMP =~ // ?

yes, RXf_EVAL_SEEN. It's used at the start of S_regtry to decide whether
to alias $_ (and PL_reg_eval_set is then set, to avoid doing it multiple
times).

> In case it’s not obvious, this is the bug I’m trying to fix:
> 
> $ ./perl -Ilib -le '"$_" =~ /(?{ print \$_; print \$_ })/;'
> SCALAR(0x803a10)
> SCALAR(0x803b50)
> 
> Taking a reference to $_ twice should not create references to two
> different variables.  The implementation details are leaking through.

Confused the hell out me! :-) I didn't realise until I looked at it just
now, that $_ is aliased within code blocks.


-- 
Gravity is just a theory; teach Intelligent Falling in our schools!
    http://www.theonion.com/content/node/39512

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