NAME
LEOCHARRE::Class - my class bases
METHODS
__init_from_hashref()
argument is a hash ref. optional argument is a list of param names that MUST be present and must be set succesfully if no args are present and none required, returns false if args are required and not present or not set succesfully, confesses, throws exceptions if args are present and all set works, returns true if any set at all fails, returns undef after going through all
we look up inside class_accessors_single, if any are present in the arg.. we set
Example:
package Circus;
use LEOCHARRE::Class;
use LEOCHARRE::Class::Accessors
single => ['name', 'genre'],
multi => ['animals', 'employees'];
sub new {
my($class,$arg) = @_;
$arg ||= {};
my $self = {};
bless $self, $class;
$self->__init_from_hashref($arg);
return $self;
}
1;
package main;
use Circus;
my $c = new Circus({
name => 'Circo Poltergeist',
animals => [qw(elephants tigers turtles cows chickens)],
});
This will have the same effect as:
package main;
use Circus;
my $c = new Circus;
$c->animals_add(qw(elephants tigers turtles cows chickens));
$c->name_set('Circo Poltergeist');
The cool thing you can do though... This is just one creative example
sub load_from_yaml {
my ($self,$abs) = @_;
require YAML;
my $arg = YAML::LoadFile($abs);
$self->__init_from_hashref($arg) or die("some args were not good");
my $self = shift;
}
If you want to throw an exception with explanation if the constructor is missing an argument.. Imagine your object MUST have 'abs_path' as argument to constructor..
sub new {
my($class,$arg) = @_;
$arg ||= {};
my $self = {};
bless $self, $class;
$self->__init_from_hashref($arg, 'abs_path');
return $self;
}
Init from hashref returns undef if any of the set methods fail. It returns 0 if there was nothing that we tried to set in the arguments. Otherwise returns true.
SEE ALSO
LEOCHARRE::Class::Accessors LEOCHARRE::Class::Accessors::Base
AUTHOR
Leo Charre leocharre at cpan dot org