Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

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

by salva (Canon)
on Mar 09, 2022 at 11:24 UTC ( #11141939=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11141939]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (1)
As of 2022-09-30 23:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (126 votes). Check out past polls.

    Notices?