Not ready for production. :) With Perl, we can insert code into @INC so that we get ahead of the constant definition:

First, MyBase.pm. This module defines a constant, FOO of 42. But we live in a universe where FOO needs to be 44. Here's the example base:

package MyBase; use strict; use warnings; use Exporter; our @ISA = qw(Exporter); our @EXPORT = qw(bar); sub FOO() {42} sub bar { return FOO(); } 1;

If someone were to use this module and call bar(), its return value would be 42.

Now I need a module that depends on MyBase.pm. We'll call it MySub.pm. Here's where I can get in front of loading MyBase. To do that I'll insert code into @INC so that use and require get a little modified behavior:

package MySub; use strict; use warnings; sub _filter { my $module = $_[1]; if ($module =~ m/^MyBase/) { foreach my $dir (@INC[1..$#INC]) { if (-e "$dir/MyBase.pm" && -f _) { open FH, '<', "$Bin/lib/MyBase.pm" or die $!; last; } } die "Couldn't find MyBase.pm in @INC\n" unless defined *FH; return \'', \*FH, sub { if (length $_) { $_ =~ s/(sub FOO\(\)\s*\{)(\d+)(\})/${1}44${3}/; return 1; } else { return 0; } }; } return (); } BEGIN {unshift @INC, \&_filter} use MyBase; use Exporter; our @ISA = qw(Exporter); our @EXPORT = qw(baz); sub baz { return bar(); } 1;

Here I have a subroutine named baz which calls MyBase::bar(), which returns the constant stored in MyBase::FOO, which would normally be 42. However, I've inserted a subroutine into @INC named _filter() that looks for the loading of MyBase.pm and replaces the FOO definition with a new one, with a value of 44.

Finally, a small sample app using this:

#!/usr/bin/env perl use strict; use warnings; use FindBin qw($Bin); use lib "$Bin/lib"; use MySub; print baz(), "\n";

If we didn't override FOO in MyBase, this code would print 42. But the approach is successful, and the output is now 44.

Obviously this isn't necessarily robust. A better approach would be to submit a patch to the maintainers of AnyEvent::DNS that makes the hard-coded value configurable. Just by removing the prototype it would become possible to subclass and override, or monkeypatch, for example. But in a pinch, a lot of things are possible. Look at the documentation in require for an explanation of how my approach works.


Dave


In reply to Re: How to redefine a modules private function? by davido
in thread How to redefine a modules private function? by sectokia

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.