Does anyone know an easy way to &#39;attach&#39; <u>different</u> sets of methods to two <u>instances</u> of the same package?<br><br>Right now I&#39;m doing something like this fairly simplified example below.<br><br>sub _attach_methods {<br>
    my ($self, @aVars) = @_;<br>    my $class = ref $self;<br>    for my $sVar (@aVars) {<br>          *{ $class .&#39;::&#39;. $sVar } = sub {<br>                  my $o = shift;<br>                return $o-&gt;{$sVar} if $o-&gt;{$sVar};<br>
                  $o-&gt;{$sVar} = $self-&gt;_get_var($sVar);<br>                return $o-&gt;{$sVar};<br>              };<br>    }<br>}<br>$instance_1-&gt;_attach_methods(qw( foo bar));<br>$instance_2-&gt;_attach_methods(qw( baz quux ));<br>
<br><br>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.<br>
<br>Unfortunately, at present when I add a colliding method name to an instance, it&#39;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&#39;m specifically intending to manipulate. I&#39;ve looked into using Moose/Class:MOP::Instance-&gt;meta-&gt;add_method(...), but I think it suffers from the same issue since it falls back to Class::MOP::Class.<br>
<br>Any ideas, or am I going to need to resort to dynamically generating unique package suffixes for each instance?<br><br>Cheers,<br>--<br>Shaun Fryer<br>