in reply to Re^4: How to redefine a modules private function?
in thread How to redefine a modules private function?

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