wiki:Overriding

Version 1 (modified by geofft@mit.edu, 13 years ago) (diff)

--

Imagine you've identified some function in the Perl source that you'd like to override for some reason. Given the liberal use of Perl OO throughout the code, this is fairly likely. Assuming your problem is not better solved by CreatingAModule or using some other more public customization interface like writing a new style, how do you do this?

Load some code like this:

no warnings 'redefine';
sub BarnOwl::Message::Zephyr::zsig {
   return "My zsig is " . shift->{zsig};
}

There are a couple of ways to load in code: one is to put it in a file, and run :perl do '/home/me/file.pl'; another is to use Nelson's DevUtils module to get an :eperl command that lets you compose Perl to be evaluated the same way you would compose a new message.

For a practical example of this technique, see Nelson's Alias module, which overrides the "context" property getter on zephyrs (which displays the class name) to allow you to display abbreviated or redacted class names. This makes the replacement compatible with any style, since they all just call ->context.

The two modules referenced can be found in /mit/nelhage/Public/BarnOwl.