.\" Automatically generated by Pod::Man 2.25 (Pod::Simple 3.16)
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" Set up some character translations and predefined strings. \*(-- will
.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
.\" double quote, and \*(R" will give a right double quote. \*(C+ will
.\" give a nicer C++. Capital omega is used to do unbreakable dashes and
.\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff,
.\" nothing in troff, for use with C<>.
.tr \(*W-
.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
.ie n \{\
. ds -- \(*W-
. ds PI pi
. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
. ds L" ""
. ds R" ""
. ds C` ""
. ds C' ""
'br\}
.el\{\
. ds -- \|\(em\|
. ds PI \(*p
. ds L" ``
. ds R" ''
'br\}
.\"
.\" Escape single quotes in literal strings from groff's Unicode transform.
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.\"
.\" If the F register is turned on, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
.\" entries marked with X<> in POD. Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.ie \nF \{\
. de IX
. tm Index:\\$1\t\\n%\t"\\$2"
..
. nr % 0
. rr F
.\}
.el \{\
. de IX
..
.\}
.\"
.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
.\" Fear. Run. Save yourself. No user-serviceable parts.
. \" fudge factors for nroff and troff
.if n \{\
. ds #H 0
. ds #V .8m
. ds #F .3m
. ds #[ \f1
. ds #] \fP
.\}
.if t \{\
. ds #H ((1u-(\\\\n(.fu%2u))*.13m)
. ds #V .6m
. ds #F 0
. ds #[ \&
. ds #] \&
.\}
. \" simple accents for nroff and troff
.if n \{\
. ds ' \&
. ds ` \&
. ds ^ \&
. ds , \&
. ds ~ ~
. ds /
.\}
.if t \{\
. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
.\}
. \" troff and (daisy-wheel) nroff accents
.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
.ds ae a\h'-(\w'a'u*4/10)'e
.ds Ae A\h'-(\w'A'u*4/10)'E
. \" corrections for vroff
.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
. \" for low resolution devices (crt and lpr)
.if \n(.H>23 .if \n(.V>19 \
\{\
. ds : e
. ds 8 ss
. ds o a
. ds d- d\h'-1'\(ga
. ds D- D\h'-1'\(hy
. ds th \o'bp'
. ds Th \o'LP'
. ds ae ae
. ds Ae AE
.\}
.rm #[ #] #H #V #F C
.\" ========================================================================
.\"
.IX Title "Class::Meta::AccessorBuilder::Affordance 3pm"
.TH Class::Meta::AccessorBuilder::Affordance 3pm "2012-07-21" "perl v5.14.2" "User Contributed Perl Documentation"
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
.nh
.SH "NAME"
Class::Meta::AccessorBuilder::Affordance \- Affordance style accessor generation
.SH "SYNOPSIS"
.IX Header "SYNOPSIS"
.Vb 1
\& package MyApp::TypeDef;
\&
\& use strict;
\& use Class::Meta::Type;
\& use IO::Socket;
\&
\& my $type = Class::Meta::Type\->add(
\& key => \*(Aqio_socket\*(Aq,
\& builder => \*(Aqaffordance\*(Aq,
\& desc => \*(AqIO::Socket object\*(Aq,
\& name => \*(AqIO::Socket Object\*(Aq
\& );
.Ve
.SH "DESCRIPTION"
.IX Header "DESCRIPTION"
This module provides the an affordance style accessor builder for Class::Meta.
Affordance accessors are attribute accessor methods that separate the getting
and setting of an attribute value into distinct methods. The approach both
eliminates the overhead of checking to see whether an accessor is called as a
getter or a setter, which is common for Perl style accessors, while also
creating a psychological barrier to accidentally misusing an attribute.
.SS "Accessors"
.IX Subsection "Accessors"
Class::Meta::AccessorBuilder::Affordance create two different types of
accessors: getters and setters. The type of accessors created depends on the
value of the \f(CW\*(C`authz\*(C'\fR attribute of the Class::Meta::Attribute for which the
accessor is being created.
.PP
For example, if the \f(CW\*(C`authz\*(C'\fR is Class::Meta::RDWR, then two accessor methods
will be created:
.PP
.Vb 2
\& my $value = $obj\->get_io_socket;
\& $obj\->set_io_socket($value);
.Ve
.PP
If the value of \f(CW\*(C`authz\*(C'\fR is Class::Meta::READ, then only the get method
will be created:
.PP
.Vb 1
\& my $value = $obj\->io_socket;
.Ve
.PP
And finally, if the value of \f(CW\*(C`authz\*(C'\fR is Class::Meta::WRITE, then only the set
method will be created (why anyone would want this is beyond me, but I provide
for the sake of completeness):
.PP
.Vb 1
\& my $value = $obj\->io_socket;
.Ve
.SS "Data Type Validation"
.IX Subsection "Data Type Validation"
Class::Meta::AccessorBuilder::Affordance uses all of the validation checks
passed to it to validate new values before assigning them to an attribute. It
also checks to see if the attribute is required, and if so, adds a check to
ensure that its value is never undefined. It does not currently check to
ensure that private and protected methods are used only in their appropriate
contexts, but may do so in a future release.
.SS "Class Attributes"
.IX Subsection "Class Attributes"
If the \f(CW\*(C`context\*(C'\fR attribute of the attribute object for which accessors are to
be built is \f(CW\*(C`Class::Meta::CLASS\*(C'\fR, Class::Meta::AccessorBuilder will build
accessors for a class attribute instead of an object attribute. Of course,
this means that if you change the value of the class attribute in any
context\*(--whether via a an object, the class name, or an an inherited class
name or object, the value will be changed everywhere.
.PP
For example, for a class attribute \*(L"count\*(R", you can expect the following to
work:
.PP
.Vb 4
\& MyApp::Custom\->set_count(10);
\& my $count = MyApp::Custom\->get_count; # Returns 10.
\& my $obj = MyApp::Custom\->new;
\& $count = $obj\->get_count; # Returns 10.
\&
\& $obj\->set_count(22);
\& $count = $obj\->get_count; # Returns 22.
\& my $count = MyApp::Custom\->get_count; # Returns 22.
\&
\& MyApp::Custom\->set_count(35);
\& $count = $obj\->get_count; # Returns 35.
\& my $count = MyApp::Custom\->get_count; # Returns 35.
.Ve
.PP
Currently, class attribute accessors are not designed to be inheritable in the
way designed by Class::Data::Inheritable, although this might be changed in a
future release. For now, I expect that the current simple approach will cover
the vast majority of circumstances.
.PP
\&\fBNote:\fR Class attribute accessors will not work accurately in multiprocess
environments such as mod_perl. If you change a class attribute's value in one
process, it will not be changed in any of the others. Furthermore, class
attributes are not currently shared across threads. So if you're using
Class::Meta class attributes in a multi-threaded environment (such as iThreads
in Perl 5.8.0 and later) the changes to a class attribute in one thread will
not be reflected in other threads.
.SH "Private and Protected Attributes"
.IX Header "Private and Protected Attributes"
Any attributes that have their \f(CW\*(C`view\*(C'\fR attribute set to Class::Meta::Private
or Class::Meta::Protected get additional validation installed to ensure that
they're truly private and protected. This includes when they are set via
parameters to constructors generated by Class::Meta. The validation is
performed by checking the caller of the accessors, and throwing an exception
when the caller isn't the class that owns the attribute (for private
attributes) or when it doesn't inherit from the class that owns the attribute
(for protected attributes).
.PP
As an implementation note, this validation is performed for parameters passed
to constructors created by Class::Meta by ignoring looking for the first
caller that isn't Class::Meta::Constructor:
.PP
.Vb 5
\& my $caller = caller;
\& # Circumvent generated constructors.
\& for (my $i = 1; $caller eq \*(AqClass::Meta::Constructor\*(Aq; $i++) {
\& $caller = caller($i);
\& }
.Ve
.PP
This works because Class::Meta::Constructor installs the closures that become
constructors, and thus, when those closures call accessors to set new values
for attributes, the caller is Class::Meta::Constructor. By going up the stack
until we find another package, we correctly check to see what context is
setting attribute values via a constructor, rather than the constructor method
itself being the context.
.PP
This is a bit of a hack, but since Perl uses call stacks for checking security
in this way, it's the best I could come up with. Other suggestions welcome. Or
see Class::Meta::Type to
create your own accessor generation code
.SH "INTERFACE"
.IX Header "INTERFACE"
The following functions must be implemented by any Class::Meta accessor
generation module.
.SS "Functions"
.IX Subsection "Functions"
\fIbuild_attr_get\fR
.IX Subsection "build_attr_get"
.PP
.Vb 1
\& my $code = Class::Meta::AccessorBuilder::Affordance::build_attr_get();
.Ve
.PP
This function is called by \f(CW\*(C`Class::Meta::Type::make_attr_get()\*(C'\fR and returns a
code reference that can be used by the \f(CW\*(C`get()\*(C'\fR method of
Class::Meta::Attribute to return the value stored for that attribute for the
object passed to the code reference.
.PP
\fIbuild_attr_set\fR
.IX Subsection "build_attr_set"
.PP
.Vb 1
\& my $code = Class::Meta::AccessorBuilder::Affordance::build_attr_set();
.Ve
.PP
This function is called by \f(CW\*(C`Class::Meta::Type::make_attr_set()\*(C'\fR and returns a
code reference that can be used by the \f(CW\*(C`set()\*(C'\fR method of
Class::Meta::Attribute to set the value stored for that attribute for the
object passed to the code reference.
.PP
\fIbuild\fR
.IX Subsection "build"
.PP
.Vb 3
\& Class::Meta::AccessorBuilder::Affordance::build(
\& $pkg, $attribute, $create, @checks
\& );
.Ve
.PP
This method is called by the \f(CW\*(C`build()\*(C'\fR method of Class::Meta::Type, and does
the work of actually generating the accessors for an attribute object. The
arguments passed to it are:
.ie n .IP "$pkg" 4
.el .IP "\f(CW$pkg\fR" 4
.IX Item "$pkg"
The name of the class to which the accessors will be added.
.ie n .IP "$attribute" 4
.el .IP "\f(CW$attribute\fR" 4
.IX Item "$attribute"
The Class::Meta::Attribute object that specifies the attribute for which the
accessors will be created.
.ie n .IP "$create" 4
.el .IP "\f(CW$create\fR" 4
.IX Item "$create"
The value of the \f(CW\*(C`create\*(C'\fR attribute of the Class::Meta::Attribute object,
which determines what accessors, if any, are to be created.
.ie n .IP "@checks" 4
.el .IP "\f(CW@checks\fR" 4
.IX Item "@checks"
A list of code references that validate the value of an attribute. These will
be used in the set accessor (mutator) to validate new attribute values.
.SH "SUPPORT"
.IX Header "SUPPORT"
This module is stored in an open GitHub
repository . Feel free to fork and
contribute!
.PP
Please file bug reports via GitHub
Issues or by sending mail to
bug\-Class\-Meta@rt.cpan.org .
.SH "AUTHOR"
.IX Header "AUTHOR"
David E. Wheeler
.SH "SEE ALSO"
.IX Header "SEE ALSO"
.IP "Class::Meta" 4
.IX Item "Class::Meta"
This class contains most of the documentation you need to get started with
Class::Meta.
.IP "Class::Meta::AccessorBuilder" 4
.IX Item "Class::Meta::AccessorBuilder"
This module generates Perl style accessors.
.IP "Class::Meta::Type" 4
.IX Item "Class::Meta::Type"
This class manages the creation of data types.
.IP "Class::Meta::Attribute" 4
.IX Item "Class::Meta::Attribute"
This class manages Class::Meta class attributes, most of which will have
generated accessors.
.SH "COPYRIGHT AND LICENSE"
.IX Header "COPYRIGHT AND LICENSE"
Copyright (c) 2002\-2011, David E. Wheeler. Some Rights Reserved.
.PP
This module is free software; you can redistribute it and/or modify it under
the same terms as Perl itself.