Do I have any expert Perl hackers in the audience today?
I’ve been rereading some of what Jonathan Rees has to say about the object oriented programming menu. I’ve only read this stuff about a half-dozen times before, and I’ve been reading about the Perl object model recently, so I decided to get another look at what JAR has to say about the definition of OOP.
In reading about the Perl object model, I was disappointed. My excited utterance, as I learn new things about it, is this:
This is in stark contrast to my response when reading about lexical closures in Perl. Perl closures made me jump up and shout “Eureka!” (or do something a touch more dignified and me-ish, while mentally I was cavorting and shouting Greek words). With much consideration of what the Perl object model attempts to accomplish, how it relates to other languages’ object models, what I know of how CLOS was put together for doing OOP in Lisp (apparently a lot of reliance on lexcial closures is involved), and what JAR has to say about the malleable definition of OOP, I’ve come to some interesting continuations (since “conclusions” is kinda final, and I’m still in the middle) about the potential for OOP in Perl.
I have a notion that, though the object model would be fairly foreign to people coming to Perl from C++ or Java, Perl has an unofficial object model in place sorta “behind” or alongside its official object model, and it’s pretty much entirely made up of leverage applied to its lexical closures. Making use of Perl closures for object-oriented programming while entirely ignoring bless() seems to provide a pretty significant chunk of the a la carte OOP menu JAR proposes. It appears to satisfy the needs of the following points:
- encapsulation — syntactically hide implementations of objects
- protection — maintain a consistent API for objects by keeping client code ignorant of its implementation
- AYCDISAM — All You Can Do (to an object) Is Send (it) A Message
- inheritance — I’m a little sketchy on what JAR means by the two different types of inheritance he mentions, but it seems that “object oriented” inheritance is definitely possible with Perl lexical closures.
I could be wrong about a couple of those, but then some of the others could also apply. I’m not really sure. I ask for expert Perl hackers because I’d like to know where I’m wrong.
What is clearly right, however, is the fact that at least some form of object model exists in Perl via lexical closures even without bless() in the allowed toolkit. Something else that is clear, at least from where I’m sitting right now, is that this object model is much cleaner to use than the official Perl OOP tools that come to mind when someone talks about doing OOP with Perl. It’s entirely possible that some things one might wish to do with OOP might not be possible, or at least reasonably easy, without the official object model, but so far I don’t really see it. I think the major problem is simply that a Perl closure object model would be so alien to most Perl hackers (coming from a VB, PHP, C++, and Java backround for OOP, or even from Python, most likely) that they’d have to reorient their brains to adjust to the very different approach to OOP that lexical closures provide.
Seriously, folks, I’m sure I’m missing something. Please explain.