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::Installer - install an accessor subroutine

SYNOPSIS

    package Class::Accessor::Foo;

    use base 'Class::Accessor::Installer';

    sub mk_foo_accessors {
        my ($self, @fields) = @_;
        my $class = ref $self || $self;

        for my $field (@fields) {
            $self->install_accessor(
                sub  => "${field}_foo",
                code => sub { ... },
            );
        }

    }

DESCRIPTION

This mixin class provides a method that will install a coderef. There are other modules that do this, but this one is a bit more specific to the needs of Class::Accessor::Complex and friends.

It is intended as a mixin, that is, your accessor-generating class should inherit from this class.

METHODS

install_accessor

Takes as arguments a named hash. The following keys are recognized:

pkg

The package into which to install the subroutine. If this argument is omitted, it will inspect $self to determine the package. Class::Accessor::* accessor generators are typically used like this:

    __PACKAGE__
        ->mk_new
        ->mk_array_accessors(qw(foo bar));

Therefore install_accessor() can determine the right package into which to install the subroutine.

name

The name or names to use for the subroutine. You can either pass a single string or a reference to an array of strings. Each string is interpreted as a subroutine name inside the given package, and the code reference is installed into the appropriate typeglob.

Why would you want to install a subroutine in more than one place inside your package? For example, Class::Accessor::Complex often creates aliases so the user can choose the version of the name that reads more naturally.

An example of this usage would be:

        $self->install_accessor(
            name => [ "clear_${field}", "${field}_clear" ],
            code => sub { ... }
        );
code

This is the code reference that should be installed.

The installed subroutine is named using Sub::Name, so it shows up with a meaningful name in stack traces (instead of as __ANON__). However, the inside the debugger, the subroutine still shows up as __ANON__. You might therefore want to use the following lines at the beginning of your subroutine:

        $self->install_accessor(
            name => $field,
            code => sub {
                local $DB::sub = local *__ANON__ = "${class}::${name}"
                    if defined &DB::DB && !$Devel::DProf::VERSION;
                ...
            }
        );

Now the subroutine will be named both in a stack trace and inside the debugger.

TAGS

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

BUGS AND LIMITATIONS

No bugs have been reported.

Please report any bugs or feature requests to bug-class-accessor-installer@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.