in reply to Strange array reference behaviour (bug?)

That is the intended behavior. It allows a reference to be passed to a sub which may alter the content, just as may be done with pointers in C.

Your loop alters @$ref because you tell it to. $test is an alias to each element in turn.

You can force copying of the array with,

for (my @foo = @$aref) { #... }
or,
for (@{[@$aref]}) { #... }

After Compline,
Zaxo

Replies are listed 'Best First'.
Re2: Strange array reference behaviour (bug?)
by dragonchild (Archbishop) on Aug 12, 2003 at 16:39 UTC
    Or, just don't modify the topic. I can't think of an instance where you can't do something other than modify the topic. Sounds like your initial design could do with some refactoring. Good luck!

    ------
    We are the carpenters and bricklayers of the Information Age.

    The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: Strange array reference behaviour (bug?)
by agrundma (Novice) on Aug 12, 2003 at 17:15 UTC
    Thanks, this is what I ended up doing, copying the reference to a temporary array. This is still really confusing...I would expect since I was using a my variable it would never alter the array. I would at least expect there to be a warning message printed in this case, but no warning is generated.

    I wonder if it's more expensive to have to create a temporary array rather than have foreach return real scalars for each value. Hmm...