File Coverage

blib/lib/Array/Base.pm
Criterion Covered Total %
statement 11 11 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 15 15 100.0


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 directive, with
29             the desired offset specified as an argument. Beware that a bare, unsigned
30             number in that argument position, such as "C", will
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 line, until the end of the enclosing block or until overridden
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": it returns to the Perlish state
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             array pair slicing (C<%a[3..5]>)
58              
59             =item *
60              
61             list indexing/slicing (C)
62              
63             =item *
64              
65             array splicing (C)
66              
67             =item *
68              
69             array last index (C<$#a>)
70              
71             =item *
72              
73             array keys (C) (Perl 5.11 and later)
74              
75             =item *
76              
77             array each (C) (Perl 5.11 and later)
78              
79             =back
80              
81             Only forwards indexing, relative to the start of the array, is supported.
82             End-relative indexing, normally done using negative index values, is
83             not supported when an index offset is in effect. Use of an index that
84             is numerically less than the index offset will have unpredictable results.
85              
86             =head2 Differences from C<$[>
87              
88             This module is a replacement for the historical L|perlvar/$[>
89             variable. In early Perl that variable was a runtime global, affecting all
90             array and string indexing in the program. In Perl 5, assignment to C<$[>
91             acts as a lexically-scoped pragma. C<$[> is deprecated. The original
92             C<$[> was removed in Perl 5.15.3, and later replaced in Perl 5.15.5 by
93             an automatically-loaded L module. This module reimplements
94             the index offset feature without any specific support from the core.
95              
96             Unlike C<$[>, this module does not affect indexing into strings.
97             This module is concerned only with arrays. To influence string indexing,
98             see L.
99              
100             This module does not show the offset value in C<$[> or any other
101             accessible variable. With the array offset being lexically scoped,
102             there should be no need to write code to handle a variable offset.
103              
104             C<$[> has some predictable, but somewhat strange, behaviour for indexes
105             less than the offset. The behaviour differs slightly between slicing
106             and scalar indexing. This module does not attempt to replicate it,
107             and does not support end-relative indexing at all.
108              
109             The last-index operator (C<$#a>), as implemented by the Perl core,
110             generates a magical scalar which is linked to the underlying array.
111             The numerical value of the scalar varies if the length of the array
112             is changed, and code with different C<$[> settings will see accordingly
113             different values. The scalar can also be written to, to change the length
114             of the array, and again the interpretation of the value written varies
115             according to the C<$[> setting of the code that is doing the writing.
116             This module does not replicate any of that behaviour. With an array
117             index offset from this module in effect, C<$#a> evaluates to an ordinary
118             rvalue scalar, giving the last index of the array as it was at the time
119             the operator was evaluated, according to the array index offset in effect
120             where the operator appears.
121              
122             =cut
123              
124             package Array::Base;
125              
126 9     9   463637 { use 5.008001; }
  9         30  
127 9     9   2802 use Lexical::SealRequireHints 0.008;
  9         5646  
  9         45  
128 9     9   238 use warnings;
  9         20  
  9         219  
129 9     9   40 use strict;
  9         17  
  9         469  
130              
131             our $VERSION = "0.006";
132              
133             require XSLoader;
134             XSLoader::load(__PACKAGE__, $VERSION);
135              
136             =head1 PACKAGE METHODS
137              
138             These methods are meant to be invoked on the C package.
139              
140             =over
141              
142             =item Array::Base->import(BASE)
143              
144             Sets up an array index offset of I, in the lexical environment
145             that is currently compiling.
146              
147             =item Array::Base->unimport
148              
149             Clears the array index offset, in the lexical environment that is
150             currently compiling.
151              
152             =back
153              
154             =head1 BUGS
155              
156             L will generate incorrect source when deparsing code that
157             uses an array index offset. It will include both the pragma to set up
158             the offset and the munged form of the affected operators. Either the
159             pragma or the munging is required to get the index offset effect; using
160             both will double the offset. Also, the code generated for an array each
161             (C) operation involves a custom operator, which L
162             can't understand, so the source it emits in that case is completely wrong.
163              
164             The additional operators generated by this module cause spurious warnings
165             if some of the affected array operations are used in void context.
166              
167             Prior to Perl 5.9.3, the lexical state of array index offset does not
168             propagate into string eval.
169              
170             =head1 SEE ALSO
171              
172             L,
173             L,
174             L
175              
176             =head1 AUTHOR
177              
178             Andrew Main (Zefram)
179              
180             =head1 COPYRIGHT
181              
182             Copyright (C) 2009, 2010, 2011, 2012, 2017
183             Andrew Main (Zefram)
184              
185             =head1 LICENSE
186              
187             This module is free software; you can redistribute it and/or modify it
188             under the same terms as Perl itself.
189              
190             =cut
191              
192             1;