require Bounded_Array; tie @ary, 'Bounded_Array', 2; # maximum allowable subscript is 2 $| = 1; for $i (0 .. 10) { print "setting index $i: "; $ary[$i] = 10 * $i; # should raise exception on 3 print "value of element $i now $ary[$i]\n"; } ***** package Bounded_Array; use Carp; use strict; ***** sub TIEARRAY { my $class = shift; my $bound = shift; confess "usage: tie(\@ary, 'Bounded_Array', max_subscript)" if @_ or $bound =~ /\D/; return bless { BOUND => $bound, ARRAY => [], }, $class; } ***** sub FETCH { my ($self, $idx) = @_; if ($idx > $self->{BOUND}) { confess "Array OOB: $idx > $self->{BOUND}"; } return $self->{ARRAY}[$idx]; } ***** sub STORE { my ($self, $idx, $value) = @_; if ($idx > $self->{BOUND} ) { confess "Array OOB: $idx > $self->{BOUND}"; } return $self->{ARRAY}[$idx] = $value; } ***** setting index 0: value of element 0 now 0 setting index 1: value of element 1 now 10 setting index 2: value of element 2 now 20 setting index 3: Array OOB: 3 > 2 at Bounded_Array.pm line 39 Bounded_Array::FETCH called at testba line 12