views:

130

answers:

4

I tried Google.

I tried Yahoo.

I tried Bing.

None of them had results for the $[ variable. Does anyone know where I can find the documentation for it?

+16  A: 

perldoc perlvar on the command line (perldoc should come with perl) or on perldoc.perl.org:

$[      The index of the first element in an array, and of the first
        character in a substring. Default is 0, but you could
        theoretically set it to 1 to make Perl behave more like awk (or
        Fortran) when subscripting and when evaluating the index() and
        substr() functions. (Mnemonic: [ begins subscripts.)
...
Anonymous
Thanks. I am using Perl on Windows, so I don't know if perldoc is available...
George Edison
It depends on whether the distribution included it. ActiveState Perl does, as do Strawberry and Vanilla Perl. It's rare perldoc isn't included on any platform. Type perldoc in a cmd window and try it out!
Anonymous
Ah yes, I have ActiveState :)
George Edison
Since perldoc uses the system's MORE.EXE it kind of sucks to use. Fortunately, ActivePerl also installs HTML versions of the the perldoc. You can find it under the start menu. Also, when you install a module using PPM, HTML perldoc for it is added to the rest of the HTML docs.
daotoad
@daotoad: My perldoc uses `less`, probably (I can't remember) because I set the env var "pager" to "less". I'm probably using Cygwin's less, but you can get it without a full Cygwin install.
Anonymous
Thanks, HTML works the best for me.
George Edison
Oh, and ActiveState usually installs local HTML Perl docs (c:\perl\html\index.html on a default Windows install), but it's rather hard to navigate, suffers from has broken intra-module links, and often the html doesn't get built for non-ppm modules. But, it's great for core Perl docs like this.
Anonymous
You can rebuild ActivePerl's HTML docs to catch non-PPM modules: `perl -MActivePerl::DocTools -e "UpdateHTML(1)"`
Michael Carman
A: 

$[ is a scalar containing the first index of all arrays. It's usually 0 since perl arrays are zero-indexed. You can set it to a different value though, like 1, and then all your arrays will start from key 1.

Alex JL
Very helpful, but I needed to know *where* I could find this out so I can look there next time.
George Edison
http://www.tutorialspoint.com/perl/perl_arrays.htm has a good rundown on array variables.
Alex JL
This tutorial sucks. The terminology and typography is wrong. It does not teach robust programming practices.
daxim
re: that tutorial - any site referring to 'PERL' (instead of 'Perl') should be approached with caution - it's likely to be old and/or written by someone outside the main Perl community, neither necessarily a bad thing of course but possibly not advocating current best practices.as for finding these variables : "perl special variables" on Google returns the perldoc page (2nd result for me just now) if you don't have access to 'perldoc perlvar' (or forget!)
plusplus
Don't blame me, I was just pointing to somewhere that explained $[. The question has long since been resolved.
Alex JL
+8  A: 

If you have a recent version of perldoc installed you could run it with the -v option.

perldoc -v '$[' http://perldoc.perl.org/perlvar.html#%24%5b

The index of the first element in an array, and of the first character in a substring. Default is 0, but
you could theoretically set it to 1 to make Perl behave more like awk (or Fortran) when subscripting and when evaluating the index() and substr() functions. (Mnemonic: [ begins subscripts.)

As of release 5 of Perl, assignment to $[ is treated as a compiler directive, and cannot influence the behavior of any other file. (That's why you can only assign compile-time constants to it.) Its use is highly discouraged.

Note that, unlike other compile-time directives (such as strict), assignment to $[ can be seen from outer lexical scopes in the same file. However, you can use local() on it to strictly bind its value to a lexical block.

This variable is deprecated in Perl version 5.12


The main reason you would find the $[ variable, would be if someone used a2p to transform an Awk script to a Perl script.

For aesthetic reasons you may wish to change the array base $[ from 1 back to perl's default of 0, but remember to change all array subscripts AND all substr() and index() operations to match.

Brad Gilbert
+8  A: 

Everyone above has covered it, but I'd like to add that Perldoc is available nicely formatted online at http://perldoc.perl.org/.

Details on all the special variable can be found at http://perldoc.perl.org/perlvar.html.

What are you using it for?

HerbN
The documentation, that variable, or Perl?
George Edison
I was thinking the variable. As the documentation notes it is deprecated in Perl 5.12 so finding an alternate way to achieve what you are doing would be well advised.
HerbN