The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Class::Accessor::Deep - Automated accessor generation for nested structures inside objects

SYNOPSIS

Foo.pm:

    package Foo;
    
    use base 'Class::Accessor::Deep';
    
    sub new {
        bless {
            a => {
                b => 'c',
            },
        }, 'Foo'
    }

bar.pl:

    use Foo;
    
    my $foo = Foo->new;
    print $foo->get_b; # prints 'c'
    $foo->set_b('d');  # sets $foo->{a}->{b} to 'd'

DESCRIPTION

This module generates accessors/mutators for structures with nesting level >= 1.

Using this module you don't have to write

    $obj->hashref->{key1}->{key2}

Now you can just

    $obj->key2 # the same is $obj->{hashref}->{key1}->{key2}

This is done by exporting an AUTOLOAD subroutine to caller's namespace. Every time you call an undefined method, AUTOLOAD walks your object structure and tries to find a field with the name of method you called.

Indeed this is done only first time you call unique method. After this it will be injected into caller's symtable. So next time you call the same method it will be executed directly, not via AUTOLOAD.

METHODS

Class::Accessor::Deep handles these methods:

  • get_attr_name - Readonly accessor

  • set_attr_name($value) - Writeonly mutator

  • attr_name($value?) - Read/write accessor/mutator

CAVEATS

Behaviour for duplicate attributes on different nesting level is not defined.

Objects inside other objects are not handled. This means that you can not access attributes of nested object using this module.

AUTHOR

sir_nuf_nuf <mialinx@rambler.ru>

Aleksey Surikov <ksuri@cpan.org>

LICENSE

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