InsideOutClass - A set of example metaclasses which implement the Inside-Out technique
package Foo; use metaclass 'InsideOutClass' => ( # tell our metaclass to use the # InsideOut attribute metclass # to construct all it's attributes ':attribute_metaclass' => 'InsideOutClass::Attribute' ); __PACKAGE__->meta->add_attribute('foo' => ( reader => 'get_foo', writer => 'set_foo' )); sub new { my $class = shift; $class->meta->new_object(@_); } # now you can just use the class as normal
This is a set of example metaclasses which implement the Inside-Out class technique. What follows is a brief explaination of the code found in this module.
First step is to subclass Class::MOP::Class and override the construct_instance method. The default construct_instance will create a HASH reference using the parameters and attribute default values. Since inside-out objects don't use HASH refs, and use package variables instead, we need to write code to handle this difference.
construct_instance
The next step is to create the subclass of Class::MOP::Attribute and override the method generation code. This requires overloading generate_accessor_method, generate_reader_method, generate_writer_method and generate_predicate_method. All other aspects are taken care of with the existing Class::MOP::Attribute infastructure.
generate_accessor_method
generate_reader_method
generate_writer_method
generate_predicate_method
And that is pretty much all. Of course I am ignoring need for inside-out objects to be DESTROY-ed, and some other details as well, but this is an example. A real implementation is left as an exercise to the reader.
DESTROY
Stevan Little <stevan@iinteractive.com>
Copyright 2006 by Infinity Interactive, Inc.
http://www.iinteractive.com
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Class::MOP, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Class::MOP
CPAN shell
perl -MCPAN -e shell install Class::MOP
For more information on module installation, please visit the detailed CPAN module installation guide.