1arybase(3pm)           Perl Programmers Reference Guide           arybase(3pm)
2
3
4

NAME

6       arybase - Set indexing base via $[
7

SYNOPSIS

9           $[ = 1;
10
11           @a = qw(Sun Mon Tue Wed Thu Fri Sat);
12           print $a[3], "\n";  # prints Tue
13

DESCRIPTION

15       This module implements Perl's $[ variable.  You should not use it
16       directly.
17
18       Assigning to $[ has the compile-time effect of making the assigned
19       value, converted to an integer, the index of the first element in an
20       array and the first character in a substring, within the enclosing
21       lexical scope.
22
23       It can be written with or without "local":
24
25           $[ = 1;
26           local $[ = 1;
27
28       It only works if the assignment can be detected at compile time and the
29       value assigned is constant.
30
31       It affects the following operations:
32
33           $array[$element]
34           @array[@slice]
35           $#array
36           (list())[$slice]
37           splice @array, $index, ...
38           each @array
39           keys @array
40
41           index $string, $substring  # return value is affected
42           pos $string
43           substr $string, $offset, ...
44
45       As with the default base of 0, negative bases count from the end of the
46       array or string, starting with -1.  If $[ is a positive integer,
47       indices from "$[-1" to 0 also count from the end.  If $[ is negative
48       (why would you do that, though?), indices from $[ to 0 count from the
49       beginning of the string, but indices below $[ count from the end of the
50       string as though the base were 0.
51
52       Prior to Perl 5.16, indices from 0 to "$[-1" inclusive, for positive
53       values of $[, behaved differently for different operations; negative
54       indices equal to or greater than a negative $[ likewise behaved
55       inconsistently.
56

HISTORY

58       Before Perl 5, $[ was a global variable that affected all array indices
59       and string offsets.
60
61       Starting with Perl 5, it became a file-scoped compile-time directive,
62       which could be made lexically-scoped with "local".  "File-scoped" means
63       that the $[ assignment could leak out of the block in which occurred:
64
65           {
66               $[ = 1;
67               # ... array base is 1 here ...
68           }
69           # ... still 1, but not in other files ...
70
71       In Perl 5.10, it became strictly lexical.  The file-scoped behaviour
72       was removed (perhaps inadvertently, but what's done is done).
73
74       In Perl 5.16, the implementation was moved into this module, and out of
75       the Perl core.  The erratic behaviour that occurred with indices
76       between -1 and $[ was made consistent between operations, and, for
77       negative bases, indices from $[ to -1 inclusive were made consistent
78       between operations.
79

BUGS

81       Error messages that mention array indices use the 0-based index.
82
83       "keys $arrayref" and "each $arrayref" do not respect the current value
84       of $[.
85

SEE ALSO

87       "$[" in perlvar, Array::Base and String::Base.
88
89
90
91perl v5.28.2                      2018-11-01                      arybase(3pm)
Impressum