http://www.perlmonks.org?node_id=643128

unlinker has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I asked a question here about overriding a module function and was directed to look at Sub::Install which I did. Here is a test example that I wrote. First here is the module that I want to use with the function I want to override:

package BaseModule; use strict; use Exporter; our @ISA = qw( Exporter ); our @EXPORT = qw( function1 ); our @EXPORT_OK = qw( function2 ); sub function1 { print "Responding from Function 1\n"; return 1; } sub function2 { print "Responding now from Function 2\n"; function1(); return 1; } 1;

And here is the code that will do the overriding:

#! /usr/bin/perl use strict; use warnings; use BaseModule qw( function1 function2 ); use Sub::Install qw( reinstall_sub ); reinstall_sub ({ code => 'overridden', into => "BaseModule", as => "function1", }); sub overridden { print "I am the overridden function\n"; function1(); } function2(); exit;

Now a few Questions that I have:

Many thanks for the benefit of your time and wisdom O monks

Replies are listed 'Best First'.
Re: using Sub::Install to override a module method that cannot be edited
by dragonchild (Archbishop) on Oct 07, 2007 at 04:25 UTC
    Meditate on the following. Make sure you fully understand it before putting it into production code. Writing the explanatory comments is left as a mandatory exercise for the reader.
    use strict; use warnings; use BaseModule qw( function1 function2 ); { no warnings 'overriden'; # I think that's the right one. my $old_function1 = \&function1; *function1 = sub { print "I am the overriden function\n"; $old_function1->( @_ ); }; }

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
      Thanks. Thats exactly what I want to do. I used Sub::Install to do it. And I was asking about the namespace calling semantics using that module. Thanks once again
Re: using Sub::Install to override a module method that cannot be edited
by perlfan (Vicar) on Oct 06, 2007 at 19:20 UTC
    If you want to override the function in the base module (class), then you'd need to form an IS-A relationship with the parent using something like use base 'My::Parent';, then you'd re-implement the function in question to override it in the child class. You'd then use that child class instead of the parent when needing the new function. To refer back to the old function from the parent within the child, you'd use the namespace "SUPER::". For example, the following override's the base class's instantiation function, My::Parent::new() ...
    package My::Child; use strict; use base 'My::Parent'; sub new { my $pkg = shift; my $self = $pkg->SUPER::new(@_); # call's My::Parent::new # .. add new fields, do additional things.. return $self; } 1;
      I do not want to do sub classing in the OO sense. I just want to override in a non-OO module
A reply falls below the community's threshold of quality. You may see it by logging in.