1Sub::HandlesVia::ManualU:s:eWritChoGnetnreirbiuct(e3d)PSeurbl::DHoacnudmleenstVaitai:o:nManual::WithGeneric(3)
2
3
4
6 Sub::HandlesVia::Manual::WithGeneric - using Sub::HandlesVia with
7 generic Perl classes
8
10 Sub::HandlesVia allows you to delegate methods from your class to the
11 values of your objects' attributes.
12
13 Conceptually, it allows you to define "$object->push_number($n)" to be
14 a shortcut for "$object->numbers->push($n)" except that
15 "$object->numbers" is an arrayref, so doesn't have methods you can call
16 on it like "push".
17
18 For Moose and Mouse, Sub::HandlesVia can use their metaobject protocols
19 to grab an attribute's definition and install the methods it needs to.
20 For Moo, it can wrap "has" and do its stuff that way. For other
21 classes, you need to be more explicit and tell it what methods to
22 delegate to what attributes.
23
24 package Kitchen {
25
26 # constructor
27 sub new {
28 my ( $class, %arg ) = @_;
29 $arg{food} ||= [];
30 return bless( \%arg, $class );
31 }
32
33 # getter/setter for `food`
34 sub food {
35 (@_ == 1) ? $_[0]{food} : ( $_[0]{food} = $_[1] );
36 }
37
38 use Sub::HandlesVia qw( delegations );
39
40 delegations(
41 attribute => 'food'
42 handles_via => 'Array',
43 handles => {
44 'add_food' => 'push',
45 'find_food' => 'grep',
46 },
47 );
48 }
49
50 Setting "attribute" to "food" means that when Sub::HandlesVia needs to
51 get the food list, it will call "$kitchen->food" and when it needs to
52 set the food list, it will call "$kitchen->food($value)". If you have
53 separate getter and setter methods, just do:
54
55 attribute => [ 'get_food', 'set_food' ],
56
57 Or if you don't have any accessors and want Sub::HandlesVia to directly
58 access the underlying hashref:
59
60 attribute => '{food}',
61
62 Or maybe you have a setter, but want to use hashref access for the
63 getter:
64
65 attribute => [ '{food}', 'set_food' ],
66
67 Or maybe you still want direct access for the getter, but your object
68 is a blessed arrayref instead of a blessed hashref:
69
70 attribute => [ '[7]', 'set_food' ],
71
72 Or maybe your needs are crazy unique:
73
74 attribute => [ \&getter, \&setter ],
75
76 The coderefs are passed the instance as their first argument, and the
77 setter is also passed a value to set.
78
79 Really, I don't think there's any object system that this won't work
80 for!
81
82 If you supply an arrayref with a getter and setter, it's also possible
83 to supply a third argument which is a coderef or string which will be
84 called as a method if needing to "reset" the value. This can be
85 thought of like a default or builder.
86
87 (The "delegations" function can be imported into Moo/Mouse/Moose
88 classes too, in which case the "attribute" needs to be the same
89 attribute name you passed to "has". You cannot use a arrayref, coderef,
90 hash key, or array index.)
91
93 Please report any bugs to
94 <https://github.com/tobyink/p5-sub-handlesvia/issues>.
95
97 Misc advanced documentation: Sub::HandlesVia::Manual::Advanced.
98
99 Sub::HandlesVia.
100
101 Documentation for delegatable methods:
102 Sub::HandlesVia::HandlerLibrary::Array,
103 Sub::HandlesVia::HandlerLibrary::Blessed,
104 Sub::HandlesVia::HandlerLibrary::Bool,
105 Sub::HandlesVia::HandlerLibrary::Code,
106 Sub::HandlesVia::HandlerLibrary::Counter,
107 Sub::HandlesVia::HandlerLibrary::Hash,
108 Sub::HandlesVia::HandlerLibrary::Number,
109 Sub::HandlesVia::HandlerLibrary::Scalar, and
110 Sub::HandlesVia::HandlerLibrary::String.
111
113 Toby Inkster <tobyink@cpan.org>.
114
116 This software is copyright (c) 2022 by Toby Inkster.
117
118 This is free software; you can redistribute it and/or modify it under
119 the same terms as the Perl 5 programming language system itself.
120
122 THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
123 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
124 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
125
126
127
128perl v5.36.0 2022-1S2u-b1:7:HandlesVia::Manual::WithGeneric(3)