1Sub::HandlesVia::ManualU:s:eWritChoCnltarsisbTuitneydS(u3Pb)e:r:lHaDnodcluemseVnitaa:t:iMoannual::WithClassTiny(3)
2
3
4
6 Sub::HandlesVia::Manual::WithClassTiny - using Sub::HandlesVia with
7 Class::Tiny
8
10 package Kitchen;
11
12 use Class::Tiny {
13 foods => sub { [] },
14 drinks => sub { [ 'water' ] },
15 };
16
17 use Sub::HandlesVia::Declare 'foods', Array => (
18 all_foods => 'all',
19 add_food => 'push',
20 );
21
22 use Sub::HandlesVia::Declare 'drinks', Array => (
23 all_drinks => 'all',
24 add_drink => 'push',
25 );
26
28 Sub::HandlesVia allows you to delegate methods from your class to the
29 values of your objects' attributes.
30
31 Conceptually, it allows you to define "$object->push_number($n)" to be
32 a shortcut for "$object->numbers->push($n)" except that
33 "$object->numbers" is an arrayref, so doesn't have methods you can call
34 on it like "push".
35
36 Which Methods Can Be Delegated To?
37 The second parameter ("Array" in the synopsis) indicates which library
38 of methods should be available. Valid values include Array, Blessed,
39 Bool, Code, Counter, Enum, Hash, Number, Scalar, and String.
40
41 An arrayref can be provided, though many of the options are
42 conceptually contradictory.
43
44 use Sub::HandlesVia::Declare 'num', [ 'Number', 'Scalar' ] => (
45 ...,
46 );
47
48 Run-Time Delegation
49 Rather than using Sub::HandlesVia::Declare which operates at compile
50 time, you can use the "delegations" function at run time. Which you
51 choose is largely down to style and personal preference.
52
53 package Kitchen;
54
55 use Class::Tiny {
56 foods => sub { [] },
57 drinks => sub { [ 'water' ] },
58 };
59
60 use Sub::HandlesVia qw( delegations );
61
62 delegations(
63 attribute => 'foods',
64 handles_via => 'Array',
65 handles => {
66 all_foods => 'all',
67 add_food => 'push',
68 },
69 );
70
71 delegations(
72 attribute => 'drinks',
73 handles_via => 'Array',
74 handles => {
75 all_drinks => 'all',
76 add_drink => 'push',
77 },
78 );
79
81 Please report any bugs to
82 <https://github.com/tobyink/p5-sub-handlesvia/issues>.
83
85 Misc advanced documentation: Sub::HandlesVia::Manual::Advanced.
86
87 Sub::HandlesVia.
88
89 Documentation for delegatable methods:
90 Sub::HandlesVia::HandlerLibrary::Array,
91 Sub::HandlesVia::HandlerLibrary::Blessed,
92 Sub::HandlesVia::HandlerLibrary::Bool,
93 Sub::HandlesVia::HandlerLibrary::Code,
94 Sub::HandlesVia::HandlerLibrary::Counter,
95 Sub::HandlesVia::HandlerLibrary::Enum,
96 Sub::HandlesVia::HandlerLibrary::Hash,
97 Sub::HandlesVia::HandlerLibrary::Number,
98 Sub::HandlesVia::HandlerLibrary::Scalar, and
99 Sub::HandlesVia::HandlerLibrary::String.
100
102 Toby Inkster <tobyink@cpan.org>.
103
105 This software is copyright (c) 2022 by Toby Inkster.
106
107 This is free software; you can redistribute it and/or modify it under
108 the same terms as the Perl 5 programming language system itself.
109
111 THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
112 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
113 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
114
115
116
117perl v5.36.0 2023S-u0b4:-:0H6andlesVia::Manual::WithClassTiny(3)