#!/usr/bin/perl use strict; use warnings; use TestFor; my @e; // moved outside as suggested before tie @e, 'TestFor', 0; my $i = 0; BIGLOOP: while (1) { # my @e; for my $j (0..@e / 2 - 1) { print "died where I shouldn't have: $i\n"; last BIGLOOP; } $i++; } #### 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;