[OT] Wrapping methods

Nicholas Clark nick at ccl4.org
Mon Jul 6 17:06:03 BST 2009

Sorry to mention the 4 letter word...

I'm not sure if there is a good answer to this, but:

Say I have a system of interchangeable components defined by

   @ISA = 'Generic::Base::Class';

   sub run {
        ... # does stuff

such that it's invoked to do its work as $object->run(...);

Is there any not-insanely-hacky way to conjure up a wrapper class

such that a component could be written as 

   @ISA = 'Generic::Base::ClassWithTimeout';

   sub run {
        ... # does stuff

called, as ever, as $object->run(...);

but Generic::Base::ClassWithTimeout has done something funky to wrap &run
in a routine that does the eval block/alarm/$SIG{ALARM} handler?

Basically, the fun is that I'd like to do something equivalent to invert the
order of inheritance, such that the parent's &run seems to be called first,
and it can "SUPER" delegate to its child.

Of course this can also be solved in various other ways, such as having the
code be

   @ISA = 'Generic::Base::ClassWithTimeout';

   sub run_with_timeout {
        ... # does stuff

and Generic::Base::ClassWithTimeout having a run() method that calls
$self->run_with_timeout(...) inside the alarmed block, but that doesn't feel
as elegant an interface.

Nicholas Clark

More information about the london.pm mailing list