On Aug 7, Michael Kelly said:
>Is there any way to recalculate a certain variable when another variable's
>value changes? For instance if,
>
>$a = 5;
>$b = $a * 4;
>$a = 10;
You can use my DynScalar module from CPAN.
use DynScalar;
$x = 5;
$y = dynamic { $x * 4 };
print "$x * 4 = $y\n";
$x = 10;
print "$x * 4 = $y\n";
A similar outcome can be gotten with tied scalars:
package Tie::DynScalar;
sub TIESCALAR {
my ($class, $func) = @_;
bless $func, $class;
}
sub FETCH { $_[0]->() }
sub STORE { $_[0] = $_[1] }
1;
This package is used like so:
use Tie::DynScalar;
$x = 5;
tie $y, 'Tie::DynScalar', sub { $x * 4 };
print "$x * 4 = $y\n";
$x = 10;
print "$x * 4 = $y\n";
The overall effect is the same.
--
Jeff "japhy" Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/
RPI Acacia brother #734 http://www.perlmonks.org/ http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]