Don't ask to ask, just ask | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
I, like many people (See the number of Class::* modules on CPAN), have sought to make oop easier in perl. After looking at many different ideas including closure objects, inside-out objects, and Class::Struct I came up with my own way of creating objects that looks something like this:
My goals were to make the syntax easy without using source filters, to ensure encapsulation, to allow inheritance, to make access to instance variables inside of methods easy, and to have as little overhead as possible. I call it a functional object because the actually object can be thought of a collection of functions that share some lexical variables, more than a collection of variables that you can call methods on. I call it an inside out object because if you look at the source code for FIOC.pm below, you can see that the functions are actually stored in a hash outside of object and the object is just a key to those hashes like in Abigail-II's inside out objects. It is called a closure object because the functions that make up an object are all closures.
I think I made my goal of making the syntax simple. Class::FIOC imports just one method "methods" which takes a hash of method names and subs and returns a blessed object. I think I made my goal of encapsulation because there is no easy way to access the instance variables and by using an inside-out design for the functions, it makes it difficult to mess with them. I haven't developed the inheritance part very much, but I think because the objects themselves are just keys I could probably modify it so objects blessed as other things could inherit from these objects. The one thing that I am not sure about is the overhead part. It is my understanding that when closures are used that the different version of a closure share the same code but just use a different set of lexicals. So I think that the overhead for an individual object should just be the set of lexicals plus the references to the CODE objects. Is this a correct understanding of how closures work? My second question about overhead is the use of the "goto" in the methods (See the source for "methods" below). The documentation for goto says: I interperted this as this form of goto does not have the same overhead as other forms of goto. I figure that if there is a little overhead that cost can be made up by the fact that inside the actual method there is no overhead for looking up an instance variable in a hash table or array. Is this a good assumption? Finally, I would like to know what my fellow monks think of this idea. It still needs some fleshing out, but I would like to know if there are some glaring flaws I have overlooked before I work on it anymore. Thanks!
In reply to Functional Inside Out Closure Objects by fletcher_the_dog
|
|