Wrapper for Class::Accessor to allow defaults
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2025-08-02 11:06:00 +01:00
dist new repo location 2024-05-26 13:20:29 +01:00
lib/Class doc correction 2025-08-02 11:06:00 +01:00
tests example code from pod docs 2021-11-15 15:34:52 +00:00
LICENSE Initial Checkin 2017-08-10 22:28:35 +01:00
Makefile.PL new repo location 2024-05-26 13:20:29 +01:00
README.md Move to ExtUtils::MakeMaker for builds, update README 2021-11-15 15:46:48 +00:00

NAME

Class::Accessorize - Wrapper for Class::Accessor to allow defaults

VERSION

version 2.3

SYNOPSIS

package BoringFile;
use base qw(Class::Accessorize);
BoringFile->mk_accessors( qw( filename ) );

sub
new
{
    my $protoverse = shift;
    my $class      = ref($protoverse) || $protoverse;
    my $self = $class->SUPER::new(
                        # Sub-class fields take priority
                        @_,
                        # we provide defaults
                        { 'filename' => '/dev/null' }
                    );
    return $self;
}

package InterestingFile
use base qw(BoringFile);
# we inherit the filename accessors from BoringFile
InterestingFile->mk_accessors( qw( source max_size ) );

sub
new
{
    my $protoverse = shift;
    my $class      = ref($protoverse) || $protoverse;
    my $self = $class->SUPER::new(
                        # Sub-class fields take priority
                        @_,
                        # we provide defaults
                        # fields do not have to be in a hash ref
                        'source'   => '/dev/random',
                        'max_size' => 1000,
                    );
    return $self;
}

package MAIN;

my $ifile = InterestingFile->new(
            {
                'filename' => 'something.real',
                'max_size' => '5555'
            }
        );

print "filename: ", $ifile->filename,
    "\nmax size: ", $ifile->max_size,
    "\nfrom source: ", $ifile->source,
    "\n";

DESCRIPTION

Class::Accessorize is a wrapper around Class::Accessor that allows a set of default values to be specified at each level of sub-class.

Since Class::Accessor specifies that the initialising data is supplied as a hash reference to the new() method we simply define that each layer puts its own defaults on the end of @_ prior to calling SUPER::new. The Class::Accessorize module then uses the merge_stacked_options method from Class::Accessorize::Common to produce a composite hash using each key that has not already been defined. This result is then passed to Class::Accessor as the fields hash.

For further examples of the merging see the documentation for the Class::Accessorize::Common module.

SEE ALSO

This module comes with a companion module Class::Accessorize::All which behaves in a very similar manner except it automatically creates accessor methods for every field provided in the new() hash.

COMPATIBILITY

  • Class::Accessorize requires Class::Accessorize::Common and Class::Accessor (quelle surprise).

AUTHOR

Bernard Quatermass toolsmith@quatermass.co.uk

COPYRIGHT AND LICENSE

This software is copyright (c) 2017, 2018, 2020, 2021 by Bernard Quatermass.

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