|Perl Monk, Perl Meditation
Fun with Closuresby Schemer (Scribe)
|on Mar 25, 2003 at 02:13 UTC
While thinking about programming languages in a half-sleep the other day I found myself wondering about closures and objects.
I have a somewhat favorite language (reflected by my nickname) which allows easy generation and manipulation of closures. Often I have heard things about objects and closures being related.
Since my Perl knowledge is somewhat limited (I own the Camel book, but never read it in full), I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
First I will pre-emptively apologize for the somewhat confusing "object" term I use, and admit I don't know much about Perl's real object system.
Ok, so I ended up with the following thing, which is basically a simple object system (message passing):
Basically, this creates an object that only has one "method" initially, which allows for the addition of other methods or slots ("member values"). Methods added are internally stored in a hash which is local to the object's closure.
Here is some example code using this thing:
So what's the value of all of this? Well, I don't think this code could be of some use in practice, since it is more like a toy example and the syntax just doesn't cut it. But at the very least I had some fun writing it, and hopefully it can be seen as a demonstration of what closures allows you to do.
Comments, thoughts and/or insults would be appreciated! :-)