documentation patch for 5.004_57
[p5sagit/p5-mst-13.2.git] / lib / Tie / Array.pm
CommitLineData
a60c0954 1package Tie::Array;
2use vars qw($VERSION);
3use strict;
4$VERSION = '1.00';
ab3c8535 5
a60c0954 6# Pod documentation after __END__ below.
7
8sub DESTROY { }
9sub EXTEND { }
10sub UNSHIFT { shift->SPLICE(0,0,@_) }
11sub SHIFT { shift->SPLICE(0,1) }
12sub CLEAR { shift->STORESIZE(0) }
13
14sub PUSH
15{
16 my $obj = shift;
17 my $i = $obj->FETCHSIZE;
18 $obj->STORE($i++, shift) while (@_);
19}
20
21sub POP
22{
23 my $obj = shift;
24 my $newsize = $obj->FETCHSIZE - 1;
25 my $val;
26 if ($newsize >= 0)
27 {
28 $val = $obj->FETCH($newsize);
29 $obj->SETSIZE($newsize);
30 }
31 $val;
32}
33
34sub SPLICE
35{
36 my $obj = shift;
37 my $sz = $obj->FETCHSIZE;
38 my $off = (@_) ? shift : 0;
39 $off += $sz if ($off < 0);
40 my $len = (@_) ? shift : $sz - $off;
41 my @result;
42 for (my $i = 0; $i < $len; $i++)
43 {
44 push(@result,$obj->FETCH($off+$i));
45 }
46 if (@_ > $len)
47 {
48 # Move items up to make room
49 my $d = @_ - $len;
50 my $e = $off+$len;
51 $obj->EXTEND($sz+$d);
52 for (my $i=$sz-1; $i >= $e; $i--)
53 {
54 my $val = $obj->FETCH($i);
55 $obj->STORE($i+$d,$val);
56 }
57 }
58 elsif (@_ < $len)
59 {
60 # Move items down to close the gap
61 my $d = $len - @_;
62 my $e = $off+$len;
63 for (my $i=$off+$len; $i < $sz; $i++)
64 {
65 my $val = $obj->FETCH($i);
66 $obj->STORE($i-$d,$val);
67 }
68 $obj->STORESIZE($sz-$d);
69 }
70 for (my $i=0; $i < @_; $i++)
71 {
72 $obj->STORE($off+$i,$_[$i]);
73 }
74 return @result;
75}
76
77package Tie::StdArray;
78use vars qw(@ISA);
79@ISA = 'Tie::Array';
80
81sub TIEARRAY { bless [], $_[0] }
82sub FETCHSIZE { scalar @{$_[0]} }
83sub STORESIZE { $#{$_[0]} = $_[1]-1 }
84sub STORE { $_[0]->[$_[1]] = $_[2] }
85sub FETCH { $_[0]->[$_[1]] }
86sub CLEAR { @{$_[0]} = () }
87sub POP { pop(@{$_[0]}) }
88sub PUSH { my $o = shift; push(@$o,@_) }
89sub SHIFT { shift(@{$_[0]}) }
90sub UNSHIFT { my $o = shift; unshift(@$o,@_) }
91
92sub SPLICE
93{
94 my $ob = shift;
95 my $sz = $ob->FETCHSIZE;
96 my $off = @_ ? shift : 0;
97 $off += $sz if $off < 0;
98 my $len = @_ ? shift : $sz-$off;
99 return splice(@$ob,$off,$len,@_);
100}
ab3c8535 101
1021;
103
104__END__
105
106=head1 NAME
107
108Tie::Array - base class for tied arrays
109
110=head1 SYNOPSIS
111
a60c0954 112 package NewArray;
ab3c8535 113 use Tie::Array;
a60c0954 114 @ISA = ('Tie::Array');
115
116 # mandatory methods
117 sub TIEARRAY { ... }
118 sub FETCH { ... }
119 sub FETCHSIZE { ... }
120
121 sub STORE { ... } # mandatory if elements writeable
122 sub STORESIZE { ... } # mandatory if elements can be added/deleted
123
124 # optional methods - for efficiency
125 sub CLEAR { ... }
ab3c8535 126 sub PUSH { ... }
127 sub POP { ... }
128 sub SHIFT { ... }
129 sub UNSHIFT { ... }
130 sub SPLICE { ... }
a60c0954 131 sub EXTEND { ... }
132 sub DESTROY { ... }
133
134 package NewStdArray;
135 use Tie::Array;
136
137 @ISA = ('Tie::StdArray');
138
139 # all methods provided by default
140
141 package main;
142
143 $object = tie @somearray,Tie::NewArray;
144 $object = tie @somearray,Tie::StdArray;
145 $object = tie @somearray,Tie::NewStdArray;
146
147
ab3c8535 148
149=head1 DESCRIPTION
150
a60c0954 151This module provides methods for array-tying classes. See
152L<perltie> for a list of the functions required in order to tie an array
153to a package. The basic B<Tie::Array> package provides stub C<DELETE>
154and C<EXTEND> methods, and implementations of C<PUSH>, C<POP>, C<SHIFT>,
155C<UNSHIFT>, C<SPLICE> and C<CLEAR> in terms of basic C<FETCH>, C<STORE>,
156C<FETCHSIZE>, C<STORESIZE>.
157
ceeed4e5 158The B<Tie::StdArray> package provides efficient methods required for tied arrays
a60c0954 159which are implemented as blessed references to an "inner" perl array.
160It inherits from B<Tie::Array>, and should cause tied arrays to behave exactly
ceeed4e5 161like standard arrays, allowing for selective overloading of methods.
a60c0954 162
163For developers wishing to write their own tied arrays, the required methods
164are briefly defined below. See the L<perltie> section for more detailed
165descriptive, as well as example code:
166
167=over
168
169=item TIEARRAY classname, LIST
170
171The class method is invoked by the command C<tie @array, classname>. Associates
172an array instance with the specified class. C<LIST> would represent
173additional arguments (along the lines of L<AnyDBM_File> and compatriots) needed
174to complete the association. The method should return an object of a class which
175provides the methods below.
176
177=item STORE this, index, value
178
179Store datum I<value> into I<index> for the tied array assoicated with
180object I<this>. If this makes the array larger then
181class's mapping of C<undef> should be returned for new positions.
182
183=item FETCH this, index
184
185Retrieve the datum in I<index> for the tied array assoicated with
186object I<this>.
187
188=item FETCHSIZE this
189
190Returns the total number of items in the tied array assoicated with
191object I<this>. (Equivalent to C<scalar(@array)>).
ab3c8535 192
a60c0954 193=item STORESIZE this, count
194
195Sets the total number of items in the tied array assoicated with
196object I<this> to be I<count>. If this makes the array larger then
197class's mapping of C<undef> should be returned for new positions.
198If the array becomes smaller then entries beyond count should be
199deleted.
200
201=item EXTEND this, count
202
203Informative call that array is likely to grow to have I<count> entries.
204Can be used to optimize allocation. This method need do nothing.
205
206=item CLEAR this
207
208Clear (remove, delete, ...) all values from the tied array assoicated with
209object I<this>.
210
211=item DESTROY this
212
213Normal object destructor method.
214
215=item PUSH this, LIST
216
217Append elements of LIST to the array.
218
219=item POP this
220
221Remove last element of the array and return it.
222
223=item SHIFT this
224
225Remove the first element of the array (shifting other elements down)
226and return it.
227
228=item UNSHIFT this, LIST
229
230Insert LIST elements at the begining of the array, moving existing elements
231up to make room.
232
233=item SPLICE this, offset, length, LIST
234
235Perform the equivalent of C<splice> on the array.
236
237I<offset> is optional and defaults to zero, negative values count back
238from the end of the array.
239
240I<length> is optional and defaults to rest of the array.
241
242I<LIST> may be empty.
243
244Returns a list of the original I<length> elements at I<offset>.
245
246=back
ab3c8535 247
248=head1 CAVEATS
249
250There is no support at present for tied @ISA. There is a potential conflict
251between magic entries needed to notice setting of @ISA, and those needed to
a60c0954 252implement 'tie'.
253
254Very little consideration has been given to the behaviour of tied arrays
255when C<$[> is not default value of zero.
256
257=head1 AUTHOR
258
259Nick Ing-Simmons E<lt>nik@tiuk.ti.comE<gt>
ab3c8535 260
261=cut
262