| 1 |
NAME |
| 2 |
Lexical::Accessor - true private attributes for Moose/Moo/Mouse |
| 3 |
|
| 4 |
SYNOPSIS |
| 5 |
my $accessor = lexical_has identifier => ( |
| 6 |
is => 'rw', |
| 7 |
isa => Int, |
| 8 |
default => sub { 0 }, |
| 9 |
); |
| 10 |
|
| 11 |
# or... |
| 12 |
lexical_has identifier => ( |
| 13 |
is => 'rw', |
| 14 |
isa => Int, |
| 15 |
default => sub { 0 }, |
| 16 |
accessor => \$accessor, |
| 17 |
); |
| 18 |
|
| 19 |
# later... |
| 20 |
say $self->$accessor; # says 0 |
| 21 |
$self->$accessor( 1 ); # setter |
| 22 |
say $self->$accessor; # says 1 |
| 23 |
|
| 24 |
DESCRIPTION |
| 25 |
Lexical::Accessor generates coderefs which can be used as methods to |
| 26 |
access private attributes for objects. |
| 27 |
|
| 28 |
The private attributes are stored inside-out, and do not add any accessors |
| 29 |
to the class' namespace, so are completely invisible to any outside code, |
| 30 |
including any subclasses. This gives your attribute complete privacy: |
| 31 |
subclasses can define a private (or even public) attribute with the same |
| 32 |
name as your private one and they will not interfere with each other. |
| 33 |
|
| 34 |
Private attributes cannot be initialized by Moose/Moo/Mouse constructors, |
| 35 |
but you can safely initialize them inside a `BUILD` sub. |
| 36 |
|
| 37 |
Functions |
| 38 |
`lexical_has $name?, %options` |
| 39 |
This module exports a function lexical_has which acts much like |
| 40 |
Moose's `has` function, but sets up a private (lexical) attribute |
| 41 |
instead of a public one. |
| 42 |
|
| 43 |
Because lexical attributes are stored inside-out, the $name is |
| 44 |
completely optional; however a name is recommended because it allows |
| 45 |
better error messages to be generated. |
| 46 |
|
| 47 |
The lexical_has function supports the following options: |
| 48 |
|
| 49 |
`is` |
| 50 |
Moose/Mouse/Moo-style `ro`, `rw`, `rwp` and `lazy` values are |
| 51 |
supported. These control what sort of coderef is returned by the |
| 52 |
`lexical_has` function itself. |
| 53 |
|
| 54 |
my $reader = lexical_has "foo" => (is => "ro"); |
| 55 |
my $accessor = lexical_has "foo" => (is => "rw"); |
| 56 |
my ($reader, $writer) = lexical_has "foo" => (is => "rwp"); |
| 57 |
|
| 58 |
If generating more than one method it is probably clearer to pass |
| 59 |
in scalar references to the `reader`, `writer`, etc methods, |
| 60 |
rather than relying on the return value of the `lexical_has` |
| 61 |
function. |
| 62 |
|
| 63 |
`reader`, `writer`, `accessor`, `predicate`, `clearer` |
| 64 |
These accept scalar references. The relevant coderefs will be |
| 65 |
plonked into them: |
| 66 |
|
| 67 |
my ($get_foo, $set_foo); |
| 68 |
|
| 69 |
lexical_has foo => ( |
| 70 |
reader => \$get_foo, |
| 71 |
writer => \$set_foo, |
| 72 |
); |
| 73 |
|
| 74 |
They can also be method names as strings: |
| 75 |
|
| 76 |
my ($set_foo); |
| 77 |
|
| 78 |
lexical_has foo => ( |
| 79 |
reader => 'get_foo', |
| 80 |
writer => \$set_foo, |
| 81 |
); |
| 82 |
|
| 83 |
This allows you to provide a partly public API for an attribute. |
| 84 |
|
| 85 |
`default`, `builder`, `lazy` |
| 86 |
Lazy defaults and builders are allowed. Eager (non-lazy) defaults |
| 87 |
and builders are currently disallowed. (Use a `BUILD` sub to set |
| 88 |
private attribute values at object construction time.) |
| 89 |
|
| 90 |
The default may be either a non-reference value, or a coderef |
| 91 |
which will be called as a method to return the value. |
| 92 |
|
| 93 |
Builders probably make less sense than defaults because they |
| 94 |
require a method in the class' namespace. The builder may be a |
| 95 |
method name, or the special value '1' which will be interpreted as |
| 96 |
meaning the attribute name prefixed by "_build_". If a coderef is |
| 97 |
provided, this is automatically installed into the class' |
| 98 |
namespace with the "_build_" prefix. (This last feature requires |
| 99 |
Sub::Name.) |
| 100 |
|
| 101 |
`isa` |
| 102 |
A type constraint for the attribute. Moo-style coderefs are |
| 103 |
accepted (including those generated by MooX::Types::MooseLike), as |
| 104 |
are Moose::Meta::TypeConstraint/MooseX::Types objects, and |
| 105 |
Mouse::Meta::TypeConstraint/MouseX::Types objects, and of course |
| 106 |
Type::Tiny type constraints. |
| 107 |
|
| 108 |
String type constraints may also be accepted, but only if |
| 109 |
Type::Utils is installed. (String type constraints are reified |
| 110 |
using `dwim_type`.) |
| 111 |
|
| 112 |
`does` |
| 113 |
As an alternative to `isa`, you can provide a role name in the |
| 114 |
`does` option. |
| 115 |
|
| 116 |
`coerce` |
| 117 |
A coderef or Type::Coercion object is accepted. |
| 118 |
|
| 119 |
If the special value '1' is provided, the type constraint object |
| 120 |
is consulted to find the coercion. (This doesn't work for coderef |
| 121 |
type constraints.) |
| 122 |
|
| 123 |
`trigger` |
| 124 |
A method name or coderef to trigger when a new value is set. |
| 125 |
|
| 126 |
`auto_deref` |
| 127 |
Boolean indicating whether to automatically dereference array and |
| 128 |
hash values if called in list context. |
| 129 |
|
| 130 |
`chain` |
| 131 |
If set to true, then writer and clearer methods, plus accessor |
| 132 |
methods when setting a value, will return the original object as |
| 133 |
their return value. This makes it possible to do: |
| 134 |
|
| 135 |
my $result = $obj->$set_foo(1)->$set_bar(2)->some_method(); |
| 136 |
$obj->$clear_foo->$clear_bar; |
| 137 |
|
| 138 |
It is also possible to set `chain` to an arrayref of which kinds |
| 139 |
of accessors are expected to be chainable. For example: `chain => |
| 140 |
[ 'clearer', 'writer' ]`. Only 'clearer', 'writer', and 'accessor' |
| 141 |
are valid values in this arrayref. |
| 142 |
|
| 143 |
`init_arg` |
| 144 |
Must be `undef` if provided at all. |
| 145 |
|
| 146 |
`required` |
| 147 |
Must be false if provided at all. |
| 148 |
|
| 149 |
`weak_ref` |
| 150 |
Boolean. Makes the setter weaken any references it is called with. |
| 151 |
|
| 152 |
`handles` |
| 153 |
Delegates methods. Has slightly different syntax to Moose's option |
| 154 |
of the same name - is required to be an arrayref of pairs such |
| 155 |
that in each pair, the first is a scalar ref or a string method |
| 156 |
name that will be handled, and the second is a coderef or string |
| 157 |
method name that will do the handling. (The second can be an |
| 158 |
arrayref in the case of currying.) |
| 159 |
|
| 160 |
my ($get, $post); |
| 161 |
|
| 162 |
lexical_has ua => ( |
| 163 |
isa => 'HTTP::Tiny', |
| 164 |
default => sub { 'HTTP::Tiny'->new }, |
| 165 |
handles => [ |
| 166 |
\$get => 'get', |
| 167 |
\$post => 'post', |
| 168 |
], |
| 169 |
); |
| 170 |
|
| 171 |
# later... |
| 172 |
my $response = $self->$get('http://example.net/'); |
| 173 |
|
| 174 |
Supports Sub::HandlesVia: |
| 175 |
|
| 176 |
my $remove_task; |
| 177 |
lexical_has tasks => ( |
| 178 |
isa => ArrayRef, |
| 179 |
handles_via => 'Array', |
| 180 |
handles => [ |
| 181 |
task_count => 'count', |
| 182 |
add_task => 'push', |
| 183 |
next_task => [ 'get', 0 ], |
| 184 |
\$remove_task => 'unshift', |
| 185 |
], |
| 186 |
); |
| 187 |
|
| 188 |
# later... |
| 189 |
while ($self->task_count) { |
| 190 |
my $task = $self->next_task; |
| 191 |
my $success = $self->handle_task($task); |
| 192 |
if ($success) { |
| 193 |
$self->$remove_task; |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
`initializer`, `traits`, `lazy_build` |
| 198 |
Not currently implemented. Providing any of these options throws |
| 199 |
an error. |
| 200 |
|
| 201 |
`documentation`, `definition_context` |
| 202 |
Don't do anything, but are allowed; effectively inline comments. |
| 203 |
|
| 204 |
Class Methods |
| 205 |
`lexical_has` |
| 206 |
This function may also be called as a class method. |
| 207 |
|
| 208 |
Comparison (benchmarking, etc) |
| 209 |
Lexical::Accessor is almost three times faster than |
| 210 |
MooX::PrivateAttributes, and almost twenty time faster than |
| 211 |
MooseX::Privacy. I'd also argue that it's a more "correct" implementation |
| 212 |
of private accessors as (short of performing impressive PadWalker |
| 213 |
manipulations), the accessors generated by this module are completely |
| 214 |
invisible to subclasses, method dispatch, etc. |
| 215 |
|
| 216 |
Compared to the usual Moose convention of using a leading underscore to |
| 217 |
indicate a private method (which is a very loose convention; it is quite |
| 218 |
common for subclasses to override such methods!), Lexical::Accessor |
| 219 |
clearly offers much better method privacy. There should be little |
| 220 |
performance hit from using lexical accessors compared to normal Moose |
| 221 |
accessors. (However they are nowhere near the speed of the XS-powered |
| 222 |
accessors that Moo *sometimes* uses and Mouse *usually* uses.) |
| 223 |
|
| 224 |
See also: `examples/benchmark.pl` bundled with this release. |
| 225 |
|
| 226 |
BUGS |
| 227 |
Please report any bugs to |
| 228 |
<http://rt.cpan.org/Dist/Display.html?Queue=Lexical-Accessor>. |
| 229 |
|
| 230 |
SUPPORT |
| 231 |
IRC: support is available through in the *#moops* channel on irc.perl.org |
| 232 |
<http://www.irc.perl.org/channels.html>. |
| 233 |
|
| 234 |
SEE ALSO |
| 235 |
MooX::PrivateAttributes, MooX::ProtectedAttributes, MooseX::Privacy, |
| 236 |
Sub::Private, Method::Lexical, etc... |
| 237 |
|
| 238 |
AUTHOR |
| 239 |
Toby Inkster <[email protected]>. |
| 240 |
|
| 241 |
COPYRIGHT AND LICENCE |
| 242 |
This software is copyright (c) 2013-2014, 2017, 2025 by Toby Inkster. |
| 243 |
|
| 244 |
This is free software; you can redistribute it and/or modify it under the |
| 245 |
same terms as the Perl 5 programming language system itself. |
| 246 |
|
| 247 |
DISCLAIMER OF WARRANTIES |
| 248 |
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED |
| 249 |
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF |
| 250 |
MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 251 |
|