in reply to Packages with subs using fully qualified class names, not always their own.

What you describe is called a "Monkey Patch"

The problem (if not properly done) are uncontrollable distant effects, because these changes are global, and another module in the same runtime using the same "parent" class might be sabotaged by the changes.

Contrary to this subclassing will only effect the subclass.

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

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

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

Replies are listed 'Best First'.
Re^2: Packages with subs using fully qualified class names, not always their own. (monkey patch)
by karlgoethebier (Abbot) on Jul 01, 2017 at 11:30 UTC
    "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

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

        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?