Dear Monks,
I am trying to write a module, which will overwrite a subroutine in another module.
The first thing I need to know is, whether it's possible to be notified when a module has been loaded. One way I thought of doing this was, to unshift this subref into the @INC:
my ($coderef, $filename) = @_;
my $result;
@INC = grep {$_ ne $self->{__connector}} @INC;
require $filename;
# try to overwrite a method here
unshift @INC, $self->{__connector};
Unfortunately, the module is still loaded after I finish my work, and I don't know how to stop this. Or is there a way to have my subroutine get called whenever a module is loaded?
Another problem, which I noticed while I was debugging this issue, was that I couldn't actually overwrite a module.
For a particular module 'IWL::Script', I tried doing this:
*setScript = *IWL::Script::setScript;
delete $::{'IWL::'}{'Script::'}{'setScript'};
no warnings qw(redefine);
sub IWL::Script::setScript {
my ($self, $param) = @_;
if (ref $param eq 'CODE') {
*setScript{CODE}->($self, $p2js->covert($param));
} else {
*setScript{CODE}->($self, $param);
}
}
I'm not even sure whether the first 2 lines are able to actually copy the subroutine, instead of just aliasing it, which is another problem in itself.
But what I noticed what, that the inner sub did not overwrite the one in the IWL::Script module
So how can I overwrite modules which are not part of my namespace, and in the same time have a reference to the original code, so that I can call it in the override (like a wrapper)?
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.