8 # Pod documentation after __END__ below.
12 sub UNSHIFT { scalar shift->SPLICE(0,0,@_) }
13 sub SHIFT { shift->SPLICE(0,1) }
14 #sub SHIFT { (shift->SPLICE(0,1))[0] }
15 sub CLEAR { shift->STORESIZE(0) }
20 my $i = $obj->FETCHSIZE;
21 $obj->STORE($i++, shift) while (@_);
27 my $newsize = $obj->FETCHSIZE - 1;
31 $val = $obj->FETCH($newsize);
32 $obj->STORESIZE($newsize);
40 my $sz = $obj->FETCHSIZE;
41 my $off = (@_) ? shift : 0;
42 $off += $sz if ($off < 0);
43 my $len = (@_) ? shift : $sz - $off;
45 for (my $i = 0; $i < $len; $i++)
47 push(@result,$obj->FETCH($off+$i));
51 # Move items up to make room
55 for (my $i=$sz-1; $i >= $e; $i--)
57 my $val = $obj->FETCH($i);
58 $obj->STORE($i+$d,$val);
63 # Move items down to close the gap
66 for (my $i=$off+$len; $i < $sz; $i++)
68 my $val = $obj->FETCH($i);
69 $obj->STORE($i-$d,$val);
71 $obj->STORESIZE($sz-$d);
73 for (my $i=0; $i < @_; $i++)
75 $obj->STORE($off+$i,$_[$i]);
82 croak "$pkg dosn't define an EXISTS method";
87 croak "$pkg dosn't define a DELETE method";
90 package Tie::StdArray;
94 sub TIEARRAY { bless [], $_[0] }
95 sub FETCHSIZE { scalar @{$_[0]} }
96 sub STORESIZE { $#{$_[0]} = $_[1]-1 }
97 sub STORE { $_[0]->[$_[1]] = $_[2] }
98 sub FETCH { $_[0]->[$_[1]] }
99 sub CLEAR { @{$_[0]} = () }
100 sub POP { pop(@{$_[0]}) }
101 sub PUSH { my $o = shift; push(@$o,@_) }
102 sub SHIFT { shift(@{$_[0]}) }
103 sub UNSHIFT { my $o = shift; unshift(@$o,@_) }
104 sub EXISTS { exists $_[0]->[$_[1]] }
105 sub DELETE { delete $_[0]->[$_[1]] }
110 my $sz = $ob->FETCHSIZE;
111 my $off = @_ ? shift : 0;
112 $off += $sz if $off < 0;
113 my $len = @_ ? shift : $sz-$off;
114 return splice(@$ob,$off,$len,@_);
123 Tie::Array - base class for tied arrays
129 @ISA = ('Tie::Array');
134 sub FETCHSIZE { ... }
136 sub STORE { ... } # mandatory if elements writeable
137 sub STORESIZE { ... } # mandatory if elements can be added/deleted
138 sub EXISTS { ... } # mandatory if exists() expected to work
139 sub DELETE { ... } # mandatory if delete() expected to work
141 # optional methods - for efficiency
154 @ISA = ('Tie::StdArray');
156 # all methods provided by default
160 $object = tie @somearray,Tie::NewArray;
161 $object = tie @somearray,Tie::StdArray;
162 $object = tie @somearray,Tie::NewStdArray;
168 This module provides methods for array-tying classes. See
169 L<perltie> for a list of the functions required in order to tie an array
170 to a package. The basic B<Tie::Array> package provides stub C<DESTROY>,
171 and C<EXTEND> methods that do nothing, stub C<DELETE> and C<EXISTS>
172 methods that croak() if the delete() or exists() builtins are ever called
173 on the tied array, and implementations of C<PUSH>, C<POP>, C<SHIFT>,
174 C<UNSHIFT>, C<SPLICE> and C<CLEAR> in terms of basic C<FETCH>, C<STORE>,
175 C<FETCHSIZE>, C<STORESIZE>.
177 The B<Tie::StdArray> package provides efficient methods required for tied arrays
178 which are implemented as blessed references to an "inner" perl array.
179 It inherits from B<Tie::Array>, and should cause tied arrays to behave exactly
180 like standard arrays, allowing for selective overloading of methods.
182 For developers wishing to write their own tied arrays, the required methods
183 are briefly defined below. See the L<perltie> section for more detailed
184 descriptive, as well as example code:
188 =item TIEARRAY classname, LIST
190 The class method is invoked by the command C<tie @array, classname>. Associates
191 an array instance with the specified class. C<LIST> would represent
192 additional arguments (along the lines of L<AnyDBM_File> and compatriots) needed
193 to complete the association. The method should return an object of a class which
194 provides the methods below.
196 =item STORE this, index, value
198 Store datum I<value> into I<index> for the tied array associated with
199 object I<this>. If this makes the array larger then
200 class's mapping of C<undef> should be returned for new positions.
202 =item FETCH this, index
204 Retrieve the datum in I<index> for the tied array associated with
209 Returns the total number of items in the tied array associated with
210 object I<this>. (Equivalent to C<scalar(@array)>).
212 =item STORESIZE this, count
214 Sets the total number of items in the tied array associated with
215 object I<this> to be I<count>. If this makes the array larger then
216 class's mapping of C<undef> should be returned for new positions.
217 If the array becomes smaller then entries beyond count should be
220 =item EXTEND this, count
222 Informative call that array is likely to grow to have I<count> entries.
223 Can be used to optimize allocation. This method need do nothing.
225 =item EXISTS this, key
227 Verify that the element at index I<key> exists in the tied array I<this>.
229 The B<Tie::Array> implementation is a stub that simply croaks.
231 =item DELETE this, key
233 Delete the element at index I<key> from the tied array I<this>.
235 The B<Tie::Array> implementation is a stub that simply croaks.
239 Clear (remove, delete, ...) all values from the tied array associated with
244 Normal object destructor method.
246 =item PUSH this, LIST
248 Append elements of LIST to the array.
252 Remove last element of the array and return it.
256 Remove the first element of the array (shifting other elements down)
259 =item UNSHIFT this, LIST
261 Insert LIST elements at the beginning of the array, moving existing elements
264 =item SPLICE this, offset, length, LIST
266 Perform the equivalent of C<splice> on the array.
268 I<offset> is optional and defaults to zero, negative values count back
269 from the end of the array.
271 I<length> is optional and defaults to rest of the array.
273 I<LIST> may be empty.
275 Returns a list of the original I<length> elements at I<offset>.
281 There is no support at present for tied @ISA. There is a potential conflict
282 between magic entries needed to notice setting of @ISA, and those needed to
285 Very little consideration has been given to the behaviour of tied arrays
286 when C<$[> is not default value of zero.
290 Nick Ing-Simmons E<lt>nik@tiuk.ti.comE<gt>