- Perl 100%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
| dist | ||
| lib/Class | ||
| tests | ||
| LICENSE | ||
| Makefile.PL | ||
| README.md | ||
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.