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();

Replies are listed 'Best First'.
Re^5: How to redefine a modules private function?
by haukex (Archbishop) on Mar 09, 2022 at 13:30 UTC

    Nice! That's like a more concise version of what I posted here.

Re^5: How to redefine a modules private function?
by LanX (Saint) on Mar 09, 2022 at 15:34 UTC
    Nice.

    But when used in production, I'd suggest to check if the regex was successful and raise an error otherwise.

    This is practically a source filter, and the patched source could change between versions. (like implementing a variable port)

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