in reply to Is there a way to dynamically copy subroutine from one module to another?

This is a variant of Javafan has shown, but really this is ugly hacking and instead you should consider using delegation (see Class::Delegation for an explanation) which may be a better fit than inheritance for your issue.
use strict; package TestA; sub new { bless {}, shift; } sub doit { my $self = shift; warn 'A::doit'; } package TestB; use base 'TestA'; sub doit { my $self = shift; warn 'B::doit'; $self->SUPER::doit(); } package TestC; use base 'TestB'; package TestD; sub new { bless {}, shift; } sub doit { my $self = shift; warn 'D::doit'; } package main; my $c = TestC->new; *TestC::doit = sub { my $self = shift; local *TestA::doit = \&TestD::doit; $self->TestB::doit(); }; $c->doit();

  • Comment on Re: Is there a way to dynamically copy subroutine from one module to another?
  • Download Code

Replies are listed 'Best First'.
Re^2: Is there a way to dynamically copy subroutine from one module to another?
by OlegG (Monk) on Feb 13, 2011 at 10:30 UTC
    Yes, it helped me. Thanks