I’m trying to monkey patch a Perl class: I want to change the behavior of an existing method.
This node on perlmonks shows how to add a function to an existing class. I found that this pattern can also be used to provide a new implementation for an existing function.
However, I’d like to know how to call the original function.
I’m looking for something like this:
use ExistingClass; # TODO: Somehow rename existingFunction() to oldExistingFunction(). sub ExistingClass::existingFunction { my $self = shift; # New behavior goes here. $self->oldExistingFunction(@_); # Call old behavior. # More new behavior here. }
Typeglob assignment
Quick and dirty. This aliases all
existingFunctionsymbols tooldExistingFunction. This includes the sub you’re interested in, but also any scalars, arrays, hashes, handles that might happen to have the same name.Coderef assignment
That one only aliases the sub. It’s still done in the package stash, so the
oldExistingFunctionsymbol is globally visible, which might or might not be what you want. Probably not.Lexical coderef
Using
mykeeps a reference to the old function that is only visible to the currrent block/file. There is no way for external code to get hold of it without your help anymore. Mind the calling convention:Moose
See jrockway’s answer. It’s got to be The Right Way, since there’s no mucking around with globs and/or references anymore, but I don’t know it enough to explain it.