Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: Safer monkey-patching

by moritz (Cardinal)
on Jan 19, 2012 at 05:34 UTC ( #948687=note: print w/replies, xml ) Need Help??

in reply to Safer monkey-patching

The big problem with monkey patching is that no two modules can safely monkey-patch the same third module. Recently there was a rather drastic example in the Ruby community when two large libraries or frameworks added incompatible json methods to Class (or was it Object? dunno...), and that meant you couldn't use these two libraries in the same process.

Your approach doesn't solve this problem. And it can't, because it's not reasonably solvable.

I might add that source filters suffer from the same problem as monkey patching: you can't combine two source filters fiddling with the same piece of syntax.

Replies are listed 'Best First'.
Re^2: Safer monkey-patching
by tobyink (Abbot) on Jan 19, 2012 at 09:18 UTC

    Indeed, this problem is not really solvable. However, my solution does offer a partial way forward.

    Let's imagine that My::AwesomeError and Your::GroovyError both superclass Example::Error, and both add an asplode method.

    We can then do:

    my $err = Example::Error->new('Heat death of universe detected!'); $err->My::AwesomeError::asplode(); $err->Your::GroovyError::asplode(); $err->asplode(); # Russian roulette

    With true monkey patching, where My::AwesomeError and Your::GroovyError each defined the asplode method directly in the Example::Error namespace, we wouldn't get the option to do the above.

      We can then do:
      my $err = Example::Error->new('Heat death of universe detected!'); $err->My::AwesomeError::asplode(); $err->Your::GroovyError::asplode(); $err->asplode(); # Russian roulette

      Of course you can, but it's not really a benefit. As you wrote in the OP, monkey patching is often done to work around limitations in existing code that you don't want to patch. But writing $obj->Class::method() requires the author of that code to be aware of the issue, and when he is, he can simply write Class::method($obj) without requiring any monkey patching.

      I do find your idea cute, but it really doesn't solve the "hard" problem, and the small problems it does solve seem to be either artifically constructed, or non-issues.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://948687]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2017-11-19 16:33 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (282 votes). Check out past polls.