Update: Tie::NumRange-0.01 is on CPAN (or will be when your local mirror refreshes), as well as my modules page.
Very simple concept really, which I got from $code_or_die's recent node for shifting RGB values. Here's an example use:package Tie::NumRange; # (initial, low, high) sub TIESCALAR { my $class = shift; my $self = bless [ @_ ], $class; $self->STORE($_[0]); # just in case return $self; } sub STORE { my ($self,$val) = @_; return $self->[0] = $self->[1] if defined($self->[1]) and $self->[1] > $val; return $self->[0] = $self->[2] if defined($self->[2]) and $self->[2] < $val; return $self->[0] = $val; } sub FETCH { $_[0][0] } 1;
The arguments to tie() are the variable, the class, the inital value, the floor, and the ceiling. To make a number with no least value, send undef as the floor (likewise for the ceiling).use Tie::NumRange; my ($r,$g,$b); tie $r, Tie::NumRange => (0, 0, 255); tie $g, Tie::NumRange => (0, 0, 255); tie $b, Tie::NumRange => (0, 0, 255); $r = 100; # fine $g = 200; # fine $b = 300; # set to 255 $b -= 150; # fine, is now 105 $g -= 150; # fine, is now 50 $r -= 150; # set to 0
In reply to Tie::NumRange by japhy
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |