Dynamic alteration of a class hierarchy
March 16, 2007
I just came across a beautiful piece of code while looking at the Perl module 'mixin' on CPAN. It boils down to:
sub mixer {
my ($mixin,$mixed_with) = @_;
require base;
eval sprintf q{
package %s;
base->import($mixed_with);
}, $mixin;
}
When called, the function mixer will alter dynamically the inheritance of the class $mixin, adding the class $mixed_with to the list of its parent classes.
sub mixer {
my ($mixin,$mixed_with) = @_;
require base;
eval sprintf q{
package %s;
base->import($mixed_with);
}, $mixin;
}
When called, the function mixer will alter dynamically the inheritance of the class $mixin, adding the class $mixed_with to the list of its parent classes.