1Sub::HandlesVia::ManualU:s:eWritChoOnbtjreicbtuPtaeddS(u3Pb)e:r:lHaDnodcluemseVnitaa:t:iMoannual::WithObjectPad(3)
2
3
4
6 Sub::HandlesVia::Manual::WithObjectPad - using Sub::HandlesVia with
7 Object::Pad
8
10 use Object::Pad;
11
12 class Kitchen {
13 has @foods;
14 use Sub::HandlesVia::Declare '@foods', Array => (
15 all_foods => 'all',
16 add_food => 'push',
17 );
18 }
19
21 Sub::HandlesVia allows you to delegate methods from your class to the
22 values of your objects' attributes.
23
24 Conceptually, it allows you to define "$object->push_number($n)" to be
25 a shortcut for "$object->numbers->push($n)" except that
26 "$object->numbers" is an arrayref, so doesn't have methods you can call
27 on it like "push".
28
29 Experimental Object::Pad support is provided by Sub::HandlesVia,
30 including for array and hash fields.
31
32 Object::Pad classes are "sealed" after compile time, preventing new
33 methods from being installed at run-time. (It's actually trivially easy
34 to add them at runtime, but not using the API provided by the
35 Object::Pad MOP, so we don't do that.) This is why
36 Sub::Handles::Declare is used, as it creates delegated methods at
37 compile-time.
38
39 The first argument is the field name, including its sigil.
40
41 The second argument is the handler library. If the field name begins
42 with a '@' or '%', this is implied to be "Array"/"Hash".
43
44 use Object::Pad;
45
46 class Kitchen {
47 has @foods;
48 use Sub::HandlesVia::Declare '@foods', (
49 all_foods => 'all',
50 add_food => 'push',
51 );
52 }
53
54 The third argument is a hash of methods to delegate.
55
56 Which Methods Can Be Delegated To?
57 The second parameter ("Array" in the synopsis) indicates which library
58 of methods should be available. Valid values include Array, Blessed,
59 Bool, Code, Counter, Hash, Number, Scalar, and String.
60
61 An arrayref can be provided, though many of the options are
62 conceptually contradictory.
63
64 use Sub::HandlesVia::Declare 'num', [ 'Number', 'Scalar' ] => (
65 ...,
66 );
67
69 Please report any bugs to
70 <https://github.com/tobyink/p5-sub-handlesvia/issues>.
71
73 Misc advanced documentation: Sub::HandlesVia::Manual::Advanced.
74
75 Sub::HandlesVia, Sub::HandlesVia::Declare.
76
77 Documentation for delegatable methods:
78 Sub::HandlesVia::HandlerLibrary::Array,
79 Sub::HandlesVia::HandlerLibrary::Blessed,
80 Sub::HandlesVia::HandlerLibrary::Bool,
81 Sub::HandlesVia::HandlerLibrary::Code,
82 Sub::HandlesVia::HandlerLibrary::Counter,
83 Sub::HandlesVia::HandlerLibrary::Hash,
84 Sub::HandlesVia::HandlerLibrary::Number,
85 Sub::HandlesVia::HandlerLibrary::Scalar, and
86 Sub::HandlesVia::HandlerLibrary::String.
87
89 Toby Inkster <tobyink@cpan.org>.
90
92 This software is copyright (c) 2022 by Toby Inkster.
93
94 This is free software; you can redistribute it and/or modify it under
95 the same terms as the Perl 5 programming language system itself.
96
98 THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
99 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
100 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
101
102
103
104perl v5.36.0 2022S-u1b2:-:1H7andlesVia::Manual::WithObjectPad(3)