7 # Pod documentation after __END__ below.
11 sub UNSHIFT { shift->SPLICE(0,0,@_) }
12 sub SHIFT { shift->SPLICE(0,1) }
13 sub CLEAR { shift->STORESIZE(0) }
18 my $i = $obj->FETCHSIZE;
19 $obj->STORE($i++, shift) while (@_);
25 my $newsize = $obj->FETCHSIZE - 1;
29 $val = $obj->FETCH($newsize);
30 $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;
43 for (my $i = 0; $i < $len; $i++)
45 push(@result,$obj->FETCH($off+$i));
49 # Move items up to make room
53 for (my $i=$sz-1; $i >= $e; $i--)
55 my $val = $obj->FETCH($i);
56 $obj->STORE($i+$d,$val);
61 # Move items down to close the gap
64 for (my $i=$off+$len; $i < $sz; $i++)
66 my $val = $obj->FETCH($i);
67 $obj->STORE($i-$d,$val);
69 $obj->STORESIZE($sz-$d);
71 for (my $i=0; $i < @_; $i++)
73 $obj->STORE($off+$i,$_[$i]);
80 croak "$pkg dosn't define an EXISTS method";
85 croak "$pkg dosn't define a DELETE method";
88 package Tie::StdArray;
92 sub TIEARRAY { bless [], $_[0] }
93 sub FETCHSIZE { scalar @{$_[0]} }
94 sub STORESIZE { $#{$_[0]} = $_[1]-1 }
95 sub STORE { $_[0]->[$_[1]] = $_[2] }
96 sub FETCH { $_[0]->[$_[1]] }
97 sub CLEAR { @{$_[0]} = () }
98 sub POP { pop(@{$_[0]}) }
99 sub PUSH { my $o = shift; push(@$o,@_) }
100 sub SHIFT { shift(@{$_[0]}) }
101 sub UNSHIFT { my $o = shift; unshift(@$o,@_) }
102 sub EXISTS { exists $_[0]->[$_[1]] }
103 sub DELETE { delete $_[0]->[$_[1]] }
108 my $sz = $ob->FETCHSIZE;
109 my $off = @_ ? shift : 0;
110 $off += $sz if $off < 0;
111 my $len = @_ ? shift : $sz-$off;
112 return splice(@$ob,$off,$len,@_);
121 Tie::Array - base class for tied arrays
127 @ISA = ('Tie::Array');
132 sub FETCHSIZE { ... }
134 sub STORE { ... } # mandatory if elements writeable
135 sub STORESIZE { ... } # mandatory if elements can be added/deleted
136 sub EXISTS { ... } # mandatory if exists() expected to work
137 sub DELETE { ... } # mandatory if delete() expected to work
139 # optional methods - for efficiency
152 @ISA = ('Tie::StdArray');
154 # all methods provided by default
158 $object = tie @somearray,Tie::NewArray;
159 $object = tie @somearray,Tie::StdArray;
160 $object = tie @somearray,Tie::NewStdArray;
166 This module provides methods for array-tying classes. See
167 L<perltie> for a list of the functions required in order to tie an array
168 to a package. The basic B<Tie::Array> package provides stub C<DESTROY>,
169 and C<EXTEND> methods that do nothing, stub C<DELETE> and C<EXISTS>
170 methods that croak() if the delete() or exists() builtins are ever called
171 on the tied array, and implementations of C<PUSH>, C<POP>, C<SHIFT>,
172 C<UNSHIFT>, C<SPLICE> and C<CLEAR> in terms of basic C<FETCH>, C<STORE>,
173 C<FETCHSIZE>, C<STORESIZE>.
175 The B<Tie::StdArray> package provides efficient methods required for tied arrays
176 which are implemented as blessed references to an "inner" perl array.
177 It inherits from B<Tie::Array>, and should cause tied arrays to behave exactly
178 like standard arrays, allowing for selective overloading of methods.
180 For developers wishing to write their own tied arrays, the required methods
181 are briefly defined below. See the L<perltie> section for more detailed
182 descriptive, as well as example code:
186 =item TIEARRAY classname, LIST
188 The class method is invoked by the command C<tie @array, classname>. Associates
189 an array instance with the specified class. C<LIST> would represent
190 additional arguments (along the lines of L<AnyDBM_File> and compatriots) needed
191 to complete the association. The method should return an object of a class which
192 provides the methods below.
194 =item STORE this, index, value
196 Store datum I<value> into I<index> for the tied array associated with
197 object I<this>. If this makes the array larger then
198 class's mapping of C<undef> should be returned for new positions.
200 =item FETCH this, index
202 Retrieve the datum in I<index> for the tied array associated with
207 Returns the total number of items in the tied array associated with
208 object I<this>. (Equivalent to C<scalar(@array)>).
210 =item STORESIZE this, count
212 Sets the total number of items in the tied array associated with
213 object I<this> to be I<count>. If this makes the array larger then
214 class's mapping of C<undef> should be returned for new positions.
215 If the array becomes smaller then entries beyond count should be
218 =item EXTEND this, count
220 Informative call that array is likely to grow to have I<count> entries.
221 Can be used to optimize allocation. This method need do nothing.
223 =item EXISTS this, key
225 Verify that the element at index I<key> exists in the tied array I<this>.
227 The B<Tie::Array> implementation is a stub that simply croaks.
229 =item DELETE this, key
231 Delete the element at index I<key> from the tied array I<this>.
233 The B<Tie::Array> implementation is a stub that simply croaks.
237 Clear (remove, delete, ...) all values from the tied array associated with
242 Normal object destructor method.
244 =item PUSH this, LIST
246 Append elements of LIST to the array.
250 Remove last element of the array and return it.
254 Remove the first element of the array (shifting other elements down)
257 =item UNSHIFT this, LIST
259 Insert LIST elements at the beginning of the array, moving existing elements
262 =item SPLICE this, offset, length, LIST
264 Perform the equivalent of C<splice> on the array.
266 I<offset> is optional and defaults to zero, negative values count back
267 from the end of the array.
269 I<length> is optional and defaults to rest of the array.
271 I<LIST> may be empty.
273 Returns a list of the original I<length> elements at I<offset>.
279 There is no support at present for tied @ISA. There is a potential conflict
280 between magic entries needed to notice setting of @ISA, and those needed to
283 Very little consideration has been given to the behaviour of tied arrays
284 when C<$[> is not default value of zero.
288 Nick Ing-Simmons E<lt>nik@tiuk.ti.comE<gt>