Greg McCarroll (deceased)


Class::Mutator - Run-time Dynamic Multiple Inheritance


  package Frog;
  use Class::Mutator qw( -isasubclass );
  sub new { ... }
  sub eat_flies { ... }

  package Prince;
  sub be_charming { ... }

  my $froggie = Frog->new;

  # Now the froggie has been reblessed into a "Frog Prince"
  # class and can take advantage of methods in both classes.


Class::Mutator adds the power of "dynamic polymorphism" to Perl objects.

Any object that inherits Class::Mutator principally gains two new methods, mutate and unmutate that allows them to add methods to themselves at runtime from other packages. The most recently mutated packages take precedence when methods with the same name are defined in more than one package.

This module is similar to, which Michael Schwern was working on around the same time, only a little bit more predictable. It came about while I was doing some training at the BBC and someone asked how you could do this easily; after discussion with my fellow'ers, in particular Piers Cawley, this module came about.

More recently Matthew Simon Cavalletto sent me a version with everything I had been meaning to do on the module a little after I uploaded version 0.03 which only had more substantial tests. So major kudos to Matthew.


To enable a class of objects to mutate, make it a subclass of Class::Mutator.

  package MyBaseClass;
  use Class::Mutator;
  push @ISA, 'Class::Mutator';
  MyBaseClass->new()->mutate( ... );

As a shortcut, you may pass the -isasubclass flag in your use statement, which will produce the same result.

  package MyBaseClass;
  use Class::Mutator '-isasubclass';
  MyBaseClass->new()->mutate( ... );

Finally, if you need to retroactively add mutation capabilities to an existing class, you can do so using the same syntax, with the target class passeds as a parameter.

  package main;
  use MyBaseClass;
  use Class::Mutator '-isasubclass' => 'MyBaseClass';
  MyBaseClass->new()->mutate( ... );

You can also import the specific methods and functions described below and call them directly.

  package main;
  use MyBaseClass;
  use Class::Mutator 'mutate';
  mutate( MyBaseClass->new(), ... );


These methods provide the module's public object-oriented interface.


  $object->mutate( @packages );

Adds a mutation.


  $object->unmutate( @packages );

Remove mutation abilities via a package


These functions are used internally to support the methods described above.


  $reblessed_object = apply_mutation( $object, $op, @packages );

Builds a new package list, based on the current package for a given object, the operator (either "+" or "-"), and the packages to be added or removed.


  @packages = get_mutation_list($object_or_class);

Returns the list of classes a mutated object is based on. If the object is not a mutant, its normal class name will be returned.


  $new_class_name = build_mutation_package(@packages);

Builds the new mutation package. Returns the name of the new class.


  @packages = modify_mutation_list($object_or_class, $op, @packages);

Builds a new package list based on the current packages list and the operation and package (the operation is upon) handed to this method.


Greg McCarroll Mail : Jabber : Homepage :

Matthew Simon Cavalletto Mail :, Homepage :