Hello all,
I am using flyweight objects (i.e. the objects are blessed scalars which are keys of a lexical hash), which have resource-intensive methods that don't take any arguments.
I sometimes have a way to get pre-computed values for many of these methods.
I want to do two things :
- Be able to tell the objects what the return values for these methods should be, so that the methods don't have to be called sometimes.
- Memoize any calls to these methods (on an instance level), so that the methods don't do the resource-intensive operations multiple times.
The way I've done this is as follows (_get, _set, and _is_set are methods that get, set or check the attribute of the object by looking in the lexical hash) :
# In the various classes :
__PACKAGE__->setupMemoizedSubs(
methodname => sub {
my $self = shift;
#... do resource-intensive stuff .. },
anothermethod => sub { ... },
)
# In a common base class :
sub setupMemoizedSubs {
my ($class,%args) = @_;
no strict 'refs';
while (my ($name,$sub) = each %args) {
my $private_element = "__memoized_$name";
*{"${class}::$name"} = sub {
my $self = shift;
$self->_set($private_element => $_[0]) if @_==1;
return $self->_get($private_element)
if $self->_is_set($private_element);
$self->_set($private_element => $sub->($self));
$self->_get($private_element);
};
}
use strict 'refs';
}
# Somewhere else
$object->methodname("pre-computed value"); # set the value
$object->methodname(); # Gets the value we set
# Or if we omit the first one...
$object->methodname() # Actually calls the method
So, my questions are :
- Does it matter whether I do this with an anonymous sub in a closure (as above) or is a dummy named subroutine better?
-
- Could I have done the above using Memoize.pm somehow?
- Any other suggested methods for accomplishing this?
thanks