[tpm] modifying methods per instance

Shaun Fryer sfryer at sourcery.ca
Fri May 7 23:37:39 PDT 2010


I found an alternate solution! I used a dispatch table within each instance,
and then created a single accessor method to execute the respective code
ref. Took a bit of tweaking, but works like a charm! Thanks for the ideas
guys. Both good, and potentially useful for future/different situations.

Cheers,
--
Shaun Fryer


On Sat, May 8, 2010 at 2:25 AM, Uri Guttman <uri at stemsystems.com> wrote:

> >>>>> "AH" == Abram Hindle <abram.hindle at softwareprocess.es> writes:
>
>  AH> I have not tried this. I bet you can get MOOSE to do it.
>
>  AH> What I'd try is to generate a new package where the previous class is
> used as base for that class.
>
>  AH> I'd define those methods within that package and then rebless the
> instance.
>
>  AH> Make a Gensym sub that can test for existence and then return a random
> package name;
>
>  AH> you could generate code like this:
>  AH> package SoAndSo::Prototype::Gensym::XYZABC
>
>  AH> use base qw(SoAndSo);
>
>  AH> *SoAndSo::Prototype::Gensym::XYZABC = $method1;
>  AH> *SoAndSo::Prototype::Gensym::XYZABC = $method2;
>
>
>  AH> bless("SoAndSo::Prototype::Gensym::XYZABC",$instance1);
>
> just use the reference itself as the package name. it is guaranteed to
> be unique. bless the object into its own class, set the @ISA to look at
> the parent class. this is basically what stuff like class::classless
> does. every object is its own class and has its own @ISA, namespace and
> methods. you can still inherit as you wish by controlling @ISA.
>
> something like:
>
>        my $package = "$obj" ;
>        bless $obj, $package ;
>        @{"${package}::ISA"} = $parent_class ; # no strict refs for this
>
>        *{"${package}::$method"} = sub { print "i am a method of $package" }
> ;
>
> much simpler than all the code so far.
>
> uri
>
> --
> Uri Guttman  ------  uri at stemsystems.com  --------  http://www.sysarch.com--
> -----  Perl Code Review , Architecture, Development, Training, Support
> ------
> ---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com---------
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/toronto-pm/attachments/20100508/2961cc86/attachment.html>


More information about the toronto-pm mailing list