#!/usr/bin/perl use warnings; use strict; #-------------------------------------------------- # MyTest::MethodInstaller # # This is a simple "module" to install a generated # method into the caller's package. # # This is analogous to your CustomMethodMaker class, # though you'll call $class->install_methods() # instead of installing the method directly. #-------------------------------------------------- package MyTest::MethodInstaller; sub install { my ($caller) = caller(0); my $method = shift; my $method_ref = sub { my $self = shift; print "$caller\::$method called.\n"; # And here's the magic trick eval "package $caller; \$self->SUPER::$method(\@_); 1" or die $@; }; # Install the method into the caller's namespace no strict 'refs'; *{"$caller\::foo"} = $method_ref; } #-------------------------------------------------- # MyTest::Super # # This is a simple test super-class. #-------------------------------------------------- package MyTest::Super; sub new { my $class = shift; return bless {}, $class; } # This is the super-class' foo() method that # we're interested in calling. This will have # been installed by Class::MethodMaker. sub foo { my $self = shift; print "MyTest::Super::foo called with (",join(',',@_),").\n"; } #-------------------------------------------------- # MyTest # # This is a simple test base-class that uses our # MethodInstaller class. #-------------------------------------------------- package MyTest; use base 'MyTest::Super'; # Request that the generated method # be installed into our namespace. MyTest::MethodInstaller::install('foo'); #-------------------------------------------------- # main # # This is the simple test script. #-------------------------------------------------- package main; my $t = new MyTest; $t->foo('bar', 'quux');