[Israel.pm] swap arrays

Mikhael Goikhman migo at homemail.com
Tue Feb 17 03:35:10 PST 2004


On 17 Feb 2004 13:13:54 +0200, Offer Kaye wrote:
> 
> > Actually, there is.  And it works the same way as scalar substitution.
> > The answer is in a seldom used feature of perl called 'typeglobs'. But
> > code first:
> > perl -e '@a=(1,2);@b=(3,4);(*a,*b)=(*b,*a);print @a, at b,"\n"'
> > Output: 3412
> >
> 
> Nice, since it even works when the arrays are not the same size. Hoever, if
> I declare @a and @b with "my", this trick doesn't work anymore:
> perl -e 'my @a=(1,2);my @b=(3,4);(*a,*b)=(*b,*a);print @a, at b,"\n"'
> Output: 1234
> 
> Any idea why not?

Because globs are symbolic references, and "my" variables are not
stored in the symbol table. "our" and "local" variables are stored.

There is information about this in perlref and perldata man pages.

Regards,
Mikhael.

-- 
perl -e 'print+chr(64+hex)for+split//,d9b815c07f9b8d1e'



More information about the Perl mailing list