in reply to Re: Packages with subs using fully qualified class names, not always their own. (monkey patch)
in thread Packages with subs using fully qualified class names, not always their own.

"I'm using monkey patching in one of my modules, but took care to localize it."

It might be of interest how you did it. Perhaps you like to show it?

Best regards, Karl

«The Crux of the Biscuit is the Apostrophe»

perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

  • Comment on Re^2: Packages with subs using fully qualified class names, not always their own. (monkey patch)
  • Download Code

Replies are listed 'Best First'.
Re^3: Packages with subs using fully qualified class names, not always their own. (monkey patch)
by LanX (Saint) on Jul 01, 2017 at 11:44 UTC
    I only changed the "alien" routine temporarily

    Simplified something like

    sub my_func { local *OtherPackage::routine = sub { ... } # now use OtherPackage }

    local will assure that the old code is restored when the scope is left and you have to keep full control over the code inside the scope.

    This is not 100% safe because an exception could occur in the middle and a SIG handler could theoretically use OtherPackage.

    in my case this is pretty impossible, but to be safe you could additionally check caller inside the patched routine.

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

      $_extend from Object::Util:

      my $object = OtherPackage->new(...); $object->$_extend({ some_method => sub { ... }, other_method => sub { ... }, }); return $object->some_method( $object->other_method );
        ?

        Only code and no message?

        anyway, from the doc:

        > Object::Util - a selection of utility methods that can be called on blessed objects

        I'm not using it on blessed objects or classes, just plain packages, in case you meant it as an alternative.

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Je suis Charlie!

      Thank you very much Rolf. Best regards, Karl

      «The Crux of the Biscuit is the Apostrophe»

      perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

      Maye Sub::Override or some such helper takes care of that possibility autofeaturely?