Class::Accessor::Complex - arrays, hashes, booleans, integers, sets and more
package MyClass; use base 'Class::Accessor::Complex'; MyClass ->mk_new ->mk_array_accessors(qw(an_array)), ->mk_hash_accessors(qw(a_hash)), ->mk_integer_accessors(qw(an_integer)), ->mk_class_hash_accessors(qw(a_hash)), ->mk_set_accessors(qw(testset)), ->mk_object_accessor(an_object => 'Some::Foo', qw(do_this do_that));
This module generates accessors for your class in the same spirit as Class::Accessor does. While the latter deals with accessors for scalar values, this module provides accessor makers for arrays, hashes, integers, booleans, sets and more.
As seen in the synopsis, you can chain calls to the accessor makers. Also, because this module inherits from Class::Accessor, you can put a call to one of its accessor makers at the end of the chain.
This section describes the accessor makers offered by this module, and the methods it generates.
Takes one simple string argument and creates a constructor. The constructor accepts named arguments (that is, a hash) and will set the hash values on the accessor methods denoted by the keys. For example,
package MyClass; use base 'Class::Accessor::Complex'; MyClass->mk_new; package main; use MyClass; my $o = MyClass->new(foo => 12, bar => [ 1..5 ]);
is the same as
my $o = MyClass->new; $o->foo(12); $o->bar([1..5]);
The constructor will also call an init() method, if there is one.
init()
Takes a single string or a reference to an array of strings as its argument. For each string it creates methods as described below, where * denotes the slot name.
*
This method returns the list of values stored in the slot. If any arguments are provided to this method, they replace the current list contents. In an array context it returns the values as an array and in a scalar context as a reference to the array. Note that this reference is currently a direct reference to the storage; changes to the storage will affect the contents of the reference, and vice-versa. This behaviour is not guaranteed; caveat emptor.
*_push
Pushes the given elements onto the end of the array. Like perl's push().
push()
*_pop
Pops one element off the end of the array. Like perl's pop().
pop()
*_shift
Shifts one element off the beginning of the array. Like perl's shift().
shift()
*_unshift
Unshifts the given elements onto the beginning of the array. Like perl's unshift().
unshift()
*_clear
Deletes all elements of the array.
*_count
Returns the number of elements in the array.
*_set
Takes a list, treated as pairs of index => value; each given index is set to the corresponding value. No return.
Called with no arguments returns the hash stored in the slot, as a hash in a list context or as a reference in a scalar context.
Called with one simple scalar argument it treats the argument as a key and returns the value stored under that key.
Called with one array (list) reference argument, the array elements are considered to be be keys of the hash. x returns the list of values stored under those keys (also known as a hash slice.)
Called with one hash reference argument, the keys and values of the hash are added to the hash.
Called with more than one argument, treats them as a series of key/value pairs and adds them to the hash.
*_keys
Returns the keys of the hash.
*_values
Returns the list of values.
*_exists
Takes a single key and returns whether that key exists in the hash.
*_delete
Takes a list and deletes each key from the hash.
Resets the hash to empty.
Takes a single string or a reference to an array of strings as its argument. For each string it creates methods like those generated with mk_hash_accessors(), except that it is a class hash, i.e. shared by all instances of the class.
mk_hash_accessors()
When called, it either dies (if Error::Hierarchy is not installed) or throws an exception of type Error::Hierarchy::Internal::AbstractMethod (if it is installed).
If given a true value - in the Perl sense, i.e. anything except undef, 0 or the empty string - it sets the slot's value to 1, otherwise to 0. If no argument is given, it returns the slot's value.
undef
0
1
set_*
Sets the slot's value to 1.
clear_*
Sets the slot's value to 0.
MyClass->mk_integer_accessors(qw(some_counter other_index));
Takes a list of accessor base names (simple strings). For each string it creates methods as described below, where * denotes the accessor base name.
A basic getter/setter that stores an integer value. Actually, it can store any value, but when read back, it returns 0 if the value is undef.
*_reset
Resets the slot's value to 0.
*_inc
Increments the value, then returns it.
*_dec
Decrements the value, then returns it.
Example:
package Foo; use base 'Class::Accessor::Complex'; Foo->mk_integer_accessors(qw(score));
Then:
my $obj = Foo->new(score => 150); my $x = $obj->score_inc; # is now 151 $obj->score_reset; # is now 0
A set is different from a list in that it can contain every value only once and there is no order on the elements (similar to hash keys, for example).
If called without arguments, it returns the elements in the set. If called with arguments, it puts those elements into the set. As such, it is a wrapper over *_insert() and *_elements().
*_insert()
*_elements()
*_insert
Inserts the given elements (arguments) into the set. If you pass an array reference as the first argument, it is being dereferenced and used instead.
*_elements
Returns the elements in the set.
Removes the given elements from the list. The order in which the elements are returned is not guaranteed.
Empties the set.
*_contains
Given an element, it returns whether the set contains the element.
*_is_empty
Returns whether or not the set is empty.
*_size
Returns the number of elements in the set.
Takes as arguments, in the given order, the name of the accessor to be created, the class whose objects that accessor accepts as values, and an optional list of methods to forward to the object stored in that accessor.
If the accessor is supplied with an object of an appropriate type, will set set the slot to that value. Else, if the slot has no value, then an object is created by calling new() on the appropriate class, passing in any supplied arguments.
new()
The stored object is then returned.
For example:
package MyClass; use base 'Class::Accessor::Complex'; MyClass ->mk_new ->mk_object_accessor(an_object => 'Some::Foo', qw(do_this do_that)); package main; use MyClass; my $o = MyClass->new; my $t = $o->an_object(text => 'foobar'); # $t is now the Some::Foo object whose text() accessor is 'foobar' # the following call to do_that() is forwarded onto the Some::Foo object # stored in $o->an_object() $o->do_that;
If you talk about this module in blogs, on del.icio.us or anywhere else, please use the classaccessorcomplex tag.
classaccessorcomplex
No bugs have been reported.
Please report any bugs or feature requests to bug-class-accessor-complex@rt.cpan.org, or through the web interface at http://rt.cpan.org.
bug-class-accessor-complex@rt.cpan.org
See perlmodinstall for information and options on installing Perl modules.
The latest version of this module is available from the Comprehensive Perl Archive Network (CPAN). Visit <http://www.perl.com/CPAN/> to find a CPAN site near you. Or see <http://www.perl.com/CPAN/authors/id/M/MA/MARCEL/>.
Marcel Grünauer, <marcel@cpan.org>
<marcel@cpan.org>
Copyright 2007 by Marcel Grünauer
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Class::Accessor::Complex, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Class::Accessor::Complex
CPAN shell
perl -MCPAN -e shell install Class::Accessor::Complex
For more information on module installation, please visit the detailed CPAN module installation guide.