NAME Homer - Simple prototype-based object system SYNOPSIS use Homer; # create a prototype object my $person = Homer->new( first_name => 'Generic', last_name => 'Person', say_hi => sub { my $self = shift; print "Hi, my name is ", $self->first_name, ' ', $self->last_name, "\n"; } ); # create a new object based on it my $homer = $person->extend( first_name => 'Homer', last_name => 'Simpson' ); $homer->say_hi; # prints 'Hi, my name is Homer Simpson' # let's extend even more my $bart = $homer->extend( first_name => 'Bart', father => sub { print "My father's name is ", $_[0]->prot->first_name, "\n" } ); $bart->say_hi; # prints 'Hi, my name is Bart Simpson' $bart->father; # prints "My father's name is Homer" DESCRIPTION "Homer" is a very simple prototype-based object system, similar to JavaScript. In a prototype based object system there are no classes. Objects are either directly created with some attributes and methods, or cloned from existing objects, in which case the object being cloned becomes the prototype of the new object. The new object inherits all attributes and methods from the prototype. Attributes and methods can be overridden, and new ones can be added. The new object can be cloned as well, becoming the prototype of yet another new object, thus creating a possibly endless chain of prototypes. Prototype-based objects can be very powerful and useful in certain cases. They can provide a quick way of solving problems. Plus, sometimes you just really need an object, but don't need a class. I like to think of prototype-based OO versus class-based OO as being similar to schema-less database systems versus relational database systems. "Homer" is a quick and dirty implementation of such a system in Perl. As Perl is a class-based language, this is merely a hack. When an object is created, "Homer" creates a specific class just for it behind the scenes. When an object is cloned, a new class is created for the clone, with the parent object's class pushed to the new one's @ISA variable, thus providing inheritance. I can't say this implementation is particularly smart or efficient, but it gives me what I need and is very lightweight ("Homer" has no non-core dependencies). If you need a more robust solution, Class::Prototyped might fit your need. HOMER AT A GLANCE * Prototypes are created by calling "new()" on the "Homer" class with a hash, holding attributes and methods: my $prototype = Homer->new( attr1 => 'value1', attr2 => 'value2', meth1 => sub { print "meth1" } ); $prototype->attr1; # value1 $prototype->attr2; # value2 $prototype->meth1; # prints "meth1" * A list of all pure-attributes of an object (i.e. not methods) can be received by calling "attributes()" on the object. $prototype->attributes; # ('attr1', 'attr2') * Every object created by Homer can be cloned using "extend( %attrs )". The hash can contain new attributes and methods, and can override existing ones. my $clone = $prototype->extend( attr2 => 'value3', meth2 => sub { print "meth2" } ); $clone->attr1; # value1 $clone->attr2; # value3 $clone->meth1; # prints "meth1" $clone->meth2; # prints "meth2" * Objects based on a prototype can refer to their prototype using the "prot()" method: $clone->prot->attr2; # value2 * All attributes are read-write: $clone->attr1('value4'); $clone->attr1; # value4 $clone->prot->attr1; # still value1 * New methods can be added to an object after its construction. If the object is a prototype of other objects, they will immediately receive the new methods too. $prototype->add_method('meth3' => sub { print "meth3" }); $clone->can('meth3'); # true * New attributes can't be added after construction (for now). * Cloned objects can be cloned too, creating a chain of prototypes: my $clone2 = $clone->extend; my $clone3 = $clone2->extend; $clone3->prot->prot->prot; # the original $prototype CONSTRUCTOR new( [ %attrs ] ) Creates a new prototype object with the provided attributes and methods (if any). CONFIGURATION AND ENVIRONMENT "Homer" requires no configuration files or environment variables. DEPENDENCIES None other than Carp. BUGS AND LIMITATIONS Please report any bugs or feature requests to "bug-Homer@rt.cpan.org", or through the web interface at . SUPPORT You can find documentation for this module with the perldoc command. perldoc Homer You can also look for information at: * RT: CPAN's request tracker * AnnoCPAN: Annotated CPAN documentation * CPAN Ratings * Search CPAN AUTHOR Ido Perlmuter LICENSE AND COPYRIGHT Copyright (c) 2014, Ido Perlmuter "ido@ido50.net". This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either version 5.8.1 or any later version. See perlartistic and perlgpl. The full text of the license can be found in the LICENSE file included with this module. DISCLAIMER OF WARRANTY BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.