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

[perl #116110] Failure to initialize my() variables

Thread Previous | Thread Next
From:
Father Chrysostomos via RT
Date:
December 23, 2012 21:08
Subject:
[perl #116110] Failure to initialize my() variables
Message ID:
rt-3.6.HEAD-17500-1356296889-1220.116110-15-0@perl.org
On Mon Dec 17 08:06:40 2012, tlhackque wrote:
> In reviewing all the related items, I noted some confusion about what 
> the right semantics for "my foo = EXPR (modifier)" should be.
> 
> Here's an attempt at a precise formulation:
> 
> When a my declaration includes an initializing expression, it is 
> executed AS IF the following sequence of steps is taken:
> 
>  1. For each item on the left hand side of the declaration, an 
> anonomyous piece of storage is allocated, of the type indicated by the 
> item's SIGIL.  Each of these is initialized to the null value (undef or 
> ()) matching its type.

The problem here is that in ‘my $x if $false’ the ‘my $x’ is never executed.

‘my $x’ conceptually creates the scalar, so ‘my $x if 0; $x’ ends up
referring to a scalar that conceptually does not exist.  Since perl
cheats and reuses the same scalar for efficiency, this has the effect of
leaking values from one entry of the surrounding scope to the next.

I agree this should be fixed, but I disagree that it is high priority. 
I also don’t offhand see a way to fix it that would not slow down early
returns (return if $foo; my $x....).  (Maybe we need SAVEt_PAD and
pp_padsv should turn off PADSTALE unconditionally; I don’t know....)

BTW, this is how you can tell perl is reusing the scalar:

$ perl -le 'for(1..5) { print \my $x }'
SCALAR(0x826db0)
SCALAR(0x826db0)
SCALAR(0x826db0)
SCALAR(0x826db0)
SCALAR(0x826db0)

But it knows how to hide that fact when the scalar is referenced elsewhere:

$ perl -le 'for(1..5) { print \my $x; push @_, \$x }'
SCALAR(0x826db0)
SCALAR(0x803bb0)
SCALAR(0x826d30)
SCALAR(0x803aa0)
SCALAR(0x826df0)

-- 

Father Chrysostomos


---
via perlbug:  queue: perl5 status: open
https://rt.perl.org:443/rt3/Ticket/Display.html?id=116110

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