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

Re: [perl #24435] setting multiple blank references using multipliers ties them together

Thread Previous
From:
Mark Jason Dominus
Date:
November 6, 2003 20:25
Subject:
Re: [perl #24435] setting multiple blank references using multipliers ties them together
Message ID:
20031107042136.4191.qmail@plover.com

> Trying to create multiple blank references on one line, like so:
> 
> my($var1 , $var2 , $var3) = ([])x3;
> 
> Ties them together in such a way that doing:
> 
> push($var1,"Foo bar.");
> 
> Will actually push that on to $var2 and $var3 as well.  

Thanks for your report.  That is not a bug.  Consider the following
code:


        $a = [];
        $b = $a;
        $c = $a;

Now we have one array, and three references to that array.  

This is the same situation that you have created with your example.
The 'x' operator does not repeatedly execute code, the way a 'for'
loop does.  Instead, it copies a value.  In this case, the value was a
reference to the (single) anonymous array created by the (single) '[]'
operator.  The reference was duplicated, so that you had three
references that all referred to the same array.

I hope this explanation clears up your confusion about what was going
on here.

Thanks again for your report.


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