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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.