8 # Pod documentation after __END__ below.
12 sub UNSHIFT { scalar shift->SPLICE(0,0,@_) }
13 sub SHIFT { shift->SPLICE(0,1) }
14 sub CLEAR { shift->STORESIZE(0) }
19 my $i = $obj->FETCHSIZE;
20 $obj->STORE($i++, shift) while (@_);
26 my $newsize = $obj->FETCHSIZE - 1;
30 $val = $obj->FETCH($newsize);
31 $obj->STORESIZE($newsize);
38 my $sz = $obj->FETCHSIZE;
39 my $off = (@_) ? shift : 0;
40 $off += $sz if ($off < 0);
41 my $len = (@_) ? shift : $sz - $off;
42 $len += $sz - $off if $len < 0;
44 for (my $i = 0; $i < $len; $i++) {
45 push(@result,$obj->FETCH($off+$i));
47 $off = $sz if $off > $sz;
48 $len -= $off + $len - $sz if $off + $len > $sz;
50 # Move items up to make room
54 for (my $i=$sz-1; $i >= $e; $i--) {
55 my $val = $obj->FETCH($i);
56 $obj->STORE($i+$d,$val);
60 # Move items down to close the gap
63 for (my $i=$off+$len; $i < $sz; $i++) {
64 my $val = $obj->FETCH($i);
65 $obj->STORE($i-$d,$val);
67 $obj->STORESIZE($sz-$d);
69 for (my $i=0; $i < @_; $i++) {
70 $obj->STORE($off+$i,$_[$i]);
72 return wantarray ? @result : pop @result;
77 croak "$pkg doesn't define an EXISTS method";
82 croak "$pkg doesn't define a DELETE method";
85 package Tie::StdArray;
86 our @ISA = 'Tie::Array';
88 sub TIEARRAY { bless [], $_[0] }
89 sub FETCHSIZE { scalar @{$_[0]} }
90 sub STORESIZE { $#{$_[0]} = $_[1]-1 }
91 sub STORE { $_[0]->[$_[1]] = $_[2] }
92 sub FETCH { $_[0]->[$_[1]] }
93 sub CLEAR { @{$_[0]} = () }
94 sub POP { pop(@{$_[0]}) }
95 sub PUSH { my $o = shift; push(@$o,@_) }
96 sub SHIFT { shift(@{$_[0]}) }
97 sub UNSHIFT { my $o = shift; unshift(@$o,@_) }
98 sub EXISTS { exists $_[0]->[$_[1]] }
99 sub DELETE { delete $_[0]->[$_[1]] }
104 my $sz = $ob->FETCHSIZE;
105 my $off = @_ ? shift : 0;
106 $off += $sz if $off < 0;
107 my $len = @_ ? shift : $sz-$off;
108 return splice(@$ob,$off,$len,@_);
117 Tie::Array - base class for tied arrays
121 package Tie::NewArray;
123 @ISA = ('Tie::Array');
128 sub FETCHSIZE { ... }
130 sub STORE { ... } # mandatory if elements writeable
131 sub STORESIZE { ... } # mandatory if elements can be added/deleted
132 sub EXISTS { ... } # mandatory if exists() expected to work
133 sub DELETE { ... } # mandatory if delete() expected to work
135 # optional methods - for efficiency
145 package Tie::NewStdArray;
148 @ISA = ('Tie::StdArray');
150 # all methods provided by default
154 $object = tie @somearray,'Tie::NewArray';
155 $object = tie @somearray,'Tie::StdArray';
156 $object = tie @somearray,'Tie::NewStdArray';
162 This module provides methods for array-tying classes. See
163 L<perltie> for a list of the functions required in order to tie an array
164 to a package. The basic B<Tie::Array> package provides stub C<DESTROY>,
165 and C<EXTEND> methods that do nothing, stub C<DELETE> and C<EXISTS>
166 methods that croak() if the delete() or exists() builtins are ever called
167 on the tied array, and implementations of C<PUSH>, C<POP>, C<SHIFT>,
168 C<UNSHIFT>, C<SPLICE> and C<CLEAR> in terms of basic C<FETCH>, C<STORE>,
169 C<FETCHSIZE>, C<STORESIZE>.
171 The B<Tie::StdArray> package provides efficient methods required for tied arrays
172 which are implemented as blessed references to an "inner" perl array.
173 It inherits from B<Tie::Array>, and should cause tied arrays to behave exactly
174 like standard arrays, allowing for selective overloading of methods.
176 For developers wishing to write their own tied arrays, the required methods
177 are briefly defined below. See the L<perltie> section for more detailed
178 descriptive, as well as example code:
182 =item TIEARRAY classname, LIST
184 The class method is invoked by the command C<tie @array, classname>. Associates
185 an array instance with the specified class. C<LIST> would represent
186 additional arguments (along the lines of L<AnyDBM_File> and compatriots) needed
187 to complete the association. The method should return an object of a class which
188 provides the methods below.
190 =item STORE this, index, value
192 Store datum I<value> into I<index> for the tied array associated with
193 object I<this>. If this makes the array larger then
194 class's mapping of C<undef> should be returned for new positions.
196 =item FETCH this, index
198 Retrieve the datum in I<index> for the tied array associated with
203 Returns the total number of items in the tied array associated with
204 object I<this>. (Equivalent to C<scalar(@array)>).
206 =item STORESIZE this, count
208 Sets the total number of items in the tied array associated with
209 object I<this> to be I<count>. If this makes the array larger then
210 class's mapping of C<undef> should be returned for new positions.
211 If the array becomes smaller then entries beyond count should be
214 =item EXTEND this, count
216 Informative call that array is likely to grow to have I<count> entries.
217 Can be used to optimize allocation. This method need do nothing.
219 =item EXISTS this, key
221 Verify that the element at index I<key> exists in the tied array I<this>.
223 The B<Tie::Array> implementation is a stub that simply croaks.
225 =item DELETE this, key
227 Delete the element at index I<key> from the tied array I<this>.
229 The B<Tie::Array> implementation is a stub that simply croaks.
233 Clear (remove, delete, ...) all values from the tied array associated with
238 Normal object destructor method.
240 =item PUSH this, LIST
242 Append elements of LIST to the array.
246 Remove last element of the array and return it.
250 Remove the first element of the array (shifting other elements down)
253 =item UNSHIFT this, LIST
255 Insert LIST elements at the beginning of the array, moving existing elements
258 =item SPLICE this, offset, length, LIST
260 Perform the equivalent of C<splice> on the array.
262 I<offset> is optional and defaults to zero, negative values count back
263 from the end of the array.
265 I<length> is optional and defaults to rest of the array.
267 I<LIST> may be empty.
269 Returns a list of the original I<length> elements at I<offset>.
275 There is no support at present for tied @ISA. There is a potential conflict
276 between magic entries needed to notice setting of @ISA, and those needed to
281 Nick Ing-Simmons E<lt>nik@tiuk.ti.comE<gt>