1Sub::HandlesVia::ManualU:s:eWritChoMnittrei(b3u)ted PerlSuDbo:c:uHmaenndtlaetsiVoina::Manual::WithMite(3)
2
3
4
6 Sub::HandlesVia::Manual::WithMite - using Sub::HandlesVia with Mite
7
9 package MyApp::Kitchen {
10 use MyApp::Mite;
11
12 has food => (
13 is => 'ro',
14 isa => 'ArrayRef[Str]',
15 handles_via => 'Array',
16 default => sub { [] },
17 handles => {
18 'add_food' => 'push',
19 'find_food' => 'grep',
20 },
21 );
22 }
23
25 Sub::HandlesVia allows you to delegate methods from your class to the
26 values of your objects' attributes.
27
28 Conceptually, it allows you to define "$object->push_number($n)" to be
29 a shortcut for "$object->numbers->push($n)" except that
30 "$object->numbers" is an arrayref, so doesn't have methods you can call
31 on it like "push".
32
33 Mite 0.009000 and above have built-in "handles_via" support. Mite will
34 co-operate with Sub::HandlesVia to implement "handles_via" and avoid
35 your project having a Sub::HandlesVia dependency!
36
37 Which Methods Can Be Delegated To?
38 The "handles_via" option indicates which library of methods should be
39 available. Valid values include Array, Blessed, Bool, Code, Counter,
40 Hash, Number, Scalar, and String.
41
42 An arrayref can be provided for "handles_via", though many of the
43 options are conceptually contradictory.
44
45 handles_via => [ 'Number', 'Scalar' ]
46
47 "use Sub::HandlesVia"
48 Mite goes to a lot of effort to avoid you needing to "use
49 Sub::HandlesVia" directly in your classes and roles, and thus avoid you
50 needing to add it as a dependency.
51
52 However, there are edge cases that it might not handle. In these cases,
53 you should be able to "use SubHandlesVia" in your code after your "use
54 MyApp::Mite" and Sub::HandlesVia will intervene and make things work.
55 This adds a dependency on Sub::HandlesVia though.
56
57 package MyApp::Kitchen {
58 use MyApp::Mite;
59 use Sub::HandlesVia;
60
61 has food => (
62 is => 'ro',
63 isa => 'ArrayRef[Str]',
64 handles_via => 'Array',
65 default => sub { [] },
66 handles => {
67 'add_food' => 'push',
68 'find_food' => 'grep',
69 },
70 );
71 }
72
73 Now Mite will no longer generate "add_food" and "find_food" methods in
74 your Kitchen.pm.mite.pm file when you run "mite compile". Instead,
75 Sub::HandlesVia will generate them at run-time when your application
76 runs.
77
79 Please report any bugs to
80 <https://github.com/tobyink/p5-sub-handlesvia/issues>.
81
83 Misc advanced documentation: Sub::HandlesVia::Manual::Advanced.
84
85 Sub::HandlesVia, Mite.
86
87 Documentation for delegatable methods:
88 Sub::HandlesVia::HandlerLibrary::Array,
89 Sub::HandlesVia::HandlerLibrary::Blessed,
90 Sub::HandlesVia::HandlerLibrary::Bool,
91 Sub::HandlesVia::HandlerLibrary::Code,
92 Sub::HandlesVia::HandlerLibrary::Counter,
93 Sub::HandlesVia::HandlerLibrary::Hash,
94 Sub::HandlesVia::HandlerLibrary::Number,
95 Sub::HandlesVia::HandlerLibrary::Scalar, and
96 Sub::HandlesVia::HandlerLibrary::String.
97
99 Toby Inkster <tobyink@cpan.org>.
100
102 This software is copyright (c) 2022 by Toby Inkster.
103
104 This is free software; you can redistribute it and/or modify it under
105 the same terms as the Perl 5 programming language system itself.
106
108 THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
109 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
110 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
111
112
113
114perl v5.36.0 2022-12-1S7ub::HandlesVia::Manual::WithMite(3)