On 1/15/07, Jay Savage <[EMAIL PROTECTED]> wrote:
On 1/15/07, Leonid Grinberg <[EMAIL PROTECTED]> wrote:
> > The backslash escapes the character following it.
>
> Oh, that's right. Sorry.
>
> > Using scalar() there is redundant as the expression is already in scalar
> > context.  Also using "scalar(@split_home) - 1" will not work correctly if 
the
> > value of $[ has been changed.
>
> What exactly *is* $[? It has been confusing me for some time.
>

$[ is the index of the first element of arrays within its lexical
scope scope. Normally Perl arrays are zero indexed,  so given @array =
qw[a b c d]

     $array[0] == "a"
     scalar(@array) == 4
     $#array == @array -1 == 3

if $[ gets changed to, say, 1, though

    $array[0] undefined and attempts to reference it return an error
    $array[1] == "a"
    scalar(@array) == 4
    $#array == @array == 4

Theoretically, $[ could be set to any number, so relying on the
equation $#array == @array - 1 is considered bad form. Use $#array to
get the index of the last element and $array[-1] to access the last
element.

In practice, no one ever changes $[ in a script because it yeilds
unpredictable results. (In addition to modifying all the arrays in the
scope, it also changes the behavior of substr() and everything that
relies on it. See perlvar for the details.) There are, however,
modules that modify all sorts of internal variables and symbol tables
to enhance efficiency and work magic, and there is always a slight
chance that someone, somewhere has messed with $[ and not told you
about it. If they have, you'll end with unexplained and nearly
impossible to diagnose fencepost errors in your code if you expect
that $#array and @array -1 will always return the same value, and
$array[$#array] and [EMAIL PROTECTED] -1] will always refer to the same
item.

HTH,

-- jay

Reply-to strikes again (or, rather, doesn't)

--j
--------------------------------------------------
This email and attachment(s): [  ] blogable; [ x ] ask first; [  ]
private and confidential

daggerquill [at] gmail [dot] com
http://www.tuaw.com  http://www.downloadsquad.com  http://www.engatiki.org

values of β will give rise to dom!

Reply via email to