1Sub::HandlesVia::ManualU:s:eWritChoMnotor(i3b)uted PerlSDuobc:u:mHeanntdalteisoVnia::Manual::WithMoo(3)
2
3
4
6 Sub::HandlesVia::Manual::WithMoo - using Sub::HandlesVia with Moo
7
9 package Kitchen {
10 use Moo;
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
27 Sub::HandlesVia allows you to delegate methods from your class to the
28 values of your objects' attributes.
29
30 Conceptually, it allows you to define "$object->push_number($n)" to be
31 a shortcut for "$object->numbers->push($n)" except that
32 "$object->numbers" is an arrayref, so doesn't have methods you can call
33 on it like "push".
34
35 You should be able to use Sub::HandlesVia as a drop-in replacement for
36 MooX::HandlesVia, which provides a similar feature, though has a more
37 limited implementation. Sub::HandlesVia honours type constraints, plus
38 it can mutate non-reference values.
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 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
51 Please report any bugs to
52 <https://github.com/tobyink/p5-sub-handlesvia/issues>.
53
55 Misc advanced documentation: Sub::HandlesVia::Manual::Advanced.
56
57 Sub::HandlesVia.
58
59 Documentation for delegatable methods:
60 Sub::HandlesVia::HandlerLibrary::Array,
61 Sub::HandlesVia::HandlerLibrary::Blessed,
62 Sub::HandlesVia::HandlerLibrary::Bool,
63 Sub::HandlesVia::HandlerLibrary::Code,
64 Sub::HandlesVia::HandlerLibrary::Counter,
65 Sub::HandlesVia::HandlerLibrary::Hash,
66 Sub::HandlesVia::HandlerLibrary::Number,
67 Sub::HandlesVia::HandlerLibrary::Scalar, and
68 Sub::HandlesVia::HandlerLibrary::String.
69
71 Toby Inkster <tobyink@cpan.org>.
72
74 This software is copyright (c) 2022 by Toby Inkster.
75
76 This is free software; you can redistribute it and/or modify it under
77 the same terms as the Perl 5 programming language system itself.
78
80 THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
81 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
82 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
83
84
85
86perl v5.36.0 2022-12-17Sub::HandlesVia::Manual::WithMoo(3)