1MooX::HandlesVia(3) User Contributed Perl Documentation MooX::HandlesVia(3)
2
3
4
6 MooX::HandlesVia - NativeTrait-like behavior for Moo.
7
9 version 0.001009
10
12 {
13 package Hashy;
14 use Moo;
15 use MooX::HandlesVia;
16
17 has hash => (
18 is => 'rw',
19 handles_via => 'Hash',
20 handles => {
21 get_val => 'get',
22 set_val => 'set',
23 all_keys => 'keys'
24 }
25 );
26 }
27
28 my $h = Hashy->new(hash => { a => 1, b => 2});
29
30 $h->get_val('b'); # 2
31
32 $h->set_val('a', 'BAR'); # sets a to BAR
33
34 my @keys = $h->all_keys; # returns a, b
35
37 MooX::HandlesVia is an extension of Moo's 'handles' attribute
38 functionality. It provides a means of proxying functionality from an
39 external class to the given atttribute. This is most commonly used as a
40 way to emulate 'Native Trait' behavior that has become commonplace in
41 Moose code, for which there was no Moo alternative.
42
44 Due to current Moo implementation details there are some deficiencies
45 in how MooX::HandlesVia in comparison to what you would expect from
46 Moose native traits.
47
48 • methods delegated via the Moo 'handles' interface are passed the
49 attribue value directly. and there is no way to access the parent
50 class. This means if an attribute is updated any triggers or type
51 coercions WILL NOT fire.
52
53 • Moo attribute method delegations are passed the attribute value.
54 This is fine for references (objects, arrays, hashrefs..) it means
55 simple scalar types are READ ONLY. This unfortunately means Number,
56 String, Counter, Bool cannot modify the attributes value, rendering
57 them largely useless.
58
59 If these are issues for you, consider Sub::HandlesVia, which uses a
60 different architecture, respecting triggers and coercions, and allowing
61 read-write access to non-reference values. It should be possible to use
62 Sub::HandlesVia as a drop-in replacement for MooX::HandlesVia.
63
65 process_has(@_)
66 MooX::HandlesVia preprocesses arguments passed to has() attribute
67 declarations via the process_has function. In a given Moo class, If
68 'handles_via' is set to a ClassName string, and 'handles' is set
69 with a hashref mapping of desired moo class methods that should map
70 to ClassName methods, process_has() will create the appropriate
71 binding to create the mapping IF ClassName provides that named
72 method.
73
74 has options => (
75 is => 'rw',
76 handles_via => 'Array',
77 handles => {
78 mixup => 'shuffle',
79 unique_options => 'uniq',
80 all_options => 'elements'
81 }
82 );
83
84 The following handles_via keywords are reserved as shorthand for
85 mapping to Data::Perl:
86
87 • Hash maps to Data::Perl::Collection::Hash::MooseLike
88
89 • Array maps to Data::Perl::Collection::Array::MooseLike
90
91 • String maps to Data::Perl::String::MooseLike
92
93 • Number maps to Data::Perl::Number::MooseLike
94
95 • Bool maps to Data::Perl::Bool::MooseLike
96
97 • Code maps to Data::Perl::Code
98
100 • Moo
101
102 • MooX::late
103
104 • Sub::HandlesVia
105
107 Matthew Phillips <mattp@cpan.org>
108
110 Toby Inkster <tobyink@cpan.org>
111
113 This software is copyright (c) 2020 by Matthew Phillips
114 <mattp@cpan.org>.
115
116 This is free software; you can redistribute it and/or modify it under
117 the same terms as the Perl 5 programming language system itself.
118
119
120
121perl v5.34.0 2022-01-21 MooX::HandlesVia(3)