0th positin set when futzing with $[

P

Paul Lalli

First, I know, - I *know* - one should not play with the $[ variable,
and I would never ever do so in "real" code. Another thread mentioned
the variable, and I was bored, so I started playing. That being said,
I see the following results:

$ perl -le'
$[ = 7;
@foo = qw/alpha beta gamma/;
print "$_ => $foo[$_]" for 0 .. $#foo;
'
0 => alpha
1 =>
2 =>
3 =>
4 => alpha
5 => beta
6 => gamma
7 => alpha
8 => beta
9 => gamma

I can understand positions 7-9 being set. 7 is the first position of
the array now. And I can understand position 4-6 being set. This is
for the "wraparound" feature that we normally see when $[ hasn't been
altered and we access negative indices . But can anyone explain to me
why the position 0 is set? (and yes, I know -w would give me three
"use of uninitialized" warnings for 1-3 above. Not relevant to my
question, so I omitted it).

I do not see any similar results when printing the values of, say -10
through 3 if $[ has not been altered. So I assume this is specific to
the 0th position, after $[ has been modified...

This is perl, v5.8.4 built for sun4-solaris

Paul Lalli
 
B

Ben Morrow

Quoth "Paul Lalli said:
First, I know, - I *know* - one should not play with the $[ variable,
and I would never ever do so in "real" code. Another thread mentioned
the variable, and I was bored, so I started playing. That being said,
I see the following results:

$ perl -le'
$[ = 7;
@foo = qw/alpha beta gamma/;
print "$_ => $foo[$_]" for 0 .. $#foo;
'
0 => alpha
1 =>
2 =>
3 =>
4 => alpha
5 => beta
6 => gamma
7 => alpha
8 => beta
9 => gamma

I can understand positions 7-9 being set. 7 is the first position of
the array now. And I can understand position 4-6 being set. This is
for the "wraparound" feature that we normally see when $[ hasn't been
altered and we access negative indices . But can anyone explain to me
why the position 0 is set? (and yes, I know -w would give me three
"use of uninitialized" warnings for 1-3 above. Not relevant to my
question, so I omitted it).

I do not see any similar results when printing the values of, say -10
through 3 if $[ has not been altered. So I assume this is specific to
the 0th position, after $[ has been modified...

Yup. One thing you didn't try was

-4 =>
-3 => alpha
-2 => beta
-1 => gamma

(with $[=7). The relevant code is in pp_hot.c:pp_aelem

if (elem > 0)
elem -= PL_curcop->cop_arybase;

where elem is the index requested and PL_curcop->cop_arybase is the
value $[ had when the current statement was compiled (you realise, I
presume, that $[=foo; statements are not ordinary variable assignments,
and are processed more like 'use arybase foo;'? In particular, their
effect is compile-time and lexically scoped).

That line (well, the 'if (elem > 0)' line) has been there since the
initial 5.003 checkin when p5p moved over to Perforce, so I can't get a
commit log to see why it's there...

WRT the original thread, I think that this carping on about how @a - $#a
is not necessarily 1 because of $[ is seriously unhelpful. Noone in
their right mind would use $[ in real code, and it must just confuse
beginners more to have some weird $[ thrown at them when they're not
understanding scalar @a vs. $#a yet.

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,786
Messages
2,569,625
Members
45,320
Latest member
icelord

Latest Threads

Top