Monkey-patching, subclassing, and accidental overriding
By Aaron Crane (arc) from Edinburgh.pm, London.pm
Date: Tuesday, 16 August 2011 11:40
Duration: 20 minutes
Target audience: Intermediate
Language:
Tags: cpan language moose oo reuse
We all know that CPAN is one of Perl's biggest advantages. But sometimes, you find yourself using a CPAN class that doesn't have quite enough features for what you're trying to do. What's the best way to deal with that sort of situation?
One option would be to monkey-patch new code into the CPAN class you're using — just add extra subroutines to the original namespace. But unconstrained monkey-patching has consequences that make it extremely hard to use in practice. So the usual alternative recommendation is to subclass the CPAN code, add the new methods in the subclass, and then ensure that the rest of your program always uses the subclass in place of the original. But that approach has two flaws. First, it can be awkward to make sure your subclass is always used in the right places. Second, it doesn't actually fix the problem: you can still experience all the same issues as with monkey-patching!
This talk examines the problems with this sort of reuse, whether you're trying to use monkey-patching or subclassing. It also offers some solutions that take advantage of Perl's flexibility to make it both easy and safe, regardless of whether you're using Perl's built-in OO or a full-featured object system like Moose.
- Damian Conway (damian)
- Leon Brocard (acme)
- Aaron Crane (arc)
- Tadeusz Sośnierz (tadzik)
- Andreas Vögele
- Patrick Mevzek
- Zefram
- Clive Darke (cdarke)
- Aliaksandr Zahatski (zag)
- Oskari Ojala (Okko)
- Ben Tisdall (bentis)
- Nikolay Mishin (mishin)
- Jose Luis Martinez
- Markus Pinkert (Bedivere)
- Tudor Constantin
- Daniel Blom
- roger blom
- Henrik Hald Nørgaard
- Teemu Nuutinen
- Norbert Gruener
- Dave Sherohman (dsheroh)
- Noah Kogler
- Ulrich Wisser (wisser)
- Iulia Bublea
- Zbigniew Lukasiak (zby)
- Alexander Orlovsky (nordicdyno)
- Trond Michelsen (trondmm)
- Heinz Knutzen
- Ben Martin
- Chad Davis (chadadavis)
- Smylers
- Moritz Onken (mo)
- Magnus Zeisig (magnuz)
- Matthias Zeichmann
- Francoise Dehinbo (franky)
- Ahti Nurminen (ade)
- Jon Jensen (jon_jensen)
- Jørgen Elgaard Larsen (elhaard)
- Adam Taylor (adam-_-)
- Lars Thegler (tagg)
- Lars Holgaard
- Karsten Silkenbaeumer (ksi)
- Łukasz Siemiradzki (plluksie)
- Sue Mynott (virtualsue)
- Lisa Hansen (iamlgh)
- Daniel Böhmer
- Olle Johansson (ollej)
- Henrik Andersen (HEM)
- Sébastien Aperghis-Tramoni (maddingue)
- Tomasz Czepiel (tjmc)
- Alex Muntada (alexm)
- Alba Ferrer (alba)
- Nikolay Morev