The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Class::Accessor::Complex - arrays, hashes, booleans, integers, sets and more

SYNOPSIS

  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));

DESCRIPTION

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.

ACCESSORS

This section describes the accessor makers offered by this module, and the methods it generates.

mk_new

Takes one simple string argument and creates a constructor of that name, or new, if the string is not given. 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.

mk_singleton

Takes one simple string argument and creates a singleton constructor of that name, or new, if the string is not given.

This constructor only ever returns a single instance of the class. That is, after the first call, repeated calls to this constructor return the same instance. Note that the instance is instantiated at the time of the first call, not before. Any arguments are treated as for mk_new(). Naturally, init() and any initializer methods are called only by the first invocation of this method.

mk_scalar_accessors

Takes an array of strings as its argument. For each string it creates methods as described below, where * denotes the slot name.

*

This method can store a value in a slot and retrieve that value. If it receives an argument, it sets the value. Only the first argument is used, subsequent arguments are ignored. If called without a value, the method retrieves the value from the slot.

*_clear, clear_*

Clears the value by setting it to undef.

mk_concat_accessors

Takes an array of strings as its argument. For each string it creates methods as described below, where * denotes the slot name.

*

Like mk_scalar_accessors(), but passing a value to the accessor doesn't clear out the original value, but instead concatenates the new value to the existing one. Thus, this kind of accessor is only good for plain scalars.

*_clear, clear_*

Clears the value by setting it to undef.

mk_array_accessors

Takes 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, push_*

Pushes the given elements onto the end of the array. Like perl's push().

*_pop, pop_*

Pops one element off the end of the array. Like perl's pop().

*_shift, shift_*

Shifts one element off the beginning of the array. Like perl's shift().

*_unshift, unshift_*

Unshifts the given elements onto the beginning of the array. Like perl's unshift().

*_splice, splice_*

Takes an offset, a length and a replacement list. The arguments and behaviour are exactly like perl's splice().

*_clear, clear_*

Deletes all elements of the array.

*_count, count_*

Returns the number of elements in the array.

*_set, set_*

Takes a list, treated as pairs of index => value; each given index is set to the corresponding value. No return.

*_index, index_*

Takes a list of indices and returns a list of the corresponding values. This is like an array slice.

mk_hash_accessors

Takes an array of strings as its argument. For each string it creates methods as described below, where * denotes the slot name.

*

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, keys_*

Returns the keys of the hash.

*_values, values_*

Returns the list of values.

*_exists, exists_*

Takes a single key and returns whether that key exists in the hash.

*_delete, delete_*

Takes a list and deletes each key from the hash.

*_clear, clear_*

Resets the hash to empty.

mk_class_hash_accessors

Takes 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_abstract_accessors

Takes an array of strings as its argument. For each string it creates methods as described below, where * denotes the slot name.

*

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).

mk_boolean_accessors

Takes an array of strings as its argument. For each string it creates methods as described below, where * denotes the slot name.

*

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.

*_set, set_*

Sets the slot's value to 1.

*_clear, clear_*

Sets the slot's value to 0.

mk_integer_accessors

    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, reset_*

Resets the slot's value to 0.

*_inc, inc_*

Increments the value, then returns it.

*_dec, 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

mk_set_accessors

Takes an array of strings as its argument. For each string it creates methods as described below, where * denotes the slot name.

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, 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, elements_*

Returns the elements in the set.

*_delete, delete_*

Removes the given elements from the list. The order in which the elements are returned is not guaranteed.

*_clear, clear_*

Empties the set.

*_contains, contains_*

Given an element, it returns whether the set contains the element.

*_is_empty, is_empty_*

Returns whether or not the set is empty.

*_size, size_*

Returns the number of elements in the set.

mk_object_accessor

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.

Creates methods as described below, where * denotes the slot name.

*

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.

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;
*_clear, clear_*

Removes the object from the accessor.

TAGS

If you talk about this module in blogs, on del.icio.us or anywhere else, please use the classaccessorcomplex tag.

BUGS AND LIMITATIONS

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.

INSTALLATION

See perlmodinstall for information and options on installing Perl modules.

AVAILABILITY

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/>.

AUTHOR

Marcel Grünauer, <marcel@cpan.org>

COPYRIGHT AND LICENSE

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.