package TestFor; use Carp; sub TIEARRAY { my $class = shift; my $elemsize = shift; if ( @_ || $elemsize =~ /\D/ ) { croak "usage: tie ARRAY, '" . __PACKAGE__ . "', elem_size"; } print "TIEARRAY\n"; return bless { ELEMSIZE => $elemsize, ARRAY => [], }, $class; } sub FETCH { my $self = shift; my $index = shift; print "FETCH\n"; return $self->{ARRAY}->[$index]; } sub STORE { my $self = shift; my ( $index, $value ) = @_; if ( length $value > $self->{ELEMSIZE} ) { croak "length of $value is greater than $self->{ELEMSIZE}"; } # fill in the blanks $self->EXTEND($index) if $index > $self->FETCHSIZE(); # right justify to keep element size for smaller elements $self->{ARRAY}->[$index] = sprintf "%$self->{ELEMSIZE}s", $value; print "STORE\n"; } sub FETCHSIZE { my $self = shift; #print "FETCHSIZE\n"; // was called very often return scalar @{ $self->{ARRAY} }; } sub STORESIZE { my $self = shift; my $count = shift; if ( $count > $self->FETCHSIZE() ) { foreach ( $count - $self->FETCHSIZE() .. $count ) { $self->STORE( $_, '' ); } } elsif ( $count < $self->FETCHSIZE() ) { foreach ( 0 .. $self->FETCHSIZE() - $count - 2 ) { $self->POP(); } } print "STORESIZE\n"; } sub DESTROY { print "DESTROY\n"; } 1;