Beefy Boxes and Bandwidth Generously Provided by pair Networks Russ
We don't bite newbies here... much
 
PerlMonks  

using Sub::Install to override a module method that cannot be edited

by unlinker (Monk)
on Oct 06, 2007 at 18:25 UTC ( #643128=perlquestion: print w/ replies, xml ) Need Help??
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:

  • Calling function1 in my test program calls the original function function1 not the overridden function. Pray, why?
  • To call the overridden function I need to qualify it with the package name: BaseModule::function1(). Why?

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

Comment on using Sub::Install to override a module method that cannot be edited
Select or Download Code
Re: using Sub::Install to override a module method that cannot be edited
by Cop on Oct 06, 2007 at 18:44 UTC

    I wish there is never the need for you to ask this question.

Re: using Sub::Install to override a module method that cannot be edited
by perlfan (Deacon) 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
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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://643128]
Approved by lidden
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (11)
As of 2014-04-17 18:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (453 votes), past polls