1Moose::Meta::Attribute:U:sNeartiCvoen(t3r)ibuted Perl DoMcouomseen:t:aMteitoan::Attribute::Native(3)
2
3
4

NAME

6       Moose::Meta::Attribute::Native - Delegate to native Perl types
7

VERSION

9       version 2.2014
10

SYNOPSIS

12         package MyClass;
13         use Moose;
14
15         has 'mapping' => (
16             traits  => ['Hash'],
17             is      => 'rw',
18             isa     => 'HashRef[Str]',
19             default => sub { {} },
20             handles => {
21                 exists_in_mapping => 'exists',
22                 ids_in_mapping    => 'keys',
23                 get_mapping       => 'get',
24                 set_mapping       => 'set',
25                 set_quantity      => [ set => 'quantity' ],
26             },
27         );
28
29         my $obj = MyClass->new;
30         $obj->set_quantity(10);      # quantity => 10
31         $obj->set_mapping('foo', 4); # foo => 4
32         $obj->set_mapping('bar', 5); # bar => 5
33         $obj->set_mapping('baz', 6); # baz => 6
34
35         # prints 5
36         print $obj->get_mapping('bar') if $obj->exists_in_mapping('bar');
37
38         # prints 'quantity, foo, bar, baz'
39         print join ', ', $obj->ids_in_mapping;
40

DESCRIPTION

42       Native delegations allow you to delegate to native Perl data structures
43       as if they were objects. For example, in the "SYNOPSIS" you can see a
44       hash reference being treated as if it has methods named "exists()",
45       "keys()", "get()", and "set()".
46
47       The delegation methods (mostly) map to Perl builtins and operators. The
48       return values of these delegations should be the same as the
49       corresponding Perl operation. Any deviations will be explicitly
50       documented.
51

API

53       Native delegations are enabled by passing certain options to "has" when
54       creating an attribute.
55
56   traits
57       To enable this feature, pass the appropriate name in the "traits" array
58       reference for the attribute. For example, to enable this feature for
59       hash reference, we include 'Hash' in the list of traits.
60
61   isa
62       You will need to make sure that the attribute has an appropriate type.
63       For example, to use this with a Hash you must specify that your
64       attribute is some sort of "HashRef".
65
66   handles
67       This is just like any other delegation, but only a hash reference is
68       allowed when defining native delegations. The keys are the methods to
69       be created in the class which contains the attribute. The values are
70       the methods provided by the associated trait. Currying works the same
71       way as it does with any other delegation.
72
73       See the docs for each native trait for details on what methods are
74       available.
75

TRAITS FOR NATIVE DELEGATIONS

77       Below are some simple examples of each native trait. More features are
78       available than what is shown here; this is just a quick synopsis.
79
80       Array (Moose::Meta::Attribute::Native::Trait::Array)
81               has 'queue' => (
82                   traits  => ['Array'],
83                   is      => 'ro',
84                   isa     => 'ArrayRef[Str]',
85                   default => sub { [] },
86                   handles => {
87                       add_item  => 'push',
88                       next_item => 'shift',
89                       # ...
90                   }
91               );
92
93       Bool (Moose::Meta::Attribute::Native::Trait::Bool)
94               has 'is_lit' => (
95                   traits  => ['Bool'],
96                   is      => 'ro',
97                   isa     => 'Bool',
98                   default => 0,
99                   handles => {
100                       illuminate  => 'set',
101                       darken      => 'unset',
102                       flip_switch => 'toggle',
103                       is_dark     => 'not',
104                       # ...
105                   }
106               );
107
108       Code (Moose::Meta::Attribute::Native::Trait::Code)
109               has 'callback' => (
110                   traits  => ['Code'],
111                   is      => 'ro',
112                   isa     => 'CodeRef',
113                   default => sub {
114                       sub {'called'}
115                   },
116                   handles => {
117                       call => 'execute',
118                       # ...
119                   }
120               );
121
122       Counter (Moose::Meta::Attribute::Native::Trait::Counter)
123               has 'counter' => (
124                   traits  => ['Counter'],
125                   is      => 'ro',
126                   isa     => 'Num',
127                   default => 0,
128                   handles => {
129                       inc_counter   => 'inc',
130                       dec_counter   => 'dec',
131                       reset_counter => 'reset',
132                       # ...
133                   }
134               );
135
136       Hash (Moose::Meta::Attribute::Native::Trait::Hash)
137               has 'options' => (
138                   traits  => ['Hash'],
139                   is      => 'ro',
140                   isa     => 'HashRef[Str]',
141                   default => sub { {} },
142                   handles => {
143                       set_option => 'set',
144                       get_option => 'get',
145                       has_option => 'exists',
146                       # ...
147                   }
148               );
149
150       Number (Moose::Meta::Attribute::Native::Trait::Number)
151               has 'integer' => (
152                   traits  => ['Number'],
153                   is      => 'ro',
154                   isa     => 'Int',
155                   default => 5,
156                   handles => {
157                       set => 'set',
158                       add => 'add',
159                       sub => 'sub',
160                       mul => 'mul',
161                       div => 'div',
162                       mod => 'mod',
163                       abs => 'abs',
164                       # ...
165                   }
166               );
167
168       String (Moose::Meta::Attribute::Native::Trait::String)
169               has 'text' => (
170                   traits  => ['String'],
171                   is      => 'ro',
172                   isa     => 'Str',
173                   default => q{},
174                   handles => {
175                       add_text     => 'append',
176                       replace_text => 'replace',
177                       # ...
178                   }
179               );
180

COMPATIBILITY WITH MooseX::AttributeHelpers

182       This feature used to be a separated CPAN distribution called
183       MooseX::AttributeHelpers.
184
185       When the feature was incorporated into the Moose core, some of the API
186       details were changed. The underlying capabilities are the same, but
187       some details of the API were changed.
188

BUGS

190       See "BUGS" in Moose for details on reporting bugs.
191

AUTHORS

193       •   Stevan Little <stevan@cpan.org>
194
195       •   Dave Rolsky <autarch@urth.org>
196
197       •   Jesse Luehrs <doy@cpan.org>
198
199       •   Shawn M Moore <sartak@cpan.org>
200
201       •   יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>
202
203       •   Karen Etheridge <ether@cpan.org>
204
205       •   Florian Ragwitz <rafl@debian.org>
206
207       •   Hans Dieter Pearcey <hdp@cpan.org>
208
209       •   Chris Prather <chris@prather.org>
210
211       •   Matt S Trout <mstrout@cpan.org>
212
214       This software is copyright (c) 2006 by Infinity Interactive, Inc.
215
216       This is free software; you can redistribute it and/or modify it under
217       the same terms as the Perl 5 programming language system itself.
218
219
220
221perl v5.32.1                      2021-01-27 Moose::Meta::Attribute::Native(3)
Impressum