<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Tie::Array;

use 5.006_001;
use strict;
use Carp;
our $VERSION = '1.06';

# Pod documentation after __END__ below.

sub DESTROY { }
sub EXTEND  { }
sub UNSHIFT { scalar shift-&gt;SPLICE(0,0,@_) }
sub SHIFT { shift-&gt;SPLICE(0,1) }
sub CLEAR   { shift-&gt;STORESIZE(0) }

sub PUSH
{
 my $obj = shift;
 my $i   = $obj-&gt;FETCHSIZE;
 $obj-&gt;STORE($i++, shift) while (@_);
}

sub POP
{
 my $obj = shift;
 my $newsize = $obj-&gt;FETCHSIZE - 1;
 my $val;
 if ($newsize &gt;= 0)
  {
   $val = $obj-&gt;FETCH($newsize);
   $obj-&gt;STORESIZE($newsize);
  }
 $val;
}

sub SPLICE {
    my $obj = shift;
    my $sz  = $obj-&gt;FETCHSIZE;
    my $off = (@_) ? shift : 0;
    $off += $sz if ($off &lt; 0);
    my $len = (@_) ? shift : $sz - $off;
    $len += $sz - $off if $len &lt; 0;
    my @result;
    for (my $i = 0; $i &lt; $len; $i++) {
        push(@result,$obj-&gt;FETCH($off+$i));
    }
    $off = $sz if $off &gt; $sz;
    $len -= $off + $len - $sz if $off + $len &gt; $sz;
    if (@_ &gt; $len) {
        # Move items up to make room
        my $d = @_ - $len;
        my $e = $off+$len;
        $obj-&gt;EXTEND($sz+$d);
        for (my $i=$sz-1; $i &gt;= $e; $i--) {
            my $val = $obj-&gt;FETCH($i);
            $obj-&gt;STORE($i+$d,$val);
        }
    }
    elsif (@_ &lt; $len) {
        # Move items down to close the gap
        my $d = $len - @_;
        my $e = $off+$len;
        for (my $i=$off+$len; $i &lt; $sz; $i++) {
            my $val = $obj-&gt;FETCH($i);
            $obj-&gt;STORE($i-$d,$val);
        }
        $obj-&gt;STORESIZE($sz-$d);
    }
    for (my $i=0; $i &lt; @_; $i++) {
        $obj-&gt;STORE($off+$i,$_[$i]);
    }
    return wantarray ? @result : pop @result;
}

sub EXISTS {
    my $pkg = ref $_[0];
    croak "$pkg doesn't define an EXISTS method";
}

sub DELETE {
    my $pkg = ref $_[0];
    croak "$pkg doesn't define a DELETE method";
}

package Tie::StdArray;
use vars qw(@ISA);
@ISA = 'Tie::Array';

sub TIEARRAY  { bless [], $_[0] }
sub FETCHSIZE { scalar @{$_[0]} }
sub STORESIZE { $#{$_[0]} = $_[1]-1 }
sub STORE     { $_[0]-&gt;[$_[1]] = $_[2] }
sub FETCH     { $_[0]-&gt;[$_[1]] }
sub CLEAR     { @{$_[0]} = () }
sub POP       { pop(@{$_[0]}) }
sub PUSH      { my $o = shift; push(@$o,@_) }
sub SHIFT     { shift(@{$_[0]}) }
sub UNSHIFT   { my $o = shift; unshift(@$o,@_) }
sub EXISTS    { exists $_[0]-&gt;[$_[1]] }
sub DELETE    { delete $_[0]-&gt;[$_[1]] }

sub SPLICE
{
 my $ob  = shift;
 my $sz  = $ob-&gt;FETCHSIZE;
 my $off = @_ ? shift : 0;
 $off   += $sz if $off &lt; 0;
 my $len = @_ ? shift : $sz-$off;
 return splice(@$ob,$off,$len,@_);
}

1;

__END__

=head1 NAME

Tie::Array - base class for tied arrays

=head1 SYNOPSIS

    package Tie::NewArray;
    use Tie::Array;
    @ISA = ('Tie::Array');

    # mandatory methods
    sub TIEARRAY { ... }
    sub FETCH { ... }
    sub FETCHSIZE { ... }

    sub STORE { ... }       # mandatory if elements writeable
    sub STORESIZE { ... }   # mandatory if elements can be added/deleted
    sub EXISTS { ... }      # mandatory if exists() expected to work
    sub DELETE { ... }      # mandatory if delete() expected to work

    # optional methods - for efficiency
    sub CLEAR { ... }
    sub PUSH { ... }
    sub POP { ... }
    sub SHIFT { ... }
    sub UNSHIFT { ... }
    sub SPLICE { ... }
    sub EXTEND { ... }
    sub DESTROY { ... }

    package Tie::NewStdArray;
    use Tie::Array;

    @ISA = ('Tie::StdArray');

    # all methods provided by default

    package main;

    $object = tie @somearray,'Tie::NewArray';
    $object = tie @somearray,'Tie::StdArray';
    $object = tie @somearray,'Tie::NewStdArray';



=head1 DESCRIPTION

This module provides methods for array-tying classes. See
L&lt;perltie&gt; for a list of the functions required in order to tie an array
to a package. The basic B&lt;Tie::Array&gt; package provides stub C&lt;DESTROY&gt;,
and C&lt;EXTEND&gt; methods that do nothing, stub C&lt;DELETE&gt; and C&lt;EXISTS&gt;
methods that croak() if the delete() or exists() builtins are ever called
on the tied array, and implementations of C&lt;PUSH&gt;, C&lt;POP&gt;, C&lt;SHIFT&gt;,
C&lt;UNSHIFT&gt;, C&lt;SPLICE&gt; and C&lt;CLEAR&gt; in terms of basic C&lt;FETCH&gt;, C&lt;STORE&gt;,
C&lt;FETCHSIZE&gt;, C&lt;STORESIZE&gt;.

The B&lt;Tie::StdArray&gt; package provides efficient methods required for tied arrays
which are implemented as blessed references to an "inner" perl array.
It inherits from B&lt;Tie::Array&gt;, and should cause tied arrays to behave exactly
like standard arrays, allowing for selective overloading of methods.

For developers wishing to write their own tied arrays, the required methods
are briefly defined below. See the L&lt;perltie&gt; section for more detailed
descriptive, as well as example code:

=over 4

=item TIEARRAY classname, LIST

The class method is invoked by the command C&lt;tie @array, classname&gt;. Associates
an array instance with the specified class. C&lt;LIST&gt; would represent
additional arguments (along the lines of L&lt;AnyDBM_File&gt; and compatriots) needed
to complete the association. The method should return an object of a class which
provides the methods below.

=item STORE this, index, value

Store datum I&lt;value&gt; into I&lt;index&gt; for the tied array associated with
object I&lt;this&gt;. If this makes the array larger then
class's mapping of C&lt;undef&gt; should be returned for new positions.

=item FETCH this, index

Retrieve the datum in I&lt;index&gt; for the tied array associated with
object I&lt;this&gt;.

=item FETCHSIZE this

Returns the total number of items in the tied array associated with
object I&lt;this&gt;. (Equivalent to C&lt;scalar(@array)&gt;).

=item STORESIZE this, count

Sets the total number of items in the tied array associated with
object I&lt;this&gt; to be I&lt;count&gt;. If this makes the array larger then
class's mapping of C&lt;undef&gt; should be returned for new positions.
If the array becomes smaller then entries beyond count should be
deleted.

=item EXTEND this, count

Informative call that array is likely to grow to have I&lt;count&gt; entries.
Can be used to optimize allocation. This method need do nothing.

=item EXISTS this, key

Verify that the element at index I&lt;key&gt; exists in the tied array I&lt;this&gt;.

The B&lt;Tie::Array&gt; implementation is a stub that simply croaks.

=item DELETE this, key

Delete the element at index I&lt;key&gt; from the tied array I&lt;this&gt;.

The B&lt;Tie::Array&gt; implementation is a stub that simply croaks.

=item CLEAR this

Clear (remove, delete, ...) all values from the tied array associated with
object I&lt;this&gt;.

=item DESTROY this

Normal object destructor method.

=item PUSH this, LIST

Append elements of LIST to the array.

=item POP this

Remove last element of the array and return it.

=item SHIFT this

Remove the first element of the array (shifting other elements down)
and return it.

=item UNSHIFT this, LIST

Insert LIST elements at the beginning of the array, moving existing elements
up to make room.

=item SPLICE this, offset, length, LIST

Perform the equivalent of C&lt;splice&gt; on the array.

I&lt;offset&gt; is optional and defaults to zero, negative values count back
from the end of the array.

I&lt;length&gt; is optional and defaults to rest of the array.

I&lt;LIST&gt; may be empty.

Returns a list of the original I&lt;length&gt; elements at I&lt;offset&gt;.

=back

=head1 CAVEATS

There is no support at present for tied @ISA. There is a potential conflict
between magic entries needed to notice setting of @ISA, and those needed to
implement 'tie'.

=head1 AUTHOR

Nick Ing-Simmons E&lt;lt&gt;nik@tiuk.ti.comE&lt;gt&gt;

=cut
</pre></body></html>