1Sub::HandlesVia::ManualU:s:eWritChoMnotursieb(u3t)ed PerSlubD:o:cHuamnednlteastViioan::Manual::WithMouse(3)
2
3
4
6 Sub::HandlesVia::Manual::WithMouse - using Sub::HandlesVia with Mouse
7
9 package Kitchen {
10 use Mouse;
11 use Sub::HandlesVia;
12 use Types::Standard qw( ArrayRef Str );
13
14 has food => (
15 is => 'ro',
16 isa => ArrayRef[Str],
17 handles_via => 'Array',
18 default => sub { [] },
19 handles => {
20 'add_food' => 'push',
21 'find_food' => 'grep',
22 },
23 );
24 }
25
26 (If you have a mouse in your kitchen, that might not be very hygienic.)
27
29 Sub::HandlesVia allows you to delegate methods from your class to the
30 values of your objects' attributes.
31
32 Conceptually, it allows you to define "$object->push_number($n)" to be
33 a shortcut for "$object->numbers->push($n)" except that
34 "$object->numbers" is an arrayref, so doesn't have methods you can call
35 on it like "push".
36
37 You should be able to use Sub::HandlesVia as a drop-in replacement for
38 MouseX::NativeTraits, just replacing "traits => ['Array']" in an
39 attribute definition with "handles_via => 'Array'".
40
41 Usage in Mouse Roles
42 Mouse roles are only partially supported.
43
44 package R1 {
45 use Mouse:Role;
46 }
47
48 package R2 {
49 use Mouse:Role;
50 use Sub::HandlesVia;
51
52 # define some attributes with delegation
53 }
54
55 # This class is broken.
56 package C1 {
57 use Mouse;
58 with 'R1', 'R2';
59 }
60
61 # This class should work.
62 package C2 {
63 use Mouse;
64 with 'R1';
65 with 'R2';
66 }
67
68 For details, see
69 <https://github.com/tobyink/p5-sub-handlesvia/issues/9>.
70
71 Patches to fix the issue are very welcome!
72
73 Which Methods Can Be Delegated To?
74 The "handles_via" option indicates which library of methods should be
75 available. Valid values include Array, Blessed, Bool, Code, Counter,
76 Enum, Hash, Number, Scalar, and String.
77
78 An arrayref can be provided for "handles_via", though many of the
79 options are conceptually contradictory.
80
81 handles_via => [ 'Number', 'Scalar' ]
82
83 Mouse Native Types
84 Although the synopsis shows Types::Standard being used for type
85 constraints, Mouse native types should also work fine.
86
87 package Kitchen {
88 use Mouse;
89 use Sub::HandlesVia;
90
91 has food => (
92 is => 'ro',
93 isa => 'ArrayRef[Str]',
94 handles_via => 'Array',
95 default => sub { [] },
96 handles => {
97 'add_food' => 'push',
98 'find_food' => 'grep',
99 },
100 );
101 }
102
103 True MouseX::NativeTraits Drop-In Syntax
104 Sub::HandlesVia will also recognize MouseX::NativeTraits-style
105 "traits". It will jump in and handle them before MouseX::NativeTraits
106 notices!
107
108 package Kitchen {
109 use Mouse;
110 use Sub::HandlesVia;
111
112 has food => (
113 is => 'ro',
114 isa => 'ArrayRef[Str]',
115 traits => ['Array'],
116 default => sub { [] },
117 handles => {
118 'add_food' => 'push',
119 'find_food' => 'grep',
120 },
121 );
122 }
123
125 Please report any bugs to
126 <https://github.com/tobyink/p5-sub-handlesvia/issues>.
127
129 Misc advanced documentation: Sub::HandlesVia::Manual::Advanced.
130
131 Sub::HandlesVia.
132
133 Documentation for delegatable methods:
134 Sub::HandlesVia::HandlerLibrary::Array,
135 Sub::HandlesVia::HandlerLibrary::Blessed,
136 Sub::HandlesVia::HandlerLibrary::Bool,
137 Sub::HandlesVia::HandlerLibrary::Code,
138 Sub::HandlesVia::HandlerLibrary::Counter,
139 Sub::HandlesVia::HandlerLibrary::Enum,
140 Sub::HandlesVia::HandlerLibrary::Hash,
141 Sub::HandlesVia::HandlerLibrary::Number,
142 Sub::HandlesVia::HandlerLibrary::Scalar, and
143 Sub::HandlesVia::HandlerLibrary::String.
144
146 Toby Inkster <tobyink@cpan.org>.
147
149 This software is copyright (c) 2022 by Toby Inkster.
150
151 This is free software; you can redistribute it and/or modify it under
152 the same terms as the Perl 5 programming language system itself.
153
155 THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
156 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
157 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
158
159
160
161perl v5.36.0 2023-04-S0u6b::HandlesVia::Manual::WithMouse(3)