line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
=head1 NAME |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
Array::Base - array index offseting |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 SYNOPSIS |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
use Array::Base +1; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
no Array::Base; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 DESCRIPTION |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
This module implements automatic offsetting of array indices. In normal |
14
|
|
|
|
|
|
|
Perl, the first element of an array has index 0, the second element has |
15
|
|
|
|
|
|
|
index 1, and so on. This module allows array indexes to start at some |
16
|
|
|
|
|
|
|
other value. Most commonly it is used to give the first element of an |
17
|
|
|
|
|
|
|
array the index 1 (and the second 2, and so on), to imitate the indexing |
18
|
|
|
|
|
|
|
behaviour of FORTRAN and many other languages. It is usually considered |
19
|
|
|
|
|
|
|
poor style to do this. |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
The array index offset is controlled at compile time, in a |
22
|
|
|
|
|
|
|
lexically-scoped manner. Each block of code, therefore, is subject to |
23
|
|
|
|
|
|
|
a fixed offset. It is expected that the affected code is written with |
24
|
|
|
|
|
|
|
knowledge of what that offset is. |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=head2 Using an array index offset |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
An array index offset is set up by a C |
29
|
|
|
|
|
|
|
the desired offset specified as an argument. Beware that a bare, unsigned |
30
|
|
|
|
|
|
|
number in that argument position, such as "C |
31
|
|
|
|
|
|
|
be interpreted as a version number to require of C. It is |
32
|
|
|
|
|
|
|
therefore necessary to give the offset a leading sign, or parenthesise |
33
|
|
|
|
|
|
|
it, or otherwise decorate it. The offset may be any integer (positive, |
34
|
|
|
|
|
|
|
zero, or negative) within the range of Perl's integer arithmetic. |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
An array index offset declaration is in effect from immediately after the |
37
|
|
|
|
|
|
|
C |
38
|
|
|
|
|
|
|
by another array index offset declaration. A declared offset always |
39
|
|
|
|
|
|
|
replaces the previous offset: they do not add. "C" is |
40
|
|
|
|
|
|
|
equivalent to "C |
41
|
|
|
|
|
|
|
with zero offset. |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
A declared array index offset influences these types of operation: |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=over |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=item * |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
array indexing (C<$a[3]>) |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=item * |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
array slicing (C<@a[3..5]>) |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=item * |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
list indexing/slicing (C) |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=item * |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
array splicing (C) |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=item * |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
array last index (C<$#a>) |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=item * |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
array keys (C) (Perl 5.11 and later) |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=item * |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
array each (C) (Perl 5.11 and later) |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=back |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
Only forwards indexing, relative to the start of the array, is supported. |
78
|
|
|
|
|
|
|
End-relative indexing, normally done using negative index values, is |
79
|
|
|
|
|
|
|
not supported when an index offset is in effect. Use of an index that |
80
|
|
|
|
|
|
|
is numerically less than the index offset will have unpredictable results. |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=head2 Differences from C<$[> |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
This module is a replacement for the historical L|perlvar/$[> |
85
|
|
|
|
|
|
|
variable. In early Perl that variable was a runtime global, affecting all |
86
|
|
|
|
|
|
|
array and string indexing in the program. In Perl 5, assignment to C<$[> |
87
|
|
|
|
|
|
|
acts as a lexically-scoped pragma. C<$[> is deprecated. The original |
88
|
|
|
|
|
|
|
C<$[> was removed in Perl 5.15.3, and later replaced in Perl 5.15.5 by |
89
|
|
|
|
|
|
|
an automatically-loaded L module. This module reimplements |
90
|
|
|
|
|
|
|
the index offset feature without any specific support from the core. |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
Unlike C<$[>, this module does not affect indexing into strings. |
93
|
|
|
|
|
|
|
This module is concerned only with arrays. To influence string indexing, |
94
|
|
|
|
|
|
|
see L. |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
This module does not show the offset value in C<$[> or any other |
97
|
|
|
|
|
|
|
accessible variable. With the array offset being lexically scoped, |
98
|
|
|
|
|
|
|
there should be no need to write code to handle a variable offset. |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
C<$[> has some predictable, but somewhat strange, behaviour for indexes |
101
|
|
|
|
|
|
|
less than the offset. The behaviour differs slightly between slicing |
102
|
|
|
|
|
|
|
and scalar indexing. This module does not attempt to replicate it, |
103
|
|
|
|
|
|
|
and does not support end-relative indexing at all. |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
The last-index operator (C<$#a>), as implemented by the Perl core, |
106
|
|
|
|
|
|
|
generates a magical scalar which is linked to the underlying array. |
107
|
|
|
|
|
|
|
The numerical value of the scalar varies if the length of the array |
108
|
|
|
|
|
|
|
is changed, and code with different C<$[> settings will see accordingly |
109
|
|
|
|
|
|
|
different values. The scalar can also be written to, to change the length |
110
|
|
|
|
|
|
|
of the array, and again the interpretation of the value written varies |
111
|
|
|
|
|
|
|
according to the C<$[> setting of the code that is doing the writing. |
112
|
|
|
|
|
|
|
This module does not replicate any of that behaviour. With an array |
113
|
|
|
|
|
|
|
index offset from this module in effect, C<$#a> evaluates to an ordinary |
114
|
|
|
|
|
|
|
rvalue scalar, giving the last index of the array as it was at the time |
115
|
|
|
|
|
|
|
the operator was evaluated, according to the array index offset in effect |
116
|
|
|
|
|
|
|
where the operator appears. |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=cut |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
package Array::Base; |
121
|
|
|
|
|
|
|
|
122
|
8
|
|
|
8
|
|
223388
|
{ use 5.008001; } |
|
8
|
|
|
|
|
32
|
|
|
8
|
|
|
|
|
367
|
|
123
|
8
|
|
|
8
|
|
16450
|
use Lexical::SealRequireHints 0.006; |
|
8
|
|
|
|
|
7373
|
|
|
8
|
|
|
|
|
52
|
|
124
|
8
|
|
|
8
|
|
346
|
use warnings; |
|
8
|
|
|
|
|
30
|
|
|
8
|
|
|
|
|
255
|
|
125
|
8
|
|
|
8
|
|
98
|
use strict; |
|
8
|
|
|
|
|
15
|
|
|
8
|
|
|
|
|
743
|
|
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
our $VERSION = "0.005"; |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
require XSLoader; |
130
|
|
|
|
|
|
|
XSLoader::load(__PACKAGE__, $VERSION); |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=head1 PACKAGE METHODS |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
These methods are meant to be invoked on the C package. |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=over |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=item Array::Base->import(BASE) |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
Sets up an array index offset of I, in the lexical environment |
141
|
|
|
|
|
|
|
that is currently compiling. |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=item Array::Base->unimport |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
Clears the array index offset, in the lexical environment that is |
146
|
|
|
|
|
|
|
currently compiling. |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=back |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=head1 BUGS |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
L will generate incorrect source when deparsing code that |
153
|
|
|
|
|
|
|
uses an array index offset. It will include both the pragma to set up |
154
|
|
|
|
|
|
|
the offset and the munged form of the affected operators. Either the |
155
|
|
|
|
|
|
|
pragma or the munging is required to get the index offset effect; using |
156
|
|
|
|
|
|
|
both will double the offset. Also, the code generated for an array each |
157
|
|
|
|
|
|
|
(C) operation involves a custom operator, which L |
158
|
|
|
|
|
|
|
can't understand, so the source it emits in that case is completely wrong. |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
The additional operators generated by this module cause spurious warnings |
161
|
|
|
|
|
|
|
if some of the affected array operations are used in void context. |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
Prior to Perl 5.9.3, the lexical state of array index offset does not |
164
|
|
|
|
|
|
|
propagate into string eval. |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=head1 SEE ALSO |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
L, |
169
|
|
|
|
|
|
|
L, |
170
|
|
|
|
|
|
|
L |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=head1 AUTHOR |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
Andrew Main (Zefram) |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=head1 COPYRIGHT |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
Copyright (C) 2009, 2010, 2011, 2012 |
179
|
|
|
|
|
|
|
Andrew Main (Zefram) |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=head1 LICENSE |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
This module is free software; you can redistribute it and/or modify it |
184
|
|
|
|
|
|
|
under the same terms as Perl itself. |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=cut |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
1; |