1Sub::HandlesVia::HandleUrsLeirbrCaornyt:r:iCboudtee(d3S)Puebr:l:HDaoncdulmeesnVtiaat:i:oHnandlerLibrary::Code(3)
2
3
4
6 Sub::HandlesVia::HandlerLibrary::Code - library of code-related methods
7
9 package My::Class {
10 use Moo;
11 use Sub::HandlesVia;
12 use Types::Standard 'CodeRef';
13 has attr => (
14 is => 'rwp',
15 isa => CodeRef,
16 handles_via => 'Code',
17 handles => {
18 'my_execute' => 'execute',
19 'my_execute_list' => 'execute_list',
20 'my_execute_method' => 'execute_method',
21 'my_execute_method_list' => 'execute_method_list',
22 'my_execute_method_scalar' => 'execute_method_scalar',
23 'my_execute_method_void' => 'execute_method_void',
24 'my_execute_scalar' => 'execute_scalar',
25 'my_execute_void' => 'execute_void',
26 },
27 );
28 }
29
31 This is a library of methods for Sub::HandlesVia.
32
34 "execute( @args )"
35 Calls the coderef, passing it any arguments.
36
37 my $coderef = sub { 'code' };
38 my $object = My::Class->new( attr => $coderef );
39
40 # Calls: $coderef->( 1, 2, 3 )
41 $object->my_execute( 1, 2, 3 );
42
43 "execute_list( @args )"
44 Calls the coderef, passing it any arguments, and forcing list context.
45 If called in scalar context, returns an arrayref.
46
47 my $context;
48 my $coderef = sub { $context = wantarray(); 'code' };
49 my $object = My::Class->new( attr => $coderef );
50
51 # Calls: $coderef->( 1, 2, 3 )
52 my $result = $object->my_execute_list( 1, 2, 3 );
53
54 say Dumper( $result ); ## ==> [ 'code' ]
55 say $context; ## ==> true
56
57 "execute_method( @args )"
58 Calls the coderef as if it were a method, passing any arguments.
59
60 my $coderef = sub { 'code' };
61 my $object = My::Class->new( attr => $coderef );
62
63 # Calls: $coderef->( $object, 1, 2, 3 )
64 $object->my_execute_method( 1, 2, 3 );
65
66 "execute_method_list( @args )"
67 Calls the coderef as if it were a method, passing any arguments, and
68 forcing list context. If called in scalar context, returns an arrayref.
69
70 my $context;
71 my $coderef = sub { $context = wantarray(); 'code' };
72 my $object = My::Class->new( attr => $coderef );
73
74 # Calls: $coderef->( $object, 1, 2, 3 )
75 my $result = $object->my_execute_method_list( 1, 2, 3 );
76
77 say Dumper( $result ); ## ==> [ 'code' ]
78 say $context; ## ==> true
79
80 "execute_method_scalar( @args )"
81 Calls the coderef as if it were a method, passing any arguments, and
82 forcing scalar context.
83
84 my $context;
85 my $coderef = sub { $context = wantarray(); 'code' };
86 my $object = My::Class->new( attr => $coderef );
87
88 # Calls: $coderef->( $object, 1, 2, 3 )
89 my $result = $object->my_execute_method_scalar( 1, 2, 3 );
90
91 say $result; ## ==> 'code'
92 say $context; ## ==> false
93
94 "execute_method_void( @args )"
95 Calls the coderef as if it were a method, passing any arguments, and
96 forcing void context. Returns undef.
97
98 my $context;
99 my $coderef = sub { $context = wantarray(); 'code' };
100 my $object = My::Class->new( attr => $coderef );
101
102 # Calls: $coderef->( $object, 1, 2, 3 )
103 my $result = $object->my_execute_method_void( 1, 2, 3 );
104
105 say $result; ## ==> undef
106 say $context; ## ==> undef
107
108 "execute_scalar( @args )"
109 Calls the coderef, passing it any arguments, and forcing scalar
110 context.
111
112 my $context;
113 my $coderef = sub { $context = wantarray(); 'code' };
114 my $object = My::Class->new( attr => $coderef );
115
116 # Calls: $coderef->( 1, 2, 3 )
117 my $result = $object->my_execute_scalar( 1, 2, 3 );
118
119 say $result; ## ==> 'code'
120 say $context; ## ==> false
121
122 "execute_void( @args )"
123 Calls the coderef, passing it any arguments, and forcing void context.
124 Returns undef.
125
126 my $context;
127 my $coderef = sub { $context = wantarray(); 'code' };
128 my $object = My::Class->new( attr => $coderef );
129
130 # Calls: $coderef->( 1, 2, 3 )
131 my $result = $object->my_execute_void( 1, 2, 3 );
132
133 say $result; ## ==> undef
134 say $context; ## ==> undef
135
137 Using execute_method
138 The execute_method handler allows a class to effectively provide
139 certain methods which can be overridden by parameters in the
140 constructor.
141
142 use strict;
143 use warnings;
144 use Data::Dumper;
145
146 package My::Processor {
147 use Moo;
148 use Sub::HandlesVia;
149 use Types::Standard qw( Str CodeRef );
150
151 has name => (
152 is => 'ro',
153 isa => Str,
154 default => 'Main Process',
155 );
156
157 my $NULL_CODEREF = sub {};
158
159 has _debug => (
160 is => 'ro',
161 isa => CodeRef,
162 handles_via => 'Code',
163 handles => { debug => 'execute_method' },
164 default => sub { $NULL_CODEREF },
165 init_arg => 'debug',
166 );
167
168 sub _do_stuff {
169 my $self = shift;
170 $self->debug( 'continuing process' );
171 return;
172 }
173
174 sub run_process {
175 my $self = shift;
176 $self->debug( 'starting process' );
177 $self->_do_stuff;
178 $self->debug( 'ending process' );
179 }
180 }
181
182 my $p1 = My::Processor->new( name => 'First Process' );
183 $p1->run_process; # no output
184
185 my @got;
186 my $p2 = My::Processor->new(
187 name => 'Second Process',
188 debug => sub {
189 my ( $processor, $message ) = @_;
190 push @got, sprintf( '%s: %s', $processor->name, $message );
191 },
192 );
193 $p2->run_process; # logged output
194
195 my @expected = (
196 'Second Process: starting process',
197 'Second Process: continuing process',
198 'Second Process: ending process',
199 );
200 say Dumper( \@got ); ## ==> \@expected
201
203 Please report any bugs to
204 <https://github.com/tobyink/p5-sub-handlesvia/issues>.
205
207 Sub::HandlesVia.
208
210 Toby Inkster <tobyink@cpan.org>.
211
213 This software is copyright (c) 2020, 2022 by Toby Inkster.
214
215 This is free software; you can redistribute it and/or modify it under
216 the same terms as the Perl 5 programming language system itself.
217
219 THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
220 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
221 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
222
223
224
225perl v5.36.0 2022-S1u2b-:1:7HandlesVia::HandlerLibrary::Code(3)