package Monitor::Array;  # file Array.pm in directory Monitor

# arguments:  package name, reference to initial value of array, name of
# tied variable (without @)
sub TIEARRAY {
   my ($pkg, $rarray, $name) = @_;
   # $rarray is a reference to an array, so @$rarray is the array, so
   # [@$rarray] is a reference to an anonymous array having the same
   # elements as @$rarray (recall that, e.g., [2,5] is a reference to an
   # anonymous array consisting of 2 and 5)
   my $obj = [$name, [@$rarray]];
   bless $obj, $pkg;
   return $obj;
}

# argument will be the variable name and an array index
sub FETCH {
   my ($obj, $index) = @_;
   my $val = $obj->[1]->[$index];
   print STDERR 'Read    $', $obj->[0], "[$index] ... $val\n";
   return $val;
}

# argument will be the variable name, an array index and the value to be
# stored in that element of the array
sub STORE {
   my ($obj, $index, $val) = @_;
   print STDERR 'Wrote   $', $obj->[0], "[$index] ... $val\n";
   $obj->[1]->[$index] = $val;
}

# to allow full array operations, would also need CLEAR() (called by
# interpreter when array is "cleared," i.e. the array name is pointed to
# a different array than before), PUSH() (called when push() is called),
# etc.; see Perl documentation for full list

1;
