[tpm] modifying methods per instance
Shaun Fryer
sfryer at sourcery.ca
Fri May 7 21:46:12 PDT 2010
Does anyone know an easy way to 'attach' *different* sets of methods to two
*instances* of the same package?
Right now I'm doing something like this fairly simplified example below.
sub _attach_methods {
my ($self, @aVars) = @_;
my $class = ref $self;
for my $sVar (@aVars) {
*{ $class .'::'. $sVar } = sub {
my $o = shift;
return $o->{$sVar} if $o->{$sVar};
$o->{$sVar} = $self->_get_var($sVar);
return $o->{$sVar};
};
}
}
$instance_1->_attach_methods(qw( foo bar));
$instance_2->_attach_methods(qw( baz quux ));
The challenge is that @aVars above will (necessarily) be different for each
instance according to a database driven chain of relations. In fact there
are more than one _attach_methods() style operations being done (ie.
_attach_these(), _attach_those() ), where each instance may have the same
method names available, but each with different functionality.
Unfortunately, at present when I add a colliding method name to an instance,
it's the parent package method which gets over-written, thereby changing the
functionality of the named method for all instances of the class at once,
rather than just the one instance I'm specifically intending to manipulate.
I've looked into using Moose/Class:MOP::Instance->meta->add_method(...), but
I think it suffers from the same issue since it falls back to
Class::MOP::Class.
Any ideas, or am I going to need to resort to dynamically generating unique
package suffixes for each instance?
Cheers,
--
Shaun Fryer
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/toronto-pm/attachments/20100508/fc391f6f/attachment.html>
More information about the toronto-pm
mailing list