develooper Front page | perl.perl5.porters | Postings from March 2001

[BUG?] aliasing arrays

Thread Next
From:
Hugo
Date:
March 14, 2001 08:40
Subject:
[BUG?] aliasing arrays
Message ID:
200103141641.QAA01716@crypt.compulink.co.uk
This occurs identically in 5.005_03, 5.6.0, a recent bleadperl:

crypt% perl -wle '$a=[1, 2, 3]; for (@$a) { print $_; splice @$a, 0, 1 }'
1
3
crypt% 

Now, I would expect 'for (@$a)' to use a copy of the list, so that this
would print 1, 2, 3. Am I wrong to expect that, or is this a bug?

The current behaviour can result in nasty action-at-a-distance effects.

A relatively easy workaround is 'for (grep 1, @$a)'; is there any more
efficient, or more obvious, workaround?

Hugo

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