File Coverage

blib/lib/Sub/HandlesVia/HandlerLibrary/Code.pm
Criterion Covered Total %
statement 38 38 100.0
branch n/a
condition n/a
subroutine 21 21 100.0
pod 8 8 100.0
total 67 67 100.0


line stmt bran cond sub pod time code
1 10     10   736 use 5.008;
  10         36  
2 10     10   53 use strict;
  10         23  
  10         262  
3 10     10   66 use warnings;
  10         27  
  10         693  
4              
5             package Sub::HandlesVia::HandlerLibrary::Code;
6              
7             our $AUTHORITY = 'cpan:TOBYINK';
8             our $VERSION = '0.050000';
9              
10 10     10   3392 use Sub::HandlesVia::HandlerLibrary;
  10         27  
  10         497  
11             our @ISA = 'Sub::HandlesVia::HandlerLibrary';
12              
13 10     10   71 use Sub::HandlesVia::Handler qw( handler );
  10         21  
  10         71  
14             our @METHODS = qw(
15             execute execute_method
16             execute_list execute_method_list
17             execute_scalar execute_method_scalar
18             execute_void execute_method_void
19             );
20              
21             sub execute {
22             handler
23             name => 'Code:execute',
24             template => '$GET->(@ARG)',
25             usage => '@args',
26             prefer_shift_self => 1,
27             documentation => 'Calls the coderef, passing it any arguments.',
28             _examples => sub {
29 1     1   71 my ( $class, $attr, $method ) = @_;
30 1         8 return join "",
31             " my \$coderef = sub { 'code' };\n",
32             " my \$object = $class\->new( $attr => \$coderef );\n",
33             " \n",
34             " # Calls: \$coderef->( 1, 2, 3 )\n",
35             " \$object->$method\( 1, 2, 3 );\n",
36             "\n";
37             },
38 40     40 1 374 }
39              
40             sub execute_method {
41             handler
42             name => 'Code:execute_method',
43             template => '$GET->($SELF, @ARG)',
44             prefer_shift_self => 1,
45             usage => '@args',
46             documentation => 'Calls the coderef as if it were a method, passing any arguments.',
47             _examples => sub {
48 1     1   65 my ( $class, $attr, $method ) = @_;
49 1         7 return join "",
50             " my \$coderef = sub { 'code' };\n",
51             " my \$object = $class\->new( $attr => \$coderef );\n",
52             " \n",
53             " # Calls: \$coderef->( \$object, 1, 2, 3 )\n",
54             " \$object->$method\( 1, 2, 3 );\n",
55             "\n";
56             },
57 22     22 1 191 }
58              
59             sub execute_list {
60             handler
61             name => 'Code:execute_list',
62             template => 'my @shv_list = $GET->(@ARG); wantarray ? @shv_list : \@shv_list',
63             usage => '@args',
64             prefer_shift_self => 1,
65             documentation => 'Calls the coderef, passing it any arguments, and forcing list context. If called in scalar context, returns an arrayref.',
66             _examples => sub {
67 1     1   64 my ( $class, $attr, $method ) = @_;
68 1         12 return join "",
69             " my \$context;\n",
70             " my \$coderef = sub { \$context = wantarray(); 'code' };\n",
71             " my \$object = $class\->new( $attr => \$coderef );\n",
72             " \n",
73             " # Calls: \$coderef->( 1, 2, 3 )\n",
74             " my \$result = \$object->$method\( 1, 2, 3 );\n",
75             " \n",
76             " say Dumper( \$result ); ## ==> [ 'code' ]\n",
77             " say \$context; ## ==> true\n",
78             "\n";
79             },
80 3     3 1 34 }
81              
82             sub execute_method_list {
83             handler
84             name => 'Code:execute_method_list',
85             template => 'my @shv_list = $GET->($SELF, @ARG); wantarray ? @shv_list : \@shv_list',
86             prefer_shift_self => 1,
87             usage => '@args',
88             documentation => 'Calls the coderef as if it were a method, passing any arguments, and forcing list context. If called in scalar context, returns an arrayref.',
89             _examples => sub {
90 1     1   64 my ( $class, $attr, $method ) = @_;
91 1         9 return join "",
92             " my \$context;\n",
93             " my \$coderef = sub { \$context = wantarray(); 'code' };\n",
94             " my \$object = $class\->new( $attr => \$coderef );\n",
95             " \n",
96             " # Calls: \$coderef->( \$object, 1, 2, 3 )\n",
97             " my \$result = \$object->$method\( 1, 2, 3 );\n",
98             " \n",
99             " say Dumper( \$result ); ## ==> [ 'code' ]\n",
100             " say \$context; ## ==> true\n",
101             "\n";
102             },
103 3     3 1 44 }
104              
105             sub execute_scalar {
106             handler
107             name => 'Code:execute_scalar',
108             template => 'scalar( $GET->(@ARG) )',
109             usage => '@args',
110             prefer_shift_self => 1,
111             documentation => 'Calls the coderef, passing it any arguments, and forcing scalar context.',
112             _examples => sub {
113 1     1   90 my ( $class, $attr, $method ) = @_;
114 1         11 return join "",
115             " my \$context;\n",
116             " my \$coderef = sub { \$context = wantarray(); 'code' };\n",
117             " my \$object = $class\->new( $attr => \$coderef );\n",
118             " \n",
119             " # Calls: \$coderef->( 1, 2, 3 )\n",
120             " my \$result = \$object->$method\( 1, 2, 3 );\n",
121             " \n",
122             " say \$result; ## ==> 'code'\n",
123             " say \$context; ## ==> false\n",
124             "\n";
125             },
126 3     3 1 51 }
127              
128             sub execute_method_scalar {
129             handler
130             name => 'Code:execute_method_scalar',
131             template => 'scalar( $GET->($SELF, @ARG) )',
132             prefer_shift_self => 1,
133             usage => '@args',
134             documentation => 'Calls the coderef as if it were a method, passing any arguments, and forcing scalar context.',
135             _examples => sub {
136 1     1   91 my ( $class, $attr, $method ) = @_;
137 1         36 return join "",
138             " my \$context;\n",
139             " my \$coderef = sub { \$context = wantarray(); 'code' };\n",
140             " my \$object = $class\->new( $attr => \$coderef );\n",
141             " \n",
142             " # Calls: \$coderef->( \$object, 1, 2, 3 )\n",
143             " my \$result = \$object->$method\( 1, 2, 3 );\n",
144             " \n",
145             " say \$result; ## ==> 'code'\n",
146             " say \$context; ## ==> false\n",
147             "\n";
148             },
149 3     3 1 33 }
150              
151             sub execute_void {
152             handler
153             name => 'Code:execute_void',
154             template => '$GET->(@ARG); undef',
155             usage => '@args',
156             prefer_shift_self => 1,
157             documentation => 'Calls the coderef, passing it any arguments, and forcing void context. Returns undef.',
158             _examples => sub {
159 1     1   95 my ( $class, $attr, $method ) = @_;
160 1         9 return join "",
161             " my \$context;\n",
162             " my \$coderef = sub { \$context = wantarray(); 'code' };\n",
163             " my \$object = $class\->new( $attr => \$coderef );\n",
164             " \n",
165             " # Calls: \$coderef->( 1, 2, 3 )\n",
166             " my \$result = \$object->$method\( 1, 2, 3 );\n",
167             " \n",
168             " say \$result; ## ==> undef\n",
169             " say \$context; ## ==> undef\n",
170             "\n";
171             },
172 3     3 1 34 }
173              
174             sub execute_method_void {
175             handler
176             name => 'Code:execute_method_void',
177             template => '$GET->($SELF, @ARG); undef',
178             prefer_shift_self => 1,
179             usage => '@args',
180             documentation => 'Calls the coderef as if it were a method, passing any arguments, and forcing void context. Returns undef.',
181             _examples => sub {
182 1     1   145 my ( $class, $attr, $method ) = @_;
183 1         9 return join "",
184             " my \$context;\n",
185             " my \$coderef = sub { \$context = wantarray(); 'code' };\n",
186             " my \$object = $class\->new( $attr => \$coderef );\n",
187             " \n",
188             " # Calls: \$coderef->( \$object, 1, 2, 3 )\n",
189             " my \$result = \$object->$method\( 1, 2, 3 );\n",
190             " \n",
191             " say \$result; ## ==> undef\n",
192             " say \$context; ## ==> undef\n",
193             "\n";
194             },
195 3     3 1 49 }
196              
197             1;