George Mpouras <
[email protected]> wrote:
[so-called 'aliasing in for loops']
it is a perl bug or bad behavior , should change.
I don't like it -- it's an "action at a distance spookiness" effect
that I've found little use for. I just work around it.
[...]
"Misfeature" or "bad design":
I think a discrepancy between your 'mental model' of 'Perl execution'
and the way it actually works exists here: In perl, values are always
represented as SVs ('scalars') and a SV is a pretty complex object,
copying of which may be 'expensive', eg, the following perl code:
---
use Devel:
eek;
my $s = 'string';
my $ss = $s;
Dump($s);
Dump($ss);
---
causes a the string assigned to s to be copied to a newly, dynamically
allocated area of memory, as can be seen in the output generated when
running this code:
---
SV = PV(0x603b78) at 0x621188
REFCNT = 1
FLAGS = (PADMY,POK,pPOK)
PV = 0x61b9f0 "string"\0
CUR = 6
LEN = 8
SV = PV(0x603c38) at 0x6211b8
REFCNT = 1
FLAGS = (PADMY,POK,pPOK)
PV = 0x617a10 "string"\0
CUR = 6
LEN = 8
---
Because an SV is not really a 'value' in the sense of, say, a C
integer or pointer, it is always passed 'by reference' into
syntactical constructs which work with 'lists of SVs' such as map { },
for ( ) { } or subroutine calls and it is up to the user to copy the
'argument SV' in case an independently modifiable copy of the original
thing is actually needed for some reason (perl supports, supported or
was supposed to support COW-sharing of 'SV string bodies' in some
circumstance or at some point in the in the past, but except 'somebody
worked on that in the past', I don't really know any details about
that). That's similar to the way Java handles this where complex
objects are always passed 'by reference', just that Java also has
'primitive types' which are passed by value and Perl doesn't.