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


NAME

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


VERSION

version 1.100880


SYNOPSIS

  package MyClass;
  use base 'Class::Accessor::Complex';
  __PACKAGE__
      ->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_accessors('Some::Foo' => {
          slot => 'an_object',
          comp_mthds => [ qw(do_this do_that) ]
      });


DESCRIPTION

This module generates accessors for your class in the same spirit as the Class::Accessor manpage 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 the Class::Accessor manpage, you can put a call to one of its accessor makers at the end of the chain.

The accessor generators also generate documentation ready to be used with the Sub::Documentation manpage.


METHODS

mk_new

Takes an array of strings as its argument. If no argument is given, it uses new as the default. For each string it creates a constructor of that name. 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';
    __PACKAGE__->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 an array of strings as its argument. If no argument is given, it uses new as the default. For each string it creates a constructor of that name.

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.

mk_class_scalar_accessors

Takes an array of strings as its argument. For each string it creates methods like those generated with mk_scalar_accessors(), except that it is a class scalar, i.e. shared by all instances of the class.

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.

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.

mk_class_array_accessors

Takes an array of strings as its argument. For each string it creates methods like those generated with mk_array_accessors(), except that it is a class hash, i.e. shared by all instances of the class.

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.

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.

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.

mk_integer_accessors

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

Example:

  package Foo;
  use base 'Class::Accessor::Complex';
  __PACKAGE__->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).

mk_object_accessors

    MyClass->mk_object_accessors(
        'Foo' => 'phooey',
        'Bar' => [ qw(bar1 bar2 bar3) ],
        'Baz' => {
            slot => 'foo',
            comp_mthds => [ qw(bar baz) ]
        },
        'Fob' => [
            {
                slot       => 'dog',
                comp_mthds => 'bark',
            },
            {
                slot       => 'cat',
                comp_mthds => 'miaow',
            },
        ],
    );

The main argument should be a reference to an array. The array should contain pairs of class => sub-argument pairs. The sub-arguments parsed thus:

Hash Reference

See Baz above. The hash should contain the following keys:

slot

The name of the instance attribute (slot).

comp_mthds

A string or array reference, naming the methods that will be forwarded directly to the object in the slot.

Array Reference

As for String, for each member of the array. Also works if each member is a hash reference (see Fob above).

String

The name of the instance attribute (slot).

For each slot x, with forwarding methods y() and z(), the following methods are created:

x

A get/set method, see * below.

y

Forwarded onto the object in slot x, which is auto-created via new() if necessary. The new(), if called, is called without arguments.

z

As for y.

So, using the example above, a method, foo(), is created, which can get and set the value of those objects in slot foo, which will generally contain an object of class Baz. Two additional methods are created named bar() and baz() which result in a call to the bar() and baz() methods on the Baz object stored in slot foo.

Apart from the forwarding methods described above, mk_object_accessors() creates methods as described below, where * denotes the slot name.

mk_forward_accessors

    __PACKAGE__->mk_forward_accessors(
        comp1 => 'method1',
        comp2 => [ qw(method2 method3) ],
    );

Takes a hash of mappings as its arguments. Each hash value is expected to be either a string or an array reference. For each hash value an accessor is created and forwarded to the accessor denoted by its associated hash key.

In the example above, a call to method1() will be forwarded onto comp1(), and calls to method2() and method3() will be forwarded onto comp2().


INSTALLATION

See perlmodinstall for information and options on installing Perl modules.


BUGS AND LIMITATIONS

No bugs have been reported.

Please report any bugs or feature requests through the web interface at http://rt.cpan.org/Public/Dist/Display.html.


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://search.cpan.org/dist/Class-Accessor-Complex/.

The development version lives at http://github.com/hanekomu/Class-Accessor-Complex/. Instead of sending patches, please fork this project using the standard git and github infrastructure.


AUTHOR

  Marcel Gruenauer <marcel@cpan.org>


COPYRIGHT AND LICENSE

This software is copyright (c) 2007 by Marcel Gruenauer.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

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