Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Granted, the object-orientation [in Perl] felt more like a hack than a normal language feature (because it was one)...

Perl's OOP is definitely a hack! But it's a clever and elegant hack, and younger languages can learn a lot from studying how an awk++ managed to evolve support for all these new programming paradigms, without being totally co-opted by the ones which turned out to be fads. (When these languages are Perl's age, will they have evolved as successfully?)

Take OOP support in Perl. Prior to that, Perl had references, which could point to other basic value types, and it had packages, which were namespaces in the symbol table. Only two small changes were necessary for OOP.

First, a new built-in function `bless` was added which marked a reference as associated with a certain package. Second, a new binary operator for dispatch was added, the familiar `->` from other languages. The dispatch operator looked up a package from the reference on the left hand side, looked up a symbol within that package given by the right hand side, and pushed the reference onto the front of the argument list in the subsequent function call. In the called function, the initial reference argument became the equivalent of `this`, by convention.

Modulo some discussion of @ISA, that's it. You now understand Perl's implementation of OOP.

The elegance of this approach finally hit home for me when building transparent RPC for Perl and other languages. On the client side, when you dispatch a function call on a "stub" object, you'd like to dispatch a real function call on a remote object. In Python, you have to worry about whether you're calling a "bound function" on the remote object, versus calling a static method of the remote object, versus calling a "free function" defined at module scope (how do you address these?), and so on. In Perl, these are all effectively the same thing because the dispatch operator is so simple, and there's no extra OOP structure baked into the language itself like there is with Python.

Disclaimer: I mostly write Python 2.x now, but things like the complicated object runtime and the general uselessness of lambdas strike me as places where the language has stumbled.



Perl OOP is a hack, but coming from university where they taught Java, suddenly it made the whole thing obvious and transparent (to me at least).

Inheritance was no longer a black box, but just a way of working out where to look for the method and execute it.


Great answer, thanks for explaining this! I'm curious about how much this approach differs from the one pursued by Javascript, which seems pretty similar to me.


Javascript lacks the "packages" part of the mix, it uses Prototypal inheritance rather than Class based inheritance.

If you squint you can view prototypal inheritance as class-based-inheritance-where-everything-is-a-singleton ... but you really need some alcohol to deal with the headache you get from squinting that much.


> ... the general uselessness of lambdas ...

This sounds interesting. Why do you feel this way?


'sigil' referenced Python with "uselessness of lambdas", not Perl.

(If you still wonder -- from your comment history you shouldn't -- try Google: python lambda broken)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: