blib/lib/Sub/Multi/Tiny/Util.pm

filter: Statementstmt 93.4% 86 / 92 Branchbran 50.0% 10 / 20 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 88.8% 16 / 18 Podpod n/a totaltotal 84.0% 116 / 138 CC 14 SCAR 12.0
CC
14
Cov
85%
_line_mark_string
8.5
_hlog
6.1
_complete_dispatcher
3.0
CRAP 3.3
« blib/lib/Sub/Multi/Tiny/Dispatcher/Default.pm ↑ Summary blib/lib/Sub/Multi/Tiny/SigParse.pm »
1 package Sub::Multi::Tiny::Util;
2  
3 13 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 13 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use vars::i [
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 '$VERBOSE' => 0, # Set this to a positive int for extra output on STDERR
10 '@EXPORT' => [],
11 '@EXPORT_OK' => [qw(_carp _croak _hlog _line_mark_string
12 _make_positional_copier _complete_dispatcher
13 *VERBOSE)],
14 13 ];
15 12 ▶use vars::i '%EXPORT_TAGS' => { all => [@EXPORT, @EXPORT_OK] };
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 12 our $VERSION = '0.000013';
18  
19  
20 # Documentation {{{1
21  
22 =head1 NAME
23  
24 Sub::Multi::Tiny::Util - Internal utilities for Sub::Multi::Tiny
25  
26 =head1 SYNOPSIS
27  
28 Used by L<Sub::Multi::Tiny>.
29  
30 =head1 VARIABLES
31  
32 =head2 $VERBOSE
33  
34 Set this truthy for extra debug output. L<Sub::Multi::Tiny/import> sets this
35 based on environment variable C<SUB_MULTI_TINY_VERBOSE>.
36  
37 =head1 FUNCTIONS
38  
39 =cut
40  
41 # }}}1
42  
43 =head2 _croak
44  
45 As L<Carp/croak>, but lazily loads L<Carp>.
46  
47 =cut
48  
49 0 ▶sub _croak {
Subroutine _croak: not called CC=1 SCAR=6.9
50 0 require Carp;
51 0 goto &Carp::croak;
52 }
53  
54 =head2 _carp
55  
56 As L<Carp/carp>, but lazily loads L<Carp>.
57  
58 =cut
59  
60 0 ▶sub _carp {
Subroutine _carp: not called CC=1 SCAR=6.9
61 0 require Carp;
62 0 goto &Carp::carp;
63 }
64  
65 =head2 _line_mark_string
66  
67 Add a C<#line> directive to a string. Usage:
68  
69 my $str = _line_mark_string <<EOT ;
70 $contents
71 EOT
72  
73 or
74  
75 my $str = _line_mark_string __FILE__, __LINE__, <<EOT ;
76 $contents
77 EOT
78  
79 In the first form, information from C<caller> will be used for the filename
80 and line number.
81  
82 The C<#line> directive will point to the line after the C<_line_mark_string>
83 invocation, i.e., the first line of C<$contents>. Generally, C<$contents> will
84 be source code, although this is not required.
85  
86 C<$contents> must be defined, but can be empty.
87  
88 =cut
89  
90 64 ▶sub _line_mark_string {
Subroutine _line_mark_string: called CC=6 SCAR=21.4
91 64 my ($contents, $filename, $line);
92 64 ▶ if(@_ == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1) { } 64 0 64
elsif (@_ == 3) { } 0 0 0
93 64 $contents = $_[0];
94 64 (undef, $filename, $line) = caller;
95 } elsif(@_ == 3) {
96 0 ($filename, $line, $contents) = @_;
97 } else {
98 0 _croak "Invalid invocation";
99 }
100  
101 64 ▶ _croak "Need text" unless defined $contents;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $contents 0 64 64
102 64 ▶ die "Couldn't get location information" unless $filename && $line;
Branch: 1 branch
BranchTrueFalseTotal
unless $filename and $line 0 64 64
Condition33%1/3
$filename and $line
!ll&&!rl&&r
0064
MC/DC0%0/2
$filename and $line
$filename $line
Truth table33%1/3
A and B
A= $filenameB= $line
ABresult
0X0
100
111
103  
104 64 $filename =~ s/"/-/g;
105 64 ++$line;
106  
107 64 return <<EOT;
108 #line $line "$filename"
109 $contents
110 EOT
111 } #_line_mark_string()
112  
113 =head2 _hlog
114  
115 Log information if L</$VERBOSE> is set. Usage:
116  
117 _hlog { <list of things to log> } [optional min verbosity level (default 1)];
118  
119 The items in the list are joined by C<' '> on output, and a C<'\n'> is added.
120 Each line is prefixed with C<'# '> for the benefit of test runs.
121  
122 The list is in C<{}> so that it won't be evaluated if logging is turned off.
123 It is a full block, so you can run arbitrary code to decide what to log.
124 If the block returns an empty list, C<_hlog> will not produce any output.
125 However, if the block returns at least one element, C<_hlog> will produce at
126 least a C<'# '>.
127  
128 The message will be output only if L</$VERBOSE> is at least the given minimum
129 verbosity level (1 by default).
130  
131 If C<< $VERBOSE > 2 >>, the filename and line from which C<_hlog> was called
132 will also be printed.
133  
134 B<Caution:> Within the C<{ }> block, C<@_> is the arguments I<to that block>,
135 not the arguments to the calling function. To log C<@_>, use something like:
136  
137 my $argref = \@_;
138 _hlog { @$argref };
139  
140 =cut
141  
142 79 ▶sub _hlog (&;$) {
Subroutine _hlog: called CC=6 SCAR=18.1
143 79 ▶ return unless $VERBOSE >= ($_[1] || 1);
Branch: 1 branch
BranchTrueFalseTotal
unless $Sub::Multi::Tiny::Util::VERBOSE >= ($_[1] || 1) 58 21 79
Condition100%2/2
$_[1] || 1
l!l
5029
MC/DC100%1/1
$_[1] || 1
$_[1]
Truth table100%2/2
A
A= $_[1]
Aresult
11
00
144  
145 21 my @log = &{$_[0]}();
146 21 ▶ return unless @log;
Branch: 1 branch
BranchTrueFalseTotal
unless @log 0 21 21
147  
148 21 ▶ chomp $log[$#log] if $log[$#log];
Branch: 1 branch
BranchTrueFalseTotal
if $log[$#log] 21 0 21
149 # TODO add an option to number the lines of the output
150 21 (my $msg = join(' ', @log)) =~ s/^/# /gm;
151 21 ▶ if($VERBOSE>2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Sub::Multi::Tiny::Util::VERBOSE > 2) 21 0 21
152 21 my ($package, $filename, $line) = caller;
153 21 $msg .= " (at $filename:$line)";
154 }
155 21 print STDERR "$msg\n";
156 } #_hlog()
157  
158 =head2 _complete_dispatcher
159  
160 Makes a standard dispatcher, given code to initialize certain variables.
161 Usage:
162  
163 my $code = "..."; # See requirements below
164 my $subref = _complete_dispatcher($multisub_hashref, $code[, ...]);
165  
166 The C<$code> argument will be inlined as-is into the generated dispatcher.
167 The C<$code> must:
168  
169 =over
170  
171 =item *
172  
173 Pick which multisub candidate to use, given args in C<@_>;
174  
175 =item *
176  
177 Put the subref of that candidate in C<$candidate>; and
178  
179 =item *
180  
181 Put a subref in C<$copier> of a routine that will copy from C<@_> into
182 the package variables created by L<Sub::Multi::Tiny/import>.
183  
184 Any arguments to C<_complete_dispatcher> after C<$code> are saved in C<my @data>,
185 which C<$code> can access.
186  
187 =back
188  
189 C<$code> is run under L<strict> and L<warnings> by default. If you don't
190 want those, you need to expressly turn them off.
191  
192 =cut
193  
194 1 ▶sub _complete_dispatcher {
Subroutine _complete_dispatcher: called CC=3 SCAR=11.0
195 1 my ($hr, $inner_code, @data) = @_;
196 1 my $argref = \@_;
197 1 my $caller = caller;
198 1 ▶ _hlog { require Data::Dumper;
Subroutine __ANON__: called CC=1 SCAR=0.0
199 1 "_complete_dispatcher making $caller dispatcher with args:",
200 1 Data::Dumper->Dump($argref, [qw(multisub inner_code data)]) };
201  
202 # Make the dispatcher
203 1 my $code = _line_mark_string <<EOT;
204 use strict;
205 use warnings;
206 sub {
207 # Find the candidate
208 my (\$candidate, \$copier);
209  
210 $inner_code
211  
212 # Save the present values of the parameters
213 EOT
214  
215 1 my $restore = '';
216 1 foreach(keys %{$hr->{possible_params}}) {
217 1 my ($sigil, $name) = /^(.)(.+)$/;
218 1 $code .= _line_mark_string
219 "my ${sigil}saved_${name} = ${sigil}$hr->{defined_in}\::${name};\n";
220 1 $restore .= _line_mark_string
221 "${sigil}$hr->{defined_in}\::${name} = ${sigil}saved_${name};\n";
222 }
223  
224 1 $code .= _line_mark_string <<EOT;
225 # Create the guard
226 my \$guard = Guard::guard {
227  
228 $restore
229  
230 }; #End of guard
231 EOT
232  
233 1 $code .= _line_mark_string <<'EOT';
234  
235 # Copy the parameters into the variables the candidate
236 # will access them from
237 &$copier; # $copier gets @_ automatically
238  
239 # Pass the guard so the parameters will be reset once \$candidate
240 # finishes running.
241 @_ = ($guard);
242  
243 # Invoke the selected candidate
244 goto &$candidate;
245 } #dispatcher
246 EOT
247  
248 1 ▶ _hlog { $caller, "dispatcher for $hr->{defined_in}\():\n$code\n" } 2;
Subroutine __ANON__: called CC=1 SCAR=0.0
249 1 my $sub = eval $code;
250 1 ▶ die "Could not create dispatcher for $hr->{defined_in}: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
251 1 return $sub;
252 } # _complete_dispatcher
253  
254 =head2 _make_positional_copier
255  
256 Make a sub to copy from @_ into package variables. The resulting sub copies
257 positional parameters. Usage:
258  
259 my $coderef = _make_positional_copier($defined_in, $impl_hashref);
260  
261 The copier is run under L<strict> and L<warnings>, for what it's worth.
262  
263 =cut
264  
265 12 our $_positional_copier_invocation_number = 0; # DEBUG
266 17 ▶sub _make_positional_copier {
Subroutine _make_positional_copier: called CC=2 SCAR=6.9
267 5 my ($defined_in, $impl) = @_;
268 5 my $argref = \@_; # For hlogging
269  
270 5 my @vars; #DEBUG
271  
272 2 ▶ _hlog { require Data::Dumper;
Subroutine __ANON__: called CC=1 SCAR=0.0
273 17 Data::Dumper->Dump($argref,[qw(mpc_defined_in mpc_impl)]) } 2;
274  
275 17 my $code = _line_mark_string <<'EOT';
276 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
277 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
278 sub {
279 EOT
280  
281 # XXX DEBUG: Some extra output to try to debug failures on earlier Perls.
282 17 $code .= _line_mark_string <<'EOT';
283 if( $] lt '5.018' || $VERBOSE > 1) {
284 require Data::Dumper;
285 require Test::More;
286 Test::More::diag(sprintf("Positional copier invocation %d:\n%s",
287 ++$Sub::Multi::Tiny::Util::_positional_copier_invocation_number,
288 Data::Dumper->Dump([\@_],['copier_args'])));
289 }
290 EOT
291  
292 17 $code .= _line_mark_string <<'EOT';
293 (
294 EOT
295  
296 @vars = map {
297 17 my ($sigil, $name) = $_->{name} =~ m/^(.)(.+)$/;
298 5 "${sigil}$defined_in\::${name}"
299 17 } @{$impl->{args}};
300  
301 $code .= join ",\n",
302 5 map { _line_mark_string
303 " $_" } @vars;
304  
305 5 $code .= _line_mark_string <<'EOT';
306 ) = @_;
307  
308 if( $] lt '5.018' || $VERBOSE > 1) {
309 Test::More::diag(sprintf("After positional copier invocation %d:",
310 $Sub::Multi::Tiny::Util::_positional_copier_invocation_number));
311 Test::More::diag(join "\n", map {
312 sprintf("%s = %s", $_, eval($_))
313 } @vars);
314 }
315  
316 } #copier
317 EOT
318  
319 5 ▶ _hlog { "Copier for $impl->{candidate_name}\():\n", $code } 2;
Subroutine __ANON__: called CC=1 SCAR=0.0
320 5 my $sub = eval $code;
321 5 ▶ die "Could not create copier for $impl->{candidate_name}: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 5 5
322 5 return $sub;
323 } #_make_positional_copier
324  
325 12 1;
326 __END__
« blib/lib/Sub/Multi/Tiny/Dispatcher/Default.pm ↑ Summary blib/lib/Sub/Multi/Tiny/SigParse.pm »