1Sub::HandlesVia::ManualU:s:eWritChoMnotorsieb(u3t)ed PerSlubD:o:cHuamnednlteastViioan::Manual::WithMoose(3)
2
3
4
6 Sub::HandlesVia::Manual::WithMoose - using Sub::HandlesVia with Moose
7
9 package Kitchen {
10 use Moose;
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 moose in your kitchen, that might be a disaster!)
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 Moose does have a built-in feature (native traits) which provides a
38 more limited version of this.
39
40 Which Methods Can Be Delegated To?
41 The "handles_via" option indicates which library of methods should be
42 available. Valid values include Array, Blessed, Bool, Code, Counter,
43 Enum, Hash, Number, Scalar, and String.
44
45 An arrayref can be provided for "handles_via", though many of the
46 options are conceptually contradictory.
47
48 handles_via => [ 'Number', 'Scalar' ]
49
50 Moose Native Types
51 Although the synopsis shows Types::Standard being used for type
52 constraints, Moose native types should also work fine.
53
54 package Kitchen {
55 use Moose;
56 use Sub::HandlesVia;
57
58 has food => (
59 is => 'ro',
60 isa => 'ArrayRef[Str]',
61 handles_via => 'Array',
62 default => sub { [] },
63 handles => {
64 'add_food' => 'push',
65 'find_food' => 'grep',
66 },
67 );
68 }
69
70 True Moose Native Traits Drop-In Syntax
71 Sub::HandlesVia will also recognize native-traits-style traits. It will
72 jump in and handle them before Moose notices!
73
74 package Kitchen {
75 use Moose;
76 use Sub::HandlesVia;
77
78 has food => (
79 is => 'ro',
80 isa => 'ArrayRef[Str]',
81 traits => ['Array'],
82 default => sub { [] },
83 handles => {
84 'add_food' => 'push',
85 'find_food' => 'grep',
86 },
87 );
88 }
89
91 Please report any bugs to
92 <https://github.com/tobyink/p5-sub-handlesvia/issues>.
93
95 Misc advanced documentation: Sub::HandlesVia::Manual::Advanced.
96
97 Sub::HandlesVia.
98
99 Documentation for delegatable methods:
100 Sub::HandlesVia::HandlerLibrary::Array,
101 Sub::HandlesVia::HandlerLibrary::Blessed,
102 Sub::HandlesVia::HandlerLibrary::Bool,
103 Sub::HandlesVia::HandlerLibrary::Code,
104 Sub::HandlesVia::HandlerLibrary::Counter,
105 Sub::HandlesVia::HandlerLibrary::Enum,
106 Sub::HandlesVia::HandlerLibrary::Hash,
107 Sub::HandlesVia::HandlerLibrary::Number,
108 Sub::HandlesVia::HandlerLibrary::Scalar, and
109 Sub::HandlesVia::HandlerLibrary::String.
110
112 Toby Inkster <tobyink@cpan.org>.
113
115 This software is copyright (c) 2022 by Toby Inkster.
116
117 This is free software; you can redistribute it and/or modify it under
118 the same terms as the Perl 5 programming language system itself.
119
121 THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
122 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
123 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
124
125
126
127perl v5.36.0 2023-04-S0u6b::HandlesVia::Manual::WithMoose(3)