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);
# You should initialize yourself array attributes
sub new { shift->SUPER::new(attribute3 => [], attribute4 => [], @_) }
# Create accessors
My::BaseClass->cgBuildAccessorsScalar(\@AS);
My::BaseClass->cgBuildAccessorsArray(\@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;
our @ISA = qw(My::BaseClass);
our @AS = qw(subclassAttribute);
My::SubClass->cgBuildAccessorsScalar(\@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 class is a subclass from
Class::Gomor. It implements objects as hash
references, and inherits methods from
Class::Gomor.
See
Class::Gomor.
-
new (hash)
- Object constructor. This is where user passed attributes
(hash argument) are checked against valid attributes (gathered by
cgGetAttributes method). Valid attributes are those that exists
(doh!), and have not an undef value. The default is to check this, you can
avoid it by setting $NoCheck global variable (see
perldoc Class::Gomor).
- cgBuildIndices
- This method does nothing. It only exists to make it more
easy to switch between Class::Gomor::Array and
Class::Gomor::Hash.
-
cgBuildAccessorsScalar (array ref)
-
cgBuildAccessorsArray (array ref)
- See Class::Gomor.
-
cgGetIndice (scalar)
- This method does nearly nothing. It only returns the
passed-in scalar parameter (so the syntax is the same as in
Class::Gomor::Array). It only exists to make it more easy to switch
between Class::Gomor::Array and Class::Gomor::Hash.
-
cgClone [ (scalar) ]
- You can clone one of your objects by calling this method.
An optional parameter may be used to create multiple clones. Cloning will
occure only on the first level attributes, that is, if you have attributes
containing other objects, they will not be cloned.
-
cgFullClone [ (scalar) ]
- This method is the same as cgClone, but will clone
all attributes recursively, but only if they are subclassed from
Class::Gomor. So, objects created with other modules than
Class::Gomor::Array or Class::Gomor::Hash will not be
cloned.
Another thing to note, there is no catch for cycling references (when you
link two objects with each others). You have been warned.
- cgDumper
- Will return a string as with Data::Dumper Dumper
method. This is less useful for hashref objects, because they already
include attributes names.
Class::Gomor
Patrice <GomoR> Auffret
Copyright (c) 2004-2015, Patrice <GomoR> Auffret
You may distribute this module under the terms of the Artistic license. See
LICENSE.Artistic file in the source distribution archive.