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

# arguments:  package name, reference to initial value, name of tied
# variable (without $)
sub TIESCALAR {
   my ($pkg, $rval, $name) = @_;
   # $obj will be a reference to an anonymous array, consisting of the
   # name of the tied variable and the (dereferenced) value
   my $obj = [$name, $$rval];
   bless $obj, $pkg;
   return $obj;
}

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

# arguments will be the variable name and the value to be written
sub STORE {
   # need parentheses when 'my' is applied to more than one variable
   my ($obj, $val) = @_;
   print STDERR 'Wrote   $', $obj->[0], " ... $val \n";
   $obj->[1] = $val;
}

1;

