-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 =pod Since I'm not in jail yet (for quoting Tim Bray), I thought I would share another method (heh) for adding methods to instances. Here's my last article, in case you missed it: L C (from which C, the Moose metaclass, inherits) has a function called C that will ... create an anonymous class. Here's how you use it. First, create some class, and instantiate it: lang:Perl package Class; use Moose; ... my $instance = Class->new; Then create a new anonymous class: my $new_metaclass = Moose::Meta::Class->create_anon_class( superclasses => ['Class'] ); Here we create an anonymous class that inherits from C. You can also pass other arguments, including C (for a list of roles to apply), and C<< cache => 1 >>, to cache the anonymous class (creating them is slow). Note that if you want to add methods (etc.) to one I, you shouldn't cache the newly-created class; otherwise any changes you make to it will persist even upon calling C again. Even though it I like you are modifying instances, you are modifying classes. It's just how Perl works. Anyway, C returns the newly-created class' metaclass. With that, we can rebless the old instance into the new class: $new_metaclass->rebless_instance($instance); And now C<$instance> is an instance of our anonymous subclass instead of an instance of C! Now we can mess around as necessary: $new_metaclass->add_method( foo => sub { say "FOO" } ); $instance->foo; # says "FOO"! You can also create a fresh instance, of course: my $new_instance = $new_metaclass->name->new( ... ); Finally, some other related functionality you might enjoy is C, which will create a shallow copy of an instance. Here's how to keep the original instance around, and then create a copy that has methods added: my $instance = Class->new; my $clone = Class->meta->clone_instance($instance); my $new_meta = Moose::Meta::Class->create_anon_class( ); $new_meta->rebless_instance($clone); $new_meta->add_method( foo => sub { say "FOO" } ); $instance->can('foo'); # nope $clone->can('foo'); # yes! Anyway, there's lots of cool stuff lurking in the MOP, so I suggest you read the source code when you have a chance. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (GNU/Linux) iD8DBQFH86uK2rw+dVvzZm0RAgSMAKCNGNWx5gakx8R1wNlZaxNs42E2aQCcC5WF EVvfBdUqVF/f0zwyaNJU9x4= =AMMt -----END PGP SIGNATURE-----