HO::class - class builder for hierarchical objects
package Foo::Bar; use subs 'init'; use HO::class _lvalue => hey => '@', _method => huh => sub { print 'go' }, _rw => bla => '%', _ro => foo => '$', alias => foobar => 'foo'; sub init { my ($self,@args) = @_; ... return $self; }
This is a simple class builder for array based objects. Normally it does its job during compile time. A constructor new is build. The generated new will initialize each member with an appropriate default value.
The method init is reserved for setting up objects during construction. This method gets the fresh build object, and the arguments given calling new. A little questionable optimization is that the call to init is not build into the constructor when no such method exists or the option init is not part of HO::class-\import> call.
init
new
HO::class-\
For that reason the pragma subs is often used, before HO::class.
subs
HO::class
Five different keys could be used, to define different accessors.
_rw
The generated accessor can read and write the data.
_ro
The accessor is for read access only.
_lvalue
_method
_index
The second field is name of the part from class which will be created. Third field is used for datatype or code references.
For this the keys _ro and _rw exists. How the accessor is defined depends on the third argument. The datatypes are defined in HO::accessor class in the global %type hash.
%type
It is possible to build class at runtime. The easiest way to do this, is calling HO::class-\import>. At runtime the caller is commonly not the wanted class name. For that reason the global variable $HO::accessor::class is used.
$HO::accessor::class
{ local $HO::accessor::class = 'My::Class'; HO::class->import(_ro => acc => '$', init => 'hash'); } my $obj = My::Class->new(acc => 'data');
You can change methods for an object if you overwrite the method using the index.
package H::first; use HO::class _method => hw => sub { 'Hallo Welt!' }; my $o2 = H::first->new; is($o2->hw,'Hallo Welt!'); # ok $o2->[$o2->_hw] = sub { 'Hello world!' } is($o2->hw,'Hello world!'); # ok
How you can see, it is quite easy to do this in perl. Here during class construction you have to provide the default method, which is used when the object does not has an own method.
The method name can be appended with an additional parameter static separated by a colon. This means that the default method is stored in an additional slot in the object. So it is changeable on per class base. This is not the default, because the extra space required.
static
use HO::XML _method => namespace:static => sub { undef }
Currently the word behind the colon could be free choosen. Only the existence of a colon in the name is checked.
Development started because there was no class builder for array based objects with all the features I needed.
Sebastian Knapp, <news@young-workers.de>
Copyright (C) 2007-2017 by Sebastian Knapp
You may distribute this code under the same terms as Perl itself.
To install HO::class, copy and paste the appropriate command in to your terminal.
cpanm
cpanm HO::class
CPAN shell
perl -MCPAN -e shell install HO::class
For more information on module installation, please visit the detailed CPAN module installation guide.