UPDATE: as requested, an example, using your code. Say we change the definitions of get/set_foo in MyThing to something like this:
After this change, the $clunker code using MyThing requires no changes at all. On the other hand, the tied HashAPI version, must change the accessing code from this:sub set_foo { my ($self, $newfoo) = @_; $self->[1] = localtime(); # track last change $self->[0] = $newfoo; $self->_recalculate_bar(); } sub get_foo { $self->[2] = localtime(); # track last access return $_[0]->[0]; }
to this:$slick->{foo} = 'Some value'; print $slick->{foo}, "\n";
You could avoid needing this change by changing the FETCH and STORE, like this:$slick->set_foo('Some value'); print $slick->get_foo(), "\n";
That would quickly become cumbersome though.sub STORE { my ($self, $key, $val) = @_; # assuming this class has more than just 'foo'... unless (grep { $_ eq $key } @CLASS_KEYS ) { croak (ref $self) . "has no $key member"; } if ($key eq 'foo') { $self->[1] = localtime(); # track last change $self->[0] = $val; $self->_recalculate_bar(); } else { # set normal keys somehow } } sub FETCH { my ($self, $key, $val) = @_; # assuming this class has more than just 'foo'... unless (grep { $_ eq $key } @CLASS_KEYS ) { croak (ref $self) . "has no $key member"; } if ($key eq 'foo') { $self->[2] = localtime(); # track last access return $self->[0]; } else { # get normal keys somehow } }
In reply to Re^9: tie for Perlish, encapsulated objects
by perrin
in thread tie for Perlish, encapsulated objects
by Roy Johnson
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |