On Sun, Oct 01, 2000 at 12:37:55PM -0700, Nathan Wiger wrote:
> Bart Lateur wrote:
> > 
> > My idea is that if anybody sets $[ to 1 in a script, all accesses to
> > array in that script would use 1 as the index of the very first item:
> > $ary[1]. But if you pass this array (for example, a reference to it) to
> > a function in a module that doesn't set $[, it would access the same
> > first item through index 0: $ary[0] or $ref->[0].
> > 
> > And vice versa (swap "module" and "script").
> 
> This seems to make sense. But I'm pretty sure this is how it works
> already:
> 
>    package Bob;
>    sub do_print {
>       my @a = @_;
>       print $a[0];
>    }
> 
>    package main;
>    $[ = 1;
>    $array[1] = "Hello\n";
> 
>    Bob::do_print(@array);   # prints "Hello\n"
> 
> And Camel says:
> 
>    Assignment to $[ is now treated as a compiler directive, and cannot
>    influence the behavior of any other file. Its use is discouraged.
> 
> I'm really surprised by this RFC, to be honest, considering that the
> overall reaction to $[ is Don't Do That. I've always thought axing $[
> was a no-brainer.

Axing the name $[ is a good idea. But couple it with allowing a lexical

use arraybase 1;

(for file or block scope as required, as suggested by Glenn Linderman)

to keep the functionality is wanted if that makes your problem easier to
understand (and read, if it removes lots of -1 from subscribts of the
form [$foo - 1])). This makes life easier when the problem needs it.

> One interesting thing to contemplate is how this might change
> higher-order function notation. If you set $[ = 1, you'd surely want ^0
> to be changed to ^1 then? And if you set $[ = 2, would you want $1 to
> become $2 ?

I don't think so. Other languages have arrays and similar structures indexed
from 1, and it's when dealing with these that you'd like perl to talk their
way. Are there any languages with regular expressions that count from zero?
 
> The :base(1) attribute seems interesting, but not viable. In particular,
> then you'd have to check each individual array to see what its base was
> set to to know how to handle it.

Yes. I think I suggest something similar (doing it per array) but Bart's
explanation made me realise this was not wanted. One doesn't want to change
the array in any way, just how the local code sees arrays. Its use can
still be discouraged, but that's not the same as saying "never use this"

Nicholas Clark

Reply via email to