Start anew
[msysgit.git] / lib / perl5 / 5.6.1 / Tie / Array.pm
blobf4c6193596505142a04f80f984d0e7edc96844cc
1 package Tie::Array;
3 use 5.005_64;
4 use strict;
5 use Carp;
6 our $VERSION = '1.01';
8 # Pod documentation after __END__ below.
10 sub DESTROY { }
11 sub EXTEND { }
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) }
17 sub PUSH
19 my $obj = shift;
20 my $i = $obj->FETCHSIZE;
21 $obj->STORE($i++, shift) while (@_);
24 sub POP
26 my $obj = shift;
27 my $newsize = $obj->FETCHSIZE - 1;
28 my $val;
29 if ($newsize >= 0)
31 $val = $obj->FETCH($newsize);
32 $obj->STORESIZE($newsize);
34 $val;
37 sub SPLICE {
38 my $obj = shift;
39 my $sz = $obj->FETCHSIZE;
40 my $off = (@_) ? shift : 0;
41 $off += $sz if ($off < 0);
42 my $len = (@_) ? shift : $sz - $off;
43 $len += $sz - $off if $len < 0;
44 my @result;
45 for (my $i = 0; $i < $len; $i++) {
46 push(@result,$obj->FETCH($off+$i));
48 $off = $sz if $off > $sz;
49 $len -= $off + $len - $sz if $off + $len > $sz;
50 if (@_ > $len) {
51 # Move items up to make room
52 my $d = @_ - $len;
53 my $e = $off+$len;
54 $obj->EXTEND($sz+$d);
55 for (my $i=$sz-1; $i >= $e; $i--) {
56 my $val = $obj->FETCH($i);
57 $obj->STORE($i+$d,$val);
60 elsif (@_ < $len) {
61 # Move items down to close the gap
62 my $d = $len - @_;
63 my $e = $off+$len;
64 for (my $i=$off+$len; $i < $sz; $i++) {
65 my $val = $obj->FETCH($i);
66 $obj->STORE($i-$d,$val);
68 $obj->STORESIZE($sz-$d);
70 for (my $i=0; $i < @_; $i++) {
71 $obj->STORE($off+$i,$_[$i]);
73 return @result;
76 sub EXISTS {
77 my $pkg = ref $_[0];
78 croak "$pkg dosn't define an EXISTS method";
81 sub DELETE {
82 my $pkg = ref $_[0];
83 croak "$pkg dosn't define a DELETE method";
86 package Tie::StdArray;
87 use vars qw(@ISA);
88 @ISA = 'Tie::Array';
90 sub TIEARRAY { bless [], $_[0] }
91 sub FETCHSIZE { scalar @{$_[0]} }
92 sub STORESIZE { $#{$_[0]} = $_[1]-1 }
93 sub STORE { $_[0]->[$_[1]] = $_[2] }
94 sub FETCH { $_[0]->[$_[1]] }
95 sub CLEAR { @{$_[0]} = () }
96 sub POP { pop(@{$_[0]}) }
97 sub PUSH { my $o = shift; push(@$o,@_) }
98 sub SHIFT { shift(@{$_[0]}) }
99 sub UNSHIFT { my $o = shift; unshift(@$o,@_) }
100 sub EXISTS { exists $_[0]->[$_[1]] }
101 sub DELETE { delete $_[0]->[$_[1]] }
103 sub SPLICE
105 my $ob = shift;
106 my $sz = $ob->FETCHSIZE;
107 my $off = @_ ? shift : 0;
108 $off += $sz if $off < 0;
109 my $len = @_ ? shift : $sz-$off;
110 return splice(@$ob,$off,$len,@_);
115 __END__
117 =head1 NAME
119 Tie::Array - base class for tied arrays
121 =head1 SYNOPSIS
123 package NewArray;
124 use Tie::Array;
125 @ISA = ('Tie::Array');
127 # mandatory methods
128 sub TIEARRAY { ... }
129 sub FETCH { ... }
130 sub FETCHSIZE { ... }
132 sub STORE { ... } # mandatory if elements writeable
133 sub STORESIZE { ... } # mandatory if elements can be added/deleted
134 sub EXISTS { ... } # mandatory if exists() expected to work
135 sub DELETE { ... } # mandatory if delete() expected to work
137 # optional methods - for efficiency
138 sub CLEAR { ... }
139 sub PUSH { ... }
140 sub POP { ... }
141 sub SHIFT { ... }
142 sub UNSHIFT { ... }
143 sub SPLICE { ... }
144 sub EXTEND { ... }
145 sub DESTROY { ... }
147 package NewStdArray;
148 use Tie::Array;
150 @ISA = ('Tie::StdArray');
152 # all methods provided by default
154 package main;
156 $object = tie @somearray,Tie::NewArray;
157 $object = tie @somearray,Tie::StdArray;
158 $object = tie @somearray,Tie::NewStdArray;
162 =head1 DESCRIPTION
164 This module provides methods for array-tying classes. See
165 L<perltie> for a list of the functions required in order to tie an array
166 to a package. The basic B<Tie::Array> package provides stub C<DESTROY>,
167 and C<EXTEND> methods that do nothing, stub C<DELETE> and C<EXISTS>
168 methods that croak() if the delete() or exists() builtins are ever called
169 on the tied array, and implementations of C<PUSH>, C<POP>, C<SHIFT>,
170 C<UNSHIFT>, C<SPLICE> and C<CLEAR> in terms of basic C<FETCH>, C<STORE>,
171 C<FETCHSIZE>, C<STORESIZE>.
173 The B<Tie::StdArray> package provides efficient methods required for tied arrays
174 which are implemented as blessed references to an "inner" perl array.
175 It inherits from B<Tie::Array>, and should cause tied arrays to behave exactly
176 like standard arrays, allowing for selective overloading of methods.
178 For developers wishing to write their own tied arrays, the required methods
179 are briefly defined below. See the L<perltie> section for more detailed
180 descriptive, as well as example code:
182 =over
184 =item TIEARRAY classname, LIST
186 The class method is invoked by the command C<tie @array, classname>. Associates
187 an array instance with the specified class. C<LIST> would represent
188 additional arguments (along the lines of L<AnyDBM_File> and compatriots) needed
189 to complete the association. The method should return an object of a class which
190 provides the methods below.
192 =item STORE this, index, value
194 Store datum I<value> into I<index> for the tied array associated with
195 object I<this>. If this makes the array larger then
196 class's mapping of C<undef> should be returned for new positions.
198 =item FETCH this, index
200 Retrieve the datum in I<index> for the tied array associated with
201 object I<this>.
203 =item FETCHSIZE this
205 Returns the total number of items in the tied array associated with
206 object I<this>. (Equivalent to C<scalar(@array)>).
208 =item STORESIZE this, count
210 Sets the total number of items in the tied array associated with
211 object I<this> to be I<count>. If this makes the array larger then
212 class's mapping of C<undef> should be returned for new positions.
213 If the array becomes smaller then entries beyond count should be
214 deleted.
216 =item EXTEND this, count
218 Informative call that array is likely to grow to have I<count> entries.
219 Can be used to optimize allocation. This method need do nothing.
221 =item EXISTS this, key
223 Verify that the element at index I<key> exists in the tied array I<this>.
225 The B<Tie::Array> implementation is a stub that simply croaks.
227 =item DELETE this, key
229 Delete the element at index I<key> from the tied array I<this>.
231 The B<Tie::Array> implementation is a stub that simply croaks.
233 =item CLEAR this
235 Clear (remove, delete, ...) all values from the tied array associated with
236 object I<this>.
238 =item DESTROY this
240 Normal object destructor method.
242 =item PUSH this, LIST
244 Append elements of LIST to the array.
246 =item POP this
248 Remove last element of the array and return it.
250 =item SHIFT this
252 Remove the first element of the array (shifting other elements down)
253 and return it.
255 =item UNSHIFT this, LIST
257 Insert LIST elements at the beginning of the array, moving existing elements
258 up to make room.
260 =item SPLICE this, offset, length, LIST
262 Perform the equivalent of C<splice> on the array.
264 I<offset> is optional and defaults to zero, negative values count back
265 from the end of the array.
267 I<length> is optional and defaults to rest of the array.
269 I<LIST> may be empty.
271 Returns a list of the original I<length> elements at I<offset>.
273 =back
275 =head1 CAVEATS
277 There is no support at present for tied @ISA. There is a potential conflict
278 between magic entries needed to notice setting of @ISA, and those needed to
279 implement 'tie'.
281 Very little consideration has been given to the behaviour of tied arrays
282 when C<$[> is not default value of zero.
284 =head1 AUTHOR
286 Nick Ing-Simmons E<lt>nik@tiuk.ti.comE<gt>
288 =cut