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

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

Thread Previous | Thread Next
Father Chrysostomos via RT
December 23, 2012 21:08
[perl #116110] Failure to initialize my() variables
Message ID:
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 }'

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

$ perl -le 'for(1..5) { print \my $x; push @_, \$x }'


Father Chrysostomos

via perlbug:  queue: perl5 status: open

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About