NAME

Class::Gomor::Hash - class and object builder, hash version

SYNPOSIS

# 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', ]);

DESCRIPTION

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.

GLOBAL VARIABLE

$NoCheck

Import it in your namespace like this:

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.

$Debug

Import it in your namespace like this:

use Class::Gomor::Hash qw($Debug);

This variable is used by the debugPrint method.

METHODS

new [ (hash) ]

Object constructor. This is where user passed attributes (hash argument) are checked against valid attributes (gathered by getAccessors 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.

checkParams (scalar, scalar)

The attribute checking method takes two arguments, the first is user passed attributes (as a hash reference), the second is the list of valid attributes, gathered via getAccessors method (as an array ref). A message is displayed and the application dies if not valid.

getIsaTree (scalar)

A recursive method. You pass a class in an array reference as an argument, and then the @ISA array is browsed, recursively. The array reference passed as an argument is increased with new classes, pushed into it.

getAccessors

This method returns available attributes for caller's class. It uses getIsaTree to search recursively in class hierarchy. It then returns an array reference with all possible attributes.

buildAccessorsScalar (scalar)

Accessor creation method. Takes an array reference containing all scalar attributes to create.

buildAccessorsArray (scalar)

Accessor creation method. Takes an array reference containing all array attributes to create.

debugPrint (scalar, scalar)

First argument is a debug level. It is compared with global $Debug, and if it is less than it, the second argument (a message string) is displayed. This method exists because I use it, maybe you will not like it.

AUTHOR

Patrice <GomoR> Auffret

COPYRIGHT AND LICENSE

Copyright (c) 2004-2005, Patrice <GomoR> Auffret

You may distribute this module under the terms of the Artistic license. See Copying file in the source distribution archive.