But hooking into a later sub like a() actually works.

But this kind of patching requires that the code never changes, tho it's arguably safer than a source-filter in disguise.

use strict; use warnings; use Data::Dump qw/pp dd/; $|=1; my $DBG; BEGIN { $DBG=0; $\="\n"; my $old = $SIG{__WARN__}; $SIG{__WARN__} = sub { my ( $msg ) = @_ ; if ($msg =~ /Constant subroutine a redefined/ ) { $SIG{__WARN__} = $old; print "REDEFINED", pp caller if $DBG; no warnings 'redefine'; *ALIEN::DOMAIN_PORT = sub() { 666 }; } }; *ALIEN::a = sub () {"DUMMY"}; # cause redefine warning } # used package package ALIEN; # ---8<---- snippet from AnyEvent::DNS sub DOMAIN_PORT() { 53 } sub resolver (); sub a($$) { my ($domain, $cb) = @_; resolver->resolve ($domain => "a", sub { $cb->(map $_->[4], @_); }); } # --->8---- snippet from AnyEvent::DNS BEGIN { print "pre compile" if $DBG; } sub test { print "Inside test: ", DOMAIN_PORT; } test();

Inside test: 666

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery


In reply to Re^5: How to redefine a modules private function? (works) by LanX
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.