File: Code.pod

package info (click to toggle)
libsub-handlesvia-perl 0.052000-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,740 kB
  • sloc: perl: 9,645; makefile: 2
file content (229 lines) | stat: -rw-r--r-- 6,112 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
=head1 NAME

Sub::HandlesVia::HandlerLibrary::Code - library of code-related methods

=head1 SYNOPSIS

  package My::Class {
    use Moo;
    use Sub::HandlesVia;
    use Types::Standard 'CodeRef';
    has attr => (
      is => 'rwp',
      isa => CodeRef,
      handles_via => 'Code',
      handles => {
        'my_execute' => 'execute',
        'my_execute_list' => 'execute_list',
        'my_execute_method' => 'execute_method',
        'my_execute_method_list' => 'execute_method_list',
        'my_execute_method_scalar' => 'execute_method_scalar',
        'my_execute_method_void' => 'execute_method_void',
        'my_execute_scalar' => 'execute_scalar',
        'my_execute_void' => 'execute_void',
      },
    );
  }

=head1 DESCRIPTION

This is a library of methods for L<Sub::HandlesVia>.

=head1 DELEGATABLE METHODS

=head2 C<< execute( @args ) >>

Calls the coderef, passing it any arguments.

  my $coderef = sub { 'code' };
  my $object  = My::Class->new( attr => $coderef );
  
  # Calls: $coderef->( 1, 2, 3 )
  $object->my_execute( 1, 2, 3 );

=head2 C<< execute_list( @args ) >>

Calls the coderef, passing it any arguments, and forcing list context. If called in scalar context, returns an arrayref.

  my $context;
  my $coderef = sub { $context = wantarray(); 'code' };
  my $object  = My::Class->new( attr => $coderef );
  
  # Calls: $coderef->( 1, 2, 3 )
  my $result = $object->my_execute_list( 1, 2, 3 );
  
  say Dumper( $result );  ## ==> [ 'code' ]
  say $context;           ## ==> true

=head2 C<< execute_method( @args ) >>

Calls the coderef as if it were a method, passing any arguments.

  my $coderef = sub { 'code' };
  my $object  = My::Class->new( attr => $coderef );
  
  # Calls: $coderef->( $object, 1, 2, 3 )
  $object->my_execute_method( 1, 2, 3 );

=head2 C<< execute_method_list( @args ) >>

Calls the coderef as if it were a method, passing any arguments, and forcing list context. If called in scalar context, returns an arrayref.

  my $context;
  my $coderef = sub { $context = wantarray(); 'code' };
  my $object  = My::Class->new( attr => $coderef );
  
  # Calls: $coderef->( $object, 1, 2, 3 )
  my $result = $object->my_execute_method_list( 1, 2, 3 );
  
  say Dumper( $result );  ## ==> [ 'code' ]
  say $context;           ## ==> true

=head2 C<< execute_method_scalar( @args ) >>

Calls the coderef as if it were a method, passing any arguments, and forcing scalar context.

  my $context;
  my $coderef = sub { $context = wantarray(); 'code' };
  my $object  = My::Class->new( attr => $coderef );
  
  # Calls: $coderef->( $object, 1, 2, 3 )
  my $result = $object->my_execute_method_scalar( 1, 2, 3 );
  
  say $result;  ## ==> 'code'
  say $context; ## ==> false

=head2 C<< execute_method_void( @args ) >>

Calls the coderef as if it were a method, passing any arguments, and forcing void context. Returns undef.

  my $context;
  my $coderef = sub { $context = wantarray(); 'code' };
  my $object  = My::Class->new( attr => $coderef );
  
  # Calls: $coderef->( $object, 1, 2, 3 )
  my $result = $object->my_execute_method_void( 1, 2, 3 );
  
  say $result;  ## ==> undef
  say $context; ## ==> undef

=head2 C<< execute_scalar( @args ) >>

Calls the coderef, passing it any arguments, and forcing scalar context.

  my $context;
  my $coderef = sub { $context = wantarray(); 'code' };
  my $object  = My::Class->new( attr => $coderef );
  
  # Calls: $coderef->( 1, 2, 3 )
  my $result = $object->my_execute_scalar( 1, 2, 3 );
  
  say $result;  ## ==> 'code'
  say $context; ## ==> false

=head2 C<< execute_void( @args ) >>

Calls the coderef, passing it any arguments, and forcing void context. Returns undef.

  my $context;
  my $coderef = sub { $context = wantarray(); 'code' };
  my $object  = My::Class->new( attr => $coderef );
  
  # Calls: $coderef->( 1, 2, 3 )
  my $result = $object->my_execute_void( 1, 2, 3 );
  
  say $result;  ## ==> undef
  say $context; ## ==> undef

=head1 EXTENDED EXAMPLES

=head2 Using execute_method

The execute_method handler allows a class to effectively provide certain methods which can be overridden by parameters in the constructor.

  use strict;
  use warnings;
  use Data::Dumper;
  
  package My::Processor {
    use Moo;
    use Sub::HandlesVia;
    use Types::Standard qw( Str CodeRef );
    
    has name => (
      is => 'ro',
      isa => Str,
      default => 'Main Process',
    );
    
    my $NULL_CODEREF = sub {};
    
    has _debug => (
      is => 'ro',
      isa => CodeRef,
      handles_via => 'Code',
      handles => { debug => 'execute_method' },
      default => sub { $NULL_CODEREF },
      init_arg => 'debug',
    );
    
    sub _do_stuff {
      my $self = shift;
      $self->debug( 'continuing process' );
      return;
    }
    
    sub run_process {
      my $self = shift;
      $self->debug( 'starting process' );
      $self->_do_stuff;
      $self->debug( 'ending process' );
    }
  }
  
  my $p1 = My::Processor->new( name => 'First Process' );
  $p1->run_process; # no output
  
  my @got;
  my $p2 = My::Processor->new(
    name => 'Second Process',
    debug => sub {
      my ( $processor, $message ) = @_;
      push @got, sprintf( '%s: %s', $processor->name, $message );
    },
  );
  $p2->run_process; # logged output
  
  my @expected = (
    'Second Process: starting process',
    'Second Process: continuing process',
    'Second Process: ending process',
  );
  say Dumper( \@got ); ## ==> \@expected

=head1 BUGS

Please report any bugs to
L<https://github.com/tobyink/p5-sub-handlesvia/issues>.

=head1 SEE ALSO

L<Sub::HandlesVia>.

=head1 AUTHOR

Toby Inkster E<lt>tobyink@cpan.orgE<gt>.

=head1 COPYRIGHT AND LICENCE

This software is copyright (c) 2020, 2022 by Toby Inkster.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=head1 DISCLAIMER OF WARRANTIES

THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.