http://qs321.pair.com?node_id=11141939


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

Ok, here it is the full solution using an in-memory file:
use strict; use warnings; use Path::Tiny; sub hotpatch { my ($self, $module) = @_; if ($module eq 'AnyEvent/DNS.pm') { for my $inc (@INC) { next if ref $inc; if (open my $in, '<', path($inc)->child($module)) { my $buffer = do { undef $/; <$in> }; $buffer =~ s/\bsub\s+DOMAIN_PORT\b/sub DOMAIN_PORT () +{ 1053 } sub FORMER_DOMAIN_PORT/; open my $out, '<', \$buffer or die "Couldn't patch AnyEvent::DNS; your Perl do +es not support in-memory filehandles"; return $out; } } warn "couldn't patch AnyEvent::DNS"; } return undef; } BEGIN { unshift @INC, \&hotpatch } use AnyEvent::DNS; BEGIN { @INC = grep not(ref and $_ eq \&hotpatch), @INC } say AnyEvent::DNS::DOMAIN_PORT(); say AnyEvent::DNS::FORMER_DOMAIN_PORT();