Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: Safer monkey-patching

by Anonymous Monk
on Jan 19, 2012 at 03:57 UTC ( #948667=note: print w/ replies, xml ) Need Help??

in reply to Safer monkey-patching

For some reason I think this worse than just monkey patching, but I really can't pin down why -- conclusion, whatever floats your boat, its all the same to me

update: After reviewing some of my recent monkey patching efforts (, Re: HTML::TableExtract Memory Usage ), I figured out why I don't like messing with @ISA, it evokes feelings of action at a distance, after all it is one step removed, where as monkey patching is as straight forward as you can get. I also vaguely recall method lookup caching could survive @ISA updates.

So , I think I'll stick to monkey patching :)

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

    Method lookup caching doesn't happen until a method has actually been called. Because the @ISA manipulation happens in a BEGIN block, this should occur before any method calls, thus before any method lookups have been cached.

    Also, according to perlobj any changes to @ISA should invalidate the cache. There may be bugs lurking somewhere in perl's method lookup caching mechanism, but none that seem to be triggered by the technique I've discussed.

    After all, the situation where package $X manipulates package $Y's <c>@ISA at BEGIN-time is really common. (Consider the case where $X eq "base" or $X eq "parent". Sure, this is happening at package $Y's request, but from Perl's perspective the same thing is happening.)

    With Moose it happens at run-time, and this doesn't seem to cause any problems.

      ... BEGIN ... perlobj ... parent ... perspective ..

      By the time you load Data::Dumper or whatever, resolution method could have happened

      But yeah, I did say I vaguely recall, esp because I didn't feel like digging up history or pinning down the details :)

      As for parent... perspective is exactly the point. If you're bothering to write a module to share, you might as well do it right, which is not dickering around with other peoples class data :) delegate (AUTOLOAD or ...)

      At least if you monkey patch, warnings will issue "Subroutine %s redefined"

      With Moose it happens at run-time, and this doesn't seem to cause any problems.

      Sure it does :) Moose Cookbook - Meta Recipe 5 Won't Run

Re^2: Safer monkey-patching
by JavaFan (Canon) on Jan 19, 2012 at 11:06 UTC
    I also vaguely recall method lookup caching could survive @ISA updates.
    It can! If your perl is old enough. I think this bug got fixed in 5.004 or 5.005.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (11)
As of 2016-02-08 13:16 GMT
Find Nodes?
    Voting Booth?

    How many photographs, souvenirs, artworks, trophies or other decorative objects are displayed in your home?

    Results (276 votes), past polls