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
|
---|
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 | |
by unlinker (Monk) on Oct 07, 2007 at 04:44 UTC | |
Re: using Sub::Install to override a module method that cannot be edited
by perlfan (Vicar) on Oct 06, 2007 at 19:20 UTC | |
by Anonymous Monk on Oct 07, 2007 at 04:05 UTC | |
A reply falls below the community's threshold of quality. You may see it by logging in. |
Back to
Seekers of Perl Wisdom