Class::Gomor::Hash - class and object builder, hash version |
Class::Gomor::Hash - class and object builder, hash version
# Create a base class in BaseClass.pm package My::BaseClass;
require Class::Gomor::Hash; our @ISA = qw(Class::Gomor::Hash);
our @AS = qw(attribute1 attribute2); our @AA = qw(attribute3 attribute4); our @AO = qw(other);
sub new { shift->SUPER::new(@_) }
My::BaseClass->buildAccessorsScalar(\@AS); My::BaseClass->buildAccessorsArray(\@AA);
sub other { my $self = shift; @_ ? $self->{other} = [ split(/\n/, shift) ] : @{$self->{other}}; }
1;
# Create a subclass in SubClass.pm package My::SubClass;
require My::BaseClass; require Class::Gomor::Hash; our @ISA = qw(My::BaseClass Class::Gomor::Hash);
our @AS = qw(subclassAttribute);
My::SubClass->buildAccessorsScalar(\@AS);
sub new { shift->SUPER::new( attribute1 => 'val1', attribute2 => 'val2', attribute3 => [ 'val3', ], attribute4 => [ 'val4', ], other => [ 'none', ], subclassAttribute => 'subVal', ); }
1;
# A program using those classes
my $new = My::SubClass->new;
my $val1 = $new->attribute1; my @values3 = $new->attribute3; my @otherOld = $new->other;
$new->other("str1\nstr2\nstr3"); my @otherNew = $new->other; print "@otherNew\n";
$new->attribute2('newValue'); $new->attribute4([ 'newVal1', 'newVal2', ]);
This module is yet another class builder. This one adds parameter checking in new constructor, that is to check for attributes existence, and definedness. Since objects are built as hashes, this module is suffixed by Hash.
In order to validate parameters, the module needs to find attributes, and that is the reason for declaring attributes in global variable names @AS, @AA, @AO. They respectively state for Attribute Scalar, Attribute Array and Attribute Other. The last one is used to avoid autocreation of accessors, that is you put in your own ones.
Attribute validation is performed by looking at classes hierarchy, by following @ISA tree inheritance.
The loss in speed by validating all attributes is quite negligeable on a decent machine (Pentium IV, 2.4 GHz), and with Perl 5.8.x.
use Class::Gomor::Hash qw($NoCheck);
If you want to disable checkParams to improve speed once your program is frozen, you can use this variable. Set it to 1 to disable parameter checking.
use Class::Gomor::Hash qw($Debug);
This variable is used by the debugPrint method.
Patrice E<lt>GomoRE<gt> Auffret
=head1 COPYRIGHT AND LICENSE
Copyright (c) 2004-2005, Patrice E<lt>GomoRE<gt> Auffret
You may distribute this module under the terms of the Artistic license. See Copying file in the source distribution archive.
Class::Gomor::Hash - class and object builder, hash version |