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.

Comments

RSS feed for comments on this post.

The URI to TrackBack this entry is: http://lemonnier.se/erwan/blog/bblog/trackback.php/6/

Leave a Comment

Sorry, Comments have been disabled for this post