LEOCHARRE::Class::Accessors::Base - particular setget methods
package Super::Hero; use LEOCHARRE::Class::Accessors::Base; __PACKAGE__->_make_new(); __PACKAGE__->_make_accessor_single('name','age','force'); __PACKAGE__->_make_accessor_multi('powers'); __PACKAGE__->_make_accessor_dual('power'); # or _make_new('Super::Hero'); _make_accessor_single('Super::Hero','name','age','force'); _make_accessor_multi('Super::Hero','powers'); _make_accessor_dual('Super::Hero','power_selected'); 1;
In script.pl:
use Super::Hero; my $sm = new Super::Hero; # set methods $sm->name_set('Superman'); $sm->powers_add('flying','x ray vision'); $sm->power_selected('punching'); # multi get methods my $powers = $sm->powers; my $powers = $sm->powers_arrayref; my $powers = $sm->powers_arrayref_sorted; my $powers = $sm->powers_sorted; # single get methods my $name = $sm->name; my $name = $sm->name_get; # more my $count_of_powers = $sm->powers_count; $sm->powers_exists('flying'); $sm->powers_delete('flying'); # get hash of all instance data this is managing my $instance_data = $sm->_instancedata; # what are the single methods? @Super::Hero::__accessors_single; # what are the multi methods? @Super::Hero::__accessors_multi; # what are all methods made? @Super::Hero::__accessors; # or $sm->class_accessors; $sm->class_accessors_multi; $sm->class_accessors_single;
These are my personal set get methods. There are two types of acessors, multi, and single.
getting a list of accessors set
@__PACKAGE__::__accessors_single;
a multi accessor is a list of unique strings. you can ask for a count of elements, add elements, delete them, test for the existance of an element in the list as well. the accessor names should be plural, as we want to deal with a list.
if the accessor's name is 'jobs' you are provided with
jobs - get array ref, in the order they were added jobs_arrayref - same as jobs __jobs_arrayref - if you want to redefine jobs and or jobs_arrayref jobs_arrayref_sorted - just like jobs, but returns in alphanum order jobs_sorted - same as jobs_arrayref_sorted jobs_hashref - get hashref, mostly unused other then internally jobs_count - returns number of elements jobs_add - add value, if you add the same twice, changes nothing __jobs_add - if you want to redefine jobs_add jobs_delete - take out value jobs_exists - if value is present or not jobs_clear - clear the values added
you may want to write our own add, this is how:
sub jobs_add { my ($self,$val) = @_; $self->is_job($val) or return 0; $self->__jobs_add($val); return 1; }
We are not really overriding anything, we are really redefining, that is why you cannot use SUPER. Beacause using LEOCHARRE::Class::Accessors creates methods in the namespace of the caller.
a single accessor is for one value within an object instance instead of the common perl set and get this is how I chose my method names.. if the accessor is named "age" you are provided with
age - get method age_get - get method age_set - set method __age_set - if you want to redefine age_set age_clear - undef the value
You may want to override(really defefine) the set method.. but still want access to it. Maybe you want to verify the data..
no warnings 'redefine'; sub age_set { my ($self,$val) = @_; $self->_agevalok($val) or return 0; $self->__age_set($val); return 1; }
these are the traditional setget methods, they both set and get they always return the value
$self->case or $self->case('papers');
$LEOCHARRE::Class::Accessors::Base::DEBUG = 1;
Leo Charre leocharre at cpan dot org
To install LEOCHARRE::Class, copy and paste the appropriate command in to your terminal.
cpanm
cpanm LEOCHARRE::Class
CPAN shell
perl -MCPAN -e shell install LEOCHARRE::Class
For more information on module installation, please visit the detailed CPAN module installation guide.