[tpm] modifying methods per instance

Uri Guttman uri at StemSystems.com
Fri May 7 23:25:43 PDT 2010


>>>>> "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 ---------


More information about the toronto-pm mailing list