in reply to Re: How to redefine a modules private function?
in thread How to redefine a modules private function?
#!/usr/bin/perl use strict; use warnings; use feature 'say'; use Path::Tiny; use File::Temp qw(tempfile); sub hotpatch { if ($_[1] eq 'AnyEvent/DNS.pm') { for my $inc (@INC) { next if ref $inc; my $fn = path($inc)->child($_[1]); if (open my $in, '<', $fn) { my ($out) = tempfile(UNLINK => 1); while (<$in>) { s/sub\s+DOMAIN_PORT\b/sub DOMAIN_PORT () { 1053 } +sub FORMER_DOMAIN_PORT/; print {$out} $_; } seek($out, 0, 0); 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();
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: How to redefine a modules private function?
by Corion (Patriarch) on Mar 09, 2022 at 10:08 UTC | |
by salva (Canon) on Mar 09, 2022 at 11:24 UTC | |
by haukex (Archbishop) on Mar 09, 2022 at 13:30 UTC | |
by LanX (Saint) on Mar 09, 2022 at 15:34 UTC |