0th positin set when futzing with $[

Discussion in 'Perl Misc' started by Paul Lalli, Aug 3, 2006.

  1. Paul Lalli

    Paul Lalli Guest

    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
     
    Paul Lalli, Aug 3, 2006
    #1
    1. Advertising

  2. Paul Lalli

    Ben Morrow Guest

    Quoth "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...


    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

    --
    I must not fear. Fear is the mind-killer. I will face my fear and
    I will let it pass through me. When the fear is gone there will be
    nothing. Only I will remain.
    Frank Herbert, 'Dune'
     
    Ben Morrow, Aug 3, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alan Silver
    Replies:
    0
    Views:
    549
    Alan Silver
    Dec 21, 2005
  2. Manav
    Replies:
    0
    Views:
    800
    Manav
    Oct 15, 2003
  3. Harald Kirsch
    Replies:
    4
    Views:
    2,872
    Harald Kirsch
    Aug 31, 2004
  4. Replies:
    9
    Views:
    434
    Paul McGuire
    Sep 16, 2006
  5. Marc Woolfson

    DataGrid.PageIndexChanged not firing on when 0th page requested

    Marc Woolfson, May 23, 2007, in forum: ASP .Net Datagrid Control
    Replies:
    4
    Views:
    973
    Scott M.
    May 25, 2007
Loading...

Share This Page