Another version, using a tied hash:
#!/usr/bin/perl
package DynHash;
use Tie::Hash;
our @ISA = 'Tie::StdHash';
sub TIEHASH {
my $class = shift;
return bless {}, $class;
}
sub FETCH {
my $self = shift;
return ${ $self->{$_[0]}[0] } - ${ $self->{$_[0]}[1] }
}
package main;
use Data::Dumper;
tie my %result, "DynHash";
my $ini = 4;
my $last = 2;
%result = (
SAS1 => [ \$ini, \$last ],
SAS2 => [ \$last, \$ini ],
SAS3 => [ \$ini, \$last ]
);
print Dumper \%result;
$ini = 10;
$last = 5;
print Dumper \%result;
__END__
$VAR1 = {
'SAS3' => 2,
'SAS2' => -2,
'SAS1' => 2
};
$VAR1 = {
'SAS3' => 5,
'SAS2' => -5,
'SAS1' => 5
};
i.e., store references to $ini and $last in the hash, and dynamically recompute the difference from them at the time the values of the (tied) hash are being fetched.