And behold, there came the time of Refactored Overloaded Objects
Posted: March 21st, 2004 Comments OffIf
you’re a Perl coder who hasn’t been keeping a close eye on CPAN then
you may have missed the latest chunk of code making quite a stir,
namely Brian “Ingy” Ingerson’s marvellous IO::All. And it is
marvellous: if Perl is the Swiss Army Chainsaw then this is the new
light saber attachment – can’t do anything you couldn’t do previously
but it slices through most IO jobs in one or two lines, from file
slurping (one line, obviously) to creating a forking server (er, two
lines). This Perl.com piece
would be a great introduction if another burst of coding from Ingy
hadn’t rendered it half-obsolete a mere three days later. But, dammit,
that’s what we like to see!
IO::All’s design could be described, for want of a decent OO
education, as “overload one class with a billion different uses” and in
this case it seems to work well. The vast majority of the code revolves
around grabbing code from other modules and wrapping them up in several
big contextualising switches so that this single class is almost all
you need for your to-ing and fro-ing with the outside filesystem. In
other words, it’s all about the interface. It feels very Perlish in its
mixture of minimal code and DWIMness so it’s not surprising that many in the Perl community have jumped on it gleefully.
This “The Best $DOMAIN Functions In The World… Ever!” approach to module-building is infectious, and Yung-Chung Lin’s Var module is probably going to be the first of many imitators. If you fancy having a go yourself, then Ingy’s Spiffy
base class is what you want to start with, but please use your enhanced
exporting powers wisely: packing a single class with tons of functions
(a la Python) is fine and pretty, but doing that to the default
namespace (a la PHP) is just inconsiderate.