blib/lib/Template/Perlish.pm

filter: Statementstmt 97.1% 302 / 311 Branchbran 79.4% 124 / 156 Conditioncond 89.8% 44 / 49 MC/DCmcdc 80.0% 24 / 30 Subroutinesub 100% 50 / 50 Podpod 100% 13 / 13 totaltotal 91.4% 557 / 609 SCAR 47.2
CC
104
Cov
91%
traverse
59.2
crumble
15.1
_compile_sub
12.0
CRAP 111.7
↑ Summary Build »
1 package Template::Perlish;
2  
3 # vim: ts=3 sts=3 sw=3 et ai :
4  
5 16 ▶use 5.008_000;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use constant ERROR_CONTEXT => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 14 { our $VERSION = '1.62'; }
12 15 ▶use Scalar::Util qw< blessed reftype >;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # Function-oriented interface
15 sub import {
16 15 ▶ my ($package, @list) = @_;
Subroutine import: called CC=3 SCAR=11.0
17  
18 15 for my $sub (@list) {
19 croak "subroutine '$sub' not exportable"
20 6 ▶ unless grep { $sub eq $_ } qw< crumble render traverse >;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$sub eq $_;} "crumble", "render", "traverse" 0 5 5
21  
22 6 my $caller = caller();
23  
24 15 ▶ no strict 'refs'; ## no critic (ProhibitNoStrict)
Subroutine BEGIN: called CC=1 SCAR=0.0
25 6 local $SIG{__WARN__} = \&Carp::carp;
26 6 *{$caller . q<::> . $sub} = \&{$package . q<::> . $sub};
27 } ## end for my $sub (@list)
28  
29 15 return;
30 } ## end sub import
31  
32 sub render {
33 19 ▶ my ($template, @rest) = @_;
Subroutine render: called CC=4 SCAR=13.9
Pod documented
34 19 my ($variables, %params);
35 19 ▶ if (@rest) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rest) 13 5 18
36 14 ▶ $variables = ref($rest[0]) ? shift(@rest) : {splice @rest, 0};
Branch: 1 branch
BranchTrueFalseTotal
ref $rest[0] ? : 13 0 13
37 14 ▶ %params = %{shift @rest} if @rest;
Branch: 1 branch
BranchTrueFalseTotal
if @rest 1 12 13
38 }
39 19 return __PACKAGE__->new(%params)->process($template, $variables);
40 } ## end sub render
41  
42 # Object-oriented interface
43 {
44 14 my (%preset_for, %inhibits_defaults);
45 BEGIN {
46 15 ▶ %preset_for = (
Subroutine BEGIN: called CC=1 SCAR=0.0
47 'default' => {
48 method_over_key => 0,
49 start => '[%',
50 stdout => 1,
51 stop => '%]',
52 strict_blessed => 0,
53 traverse_methods => 0,
54 utf8 => 1,
55 },
56 '1.52' => {
57 method_over_key => 1,
58 stdout => 0,
59 traverse_methods => 1,
60 },
61 );
62  
63 # some defaults are inhibited by the presence of certain input
64 # parameters. These parameters can still be put externally, though.
65 15 %inhibits_defaults = (
66 binmode => [qw< utf8 >],
67 );
68 }
69 sub new {
70 29 ▶ my $package = shift;
Subroutine new: called CC=9 SCAR=22.3
Pod documented
71  
72 29 my %external;
73 29 ▶ if (@_ == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1) { } 0 28 28
elsif (scalar(@_) % 2 == 0) { } 28 0 28
74 1 %external = %{$_[0]};
75 }
76 elsif (scalar(@_) % 2 == 0) {
77 29 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 4 28 32
78 5 my ($key, $value) = splice @_, 0, 2;
79 5 ▶ if ($key eq '-preset') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq '-preset') { } 0 4 4
80 croak "invalid preset $value in new()"
81 1 ▶ unless exists $preset_for{$value};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $preset_for{$value} 0 0 0
82 1 %external = (%external, %{$preset_for{$value}});
83 }
84 else {
85 5 $external{$key} = $value;
86 }
87 }
88 }
89 else {
90 1 croak 'invalid number of input arguments for constructor';
91 }
92  
93 # compute defaults, removing inhibitions
94 29 my %defaults =(%{$preset_for{'default'}}, variables => {});
95 29 for my $inhibitor (keys %inhibits_defaults) {
96 29 ▶ next unless exists $external{$inhibitor};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $external{$inhibitor} 27 1 28
97 2 delete $defaults{$_} for @{$inhibits_defaults{$inhibitor}};
98 }
99  
100 29 return bless {%defaults, %external}, $package;
101 } ## end sub new
102 }
103  
104 14 sub process {
105 74 ▶ my ($self, $template, $vars) = @_;
Subroutine process: called CC=1 SCAR=0.0
Pod documented
106 74 return $self->evaluate($self->compile($template), $vars);
107 }
108  
109 sub evaluate {
110 74 ▶ my ($self, $compiled, $vars) = @_;
Subroutine evaluate: called CC=2 SCAR=7.1
Pod documented
111 $self->_compile_sub($compiled)
112 74 ▶ unless exists $compiled->{sub};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $compiled->{"sub"} 0 73 73
113 74 return $compiled->{sub}->($vars);
114 } ## end sub evaluate
115  
116 sub compile { ## no critic (RequireArgUnpacking)
117 77 ▶ my ($self, undef, %args) = @_;
Subroutine compile: called CC=2 SCAR=7.0
Pod documented
118 77 my $outcome = $self->_compile_code_text($_[1]);
119 77 ▶ return $outcome if $args{no_check};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"no_check"} 0 76 76
120 77 return $self->_compile_sub($outcome);
121 } ## end sub compile
122  
123 sub compile_as_sub { ## no critic (RequireArgUnpacking)
124 4 ▶ my $self = shift;
Subroutine compile_as_sub: called CC=1 SCAR=0.0
Pod documented
125 4 return $self->compile($_[0])->{'sub'};
126 }
127  
128 sub _compile_code_text {
129 77 ▶ my ($self, $template) = @_;
Subroutine _compile_code_text: called CC=11 SCAR=24.0
130  
131 77 my $starter = $self->{start};
132 77 my $stopper = $self->{stop};
133  
134 77 my $compiled = "# line 1 'input'\n";
135 77 ▶ $compiled .= "use utf8;\n\n" if $self->{utf8};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"utf8"} 74 2 76
136 77 $compiled .= "P('');\n\n";
137 77 my $pos = 0;
138 77 my $line_no = 1;
139 77 ▶ while ($pos < length $template) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < length $template) 143 20 163
140  
141 # Find starter and emit all previous text as simple text
142 144 my $start = index $template, $starter, $pos;
143 144 ▶ last if $start < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $start < 0 56 87 143
144 88 my $chunk = substr $template, $pos, $start - $pos;
145 88 ▶ $compiled .= _simple_text($chunk)
Branch: 1 branch
BranchTrueFalseTotal
if $start > $pos 64 23 87
146 if $start > $pos;
147  
148 # Update scanning variables. The line counter is advanced for
149 # the chunk but not yet for the $starter, so that error reporting
150 # for unmatched $starter will point to the correct line
151 88 $pos = $start + length $starter;
152 88 $line_no += ($chunk =~ tr/\n//);
153  
154 # Grab code
155 88 my $stop = index $template, $stopper, $pos;
156 88 ▶ if ($stop < 0) { # no matching $stopper, bummer!
Branch: 1 branch
BranchTrueFalseTotal
if ($stop < 0) 0 87 87
157 1 my $section = _extract_section({template => $template}, $line_no);
158 1 croak "unclosed starter '$starter' at line $line_no\n$section";
159 }
160 88 my $code = substr $template, $pos, $stop - $pos;
161  
162 # Now I can advance the line count considering the $starter too
163 88 $line_no += ($starter =~ tr/\n//);
164  
165 88 ▶ if (length $code) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $code) 83 4 87
166 84 ▶ if (my $path = crumble($code)) {
Branch: 3 branches
BranchTrueFalseTotal
if (my $path = crumble($code)) { } 32 51 83
elsif (my($scalar) = $code =~ /\A\s* (\$ [[:alpha:]_]\w*) \s*\z/msx) { } 0 51 51
elsif (substr($code, 0, 1) eq '=') { } 24 27 51
167 33 $compiled .= _variable($path);
168 }
169 elsif (my ($scalar) =
170 $code =~ m{\A\s* (\$ [[:alpha:]_]\w*) \s*\z}mxs)
171 {
172 1 $compiled .=
173 "\nP($scalar); ### straight scalar\n\n";
174 } ## end elsif (my ($scalar) = $code...)
175 elsif (substr($code, 0, 1) eq q<=>) {
176 25 $compiled .= "\n# line $line_no 'template<3,$line_no>'\n"
177 . _expression(substr $code, 1);
178 }
179 else {
180 28 $compiled .=
181 "\n# line $line_no 'template<0,$line_no>'\n" . $code;
182 }
183 } ## end if (length $code)
184  
185 # Update scanning variables
186 88 $pos = $stop + length $stopper;
187 88 $line_no += (($code . $stopper) =~ tr/\n//);
188  
189 } ## end while ($pos < length $template)
190  
191 # put last part of input string as simple text
192 77 ▶ $compiled .= _simple_text(substr($template, $pos || 0));
Condition100%2/2
$pos || 0
l!l
6313
MC/DC100%1/1
$pos || 0
$pos
Truth table100%2/2
A
A= $pos
Aresult
11
00
193  
194 return {
195 77 template => $template,
196 code_text => $compiled,
197 };
198 } ## end sub _compile_code_text
199  
200 # The following function is long and complex because it deals with many
201 # different cases. It is kept as-is to avoid too many calls to other
202 # subroutines; for this reason, it's reasonably commented.
203 sub traverse { ## no critic (RequireArgUnpacking,ProhibitExcessComplexity)
204  
205 ## no critic (ProhibitDoubleSigils)
206 88 ▶ my $iref = ref($_[0]);
Subroutine traverse: called CC=49 SCAR=40.8
Pod documented
207 88 ▶ my $ref_wanted = ($iref eq 'SCALAR') || ($iref eq 'REF');
Condition100%3/3
$iref eq 'SCALAR' || $iref eq 'REF'
l!l&&r!l&&!r
1779
MC/DC100%2/2
$iref eq 'SCALAR' || $iref eq 'REF'
$iref eq 'SCALAR' $iref eq 'REF'
Truth table100%3/3
A || B
A= $iref eq 'SCALAR'B= $iref eq 'REF'
ABresult
1X1
011
000
208 88 ▶ my $ref_to_value = $ref_wanted ? shift : \shift;
Branch: 1 branch
BranchTrueFalseTotal
$ref_wanted ? : 8 79 87
209  
210 # early detection of options, remove them from args list
211 88 ▶ my $opts = (@_ && (ref($_[-1]) eq 'HASH')) ? pop(@_) : {};
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[-1] eq 'HASH' ? : 10 77 87
Condition100%3/3
@_ and ref $_[-1] eq "HASH"
!ll&&!rl&&r
27510
MC/DC100%2/2
@_ and ref $_[-1] eq "HASH"
@_ ref $_[-1] eq "HASH"
Truth table100%3/3
A and B
A= @_B= ref $_[-1] eq "HASH"
ABresult
0X0
100
111
212  
213 # if there's not $path provided, just don't bother going on. Actually,
214 # no $path means just return root, undefined path is always "not
215 # present" though.
216 88 ▶ return ($ref_wanted ? $ref_to_value : $$ref_to_value) unless @_;
Branch: 2 branches
BranchTrueFalseTotal
unless @_ 2 85 87
$ref_wanted ? : 1 1 2
217 86 my $path_input = shift;
218 86 ▶ return ($ref_wanted ? undef : '') unless defined $path_input;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $path_input 0 85 85
$ref_wanted ? : 0 0 0
219  
220 86 my $crumbs;
221 86 ▶ if (ref $path_input) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $path_input) { } 56 29 85
222 57 $crumbs = $path_input;
223 }
224 else {
225 30 ▶ return ($ref_wanted ? $ref_to_value : $$ref_to_value)
Branch: 2 branches
BranchTrueFalseTotal
if defined $path_input and not length $path_input 0 29 29
$ref_wanted ? : 0 0 0
Condition33%1/3
defined $path_input and not length $path_input
!ll&&!rl&&r
0290
MC/DC0%0/2
defined $path_input and not length $path_input
defined $path_input not length $path_input
Truth table33%1/3
A and not(B)
A= defined $path_inputB= not length $path_input
ABresult
0X0
100
111
226 if defined($path_input) && !length($path_input);
227 30 $crumbs = crumble($path_input);
228 }
229 86 ▶ return ($ref_wanted ? undef : '') unless defined $crumbs;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $crumbs 0 85 85
$ref_wanted ? : 0 0 0
230  
231 # go down the rabbit hole
232 86 ▶ my $use_method = $opts->{traverse_methods} || 0;
Condition100%2/2
$opts->{'traverse_methods'} || 0
l!l
1075
MC/DC100%1/1
$opts->{'traverse_methods'} || 0
$opts->{'traverse_methods'}
Truth table100%2/2
A
A= $opts->{'traverse_methods'}
Aresult
11
00
233 86 my ($strict_blessed, $method_pre) = (0, 0);
234 86 ▶ if ($use_method) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_method) 10 75 85
235 11 ▶ $strict_blessed = $opts->{strict_blessed} || 0;
Condition100%2/2
$opts->{'strict_blessed'} || 0
l!l
28
MC/DC100%1/1
$opts->{'strict_blessed'} || 0
$opts->{'strict_blessed'}
Truth table100%2/2
A
A= $opts->{'strict_blessed'}
Aresult
11
00
236 11 ▶ $method_pre = (! $strict_blessed && $opts->{method_over_key}) || 0;
Condition100%5/5
!$strict_blessed && $opts->{'method_over_key'} || 0
l!l
46
not $strict_blessed and $opts->{"method_over_key"}
!ll&&!rl&&r
244
MC/DC100%2/2
!$strict_blessed && $opts->{'method_over_key'} || 0
not $strict_blessed $opts->{"method_over_key"}
Truth table100%3/3
not(A) and B
A= not $strict_blessedB= $opts->{"method_over_key"}
ABresult
111
0X0
100
237 }
238 86 for my $crumb (@$crumbs) {
239  
240 # $key is what we will look into $$ref_to_value. We don't use
241 # $crumb directly as we might change $key in the loop, and we
242 # don't want to spoil $crumbs
243 170 my $key = $crumb;
244  
245 # $ref tells me how to look down into $$ref_to_value, i.e. as
246 # an ARRAY or a HASH... or object
247 170 my $ref = reftype $$ref_to_value;
248  
249 # if $ref is not true, we hit a wall. How we proceed depends on
250 # whether we were asked to auto-vivify or not.
251 170 ▶ if (!$ref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ref) 7 162 169
252 8 ▶ return (exists($opts->{missing}) ? $opts->{missing} : '')
Branch: 2 branches
BranchTrueFalseTotal
unless $ref_wanted 1 6 7
exists $opts->{'missing'} ? : 0 1 1
253 unless $ref_wanted; # don't bother going on
254  
255 # auto-vivification requested! $key will tell us how to
256 # proceed further, hopefully
257 7 $ref = ref $key;
258 } ## end if (!$ref)
259  
260 # if $key is a reference, it will tell us what's expected now
261 169 ▶ if (my $key_ref = ref $key) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $key_ref = ref $key) 9 159 168
262  
263 # if $key_ref is not the same as $ref there is a mismatch
264 # between what's available ($ref) and what' expected ($key_ref)
265 10 ▶ return($ref_wanted ? undef : '') if $key_ref ne $ref;
Branch: 2 branches
BranchTrueFalseTotal
if $key_ref ne $ref 1 8 9
$ref_wanted ? : 0 1 1
266  
267 # OK, data and expectations agree. Get the "real" key
268 9 ▶ if ($key_ref eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ($key_ref eq 'ARRAY') { } 0 8 8
elsif ($key_ref eq 'HASH') { } 8 0 8
269 1 $key = $crumb->[0]; # it's an array, key is (only) element
270 }
271 elsif ($key_ref eq 'HASH') {
272 9 ($key) = keys %$crumb; # hash... key is (only) key
273 }
274 } ## end if (my $key_ref = ref ...)
275  
276 # if $ref is still not true at this point, we're doing
277 # auto-vivification and we have a plain key. Some guessing
278 # will be needed! Plain non-negative integers resolve to ARRAY,
279 # otherwise we'll consider $key as a HASH key
280 168 ▶ $ref ||= ($key =~ m{\A (?: 0 | [1-9]\d*) \z}mxs) ? 'ARRAY' : 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
$key =~ /\A (?: 0 | [1-9]\d*) \z/msx ? : 2 3 5
Condition66%2/3
$ref ||= $key =~ /\A (?: 0 | [1-9]\d*) \z/msx ? "ARRAY" : "HASH"
l!l&&r!l&&!r
16250
MC/DC0%0/2
$ref ||= $key =~ /\A (?: 0 | [1-9]\d*) \z/msx ? "ARRAY" : "HASH"
$ref $key =~ /\A (?: 0 | [1-9]\d*) \z/msx ? "ARRAY" : "HASH"
Truth table66%2/3
A ||= B
A= $refB= $key =~ /\A (?: 0 | [1-9]\d*) \z/msx ? "ARRAY" : "HASH"
ABresult
1X1
011
000
281  
282 # time to actually do the next step
283 168 my $is_blessed = blessed $$ref_to_value;
284 168 ▶ my $method = $is_blessed && $$ref_to_value->can($key);
Condition100%3/3
$is_blessed && $$ref_to_value->can($key)
!ll&&!rl&&r
15755
MC/DC100%2/2
$is_blessed && $$ref_to_value->can($key)
$is_blessed $$ref_to_value->can($key)
Truth table100%3/3
A && B
A= $is_blessedB= $$ref_to_value->can($key)
ABresult
0X0
100
111
285 168 ▶ if ($is_blessed && $strict_blessed) {
Branch: 7 branches
BranchTrueFalseTotal
if ($is_blessed and $strict_blessed) { } 1 166 167
elsif ($method and $method_pre) { } 2 164 166
elsif ($ref eq 'HASH' and exists $$ref_to_value->{$key}) { } 134 30 164
elsif ($ref eq 'ARRAY' and exists $$ref_to_value->[$key]) { } 17 13 30
elsif ($method and $use_method) { } 1 12 13
elsif ($ref eq 'HASH') { } 10 2 12
elsif ($ref eq 'ARRAY') { } 2 0 2
Condition100%3/3
$is_blessed and $strict_blessed
!ll&&!rl&&r
15791
MC/DC100%2/2
$is_blessed and $strict_blessed
$is_blessed $strict_blessed
Truth table100%3/3
A and B
A= $is_blessedB= $strict_blessed
ABresult
0X0
100
111
286 2 ▶ return($ref_wanted ? undef : '') unless $method;
Branch: 2 branches
BranchTrueFalseTotal
unless $method 1 0 1
$ref_wanted ? : 0 1 1
287 0 $ref_to_value = \($$ref_to_value->$method());
288 }
289 ▶ elsif ($method && $method_pre) {
Condition100%3/3
$method and $method_pre
!ll&&!rl&&r
16132
MC/DC100%2/2
$method and $method_pre
$method $method_pre
Truth table100%3/3
A and B
A= $methodB= $method_pre
ABresult
0X0
100
111
290 2 $ref_to_value = \($$ref_to_value->$method());
291 }
292 ▶ elsif (($ref eq 'HASH') && exists($$ref_to_value->{$key})) {
Condition100%3/3
$ref eq "HASH" and exists $$ref_to_value->{$key}
!ll&&!rl&&r
1911134
MC/DC100%2/2
$ref eq "HASH" and exists $$ref_to_value->{$key}
$ref eq "HASH" exists $$ref_to_value->{$key}
Truth table100%3/3
A and B
A= $ref eq "HASH"B= exists $$ref_to_value->{$key}
ABresult
0X0
100
111
293 134 $ref_to_value = \($$ref_to_value->{$key});
294 }
295 ▶ elsif (($ref eq 'ARRAY') && exists($$ref_to_value->[$key])) {
Condition100%3/3
$ref eq "ARRAY" and exists $$ref_to_value->[$key]
!ll&&!rl&&r
11217
MC/DC100%2/2
$ref eq "ARRAY" and exists $$ref_to_value->[$key]
$ref eq "ARRAY" exists $$ref_to_value->[$key]
Truth table100%3/3
A and B
A= $ref eq "ARRAY"B= exists $$ref_to_value->[$key]
ABresult
0X0
100
111
296 17 $ref_to_value = \($$ref_to_value->[$key]);
297 }
298 ▶ elsif ($method && $use_method) {
Condition66%2/3
$method and $use_method
!ll&&!rl&&r
1201
MC/DC50%1/2
$method and $use_method
$method $use_method
Truth table66%2/3
A and B
A= $methodB= $use_method
ABresult
0X0
100
111
299 1 $ref_to_value = \($$ref_to_value->$method());
300 }
301 # autovivification goes here eventually
302 elsif ($ref eq 'HASH') {
303 10 $ref_to_value = \($$ref_to_value->{$key});
304 }
305 elsif ($ref eq 'ARRAY') {
306 2 $ref_to_value = \($$ref_to_value->[$key]);
307 }
308 else { # don't know what to do with other references!
309 0 ▶ return $ref_wanted ? undef : '';
Branch: 1 branch
BranchTrueFalseTotal
$ref_wanted ? : 0 0 0
310 }
311 } ## end for my $crumb (@$crumbs)
312  
313 # normalize output, substitute undef with '' unless $ref_wanted
314 return
315 82 ▶ $ref_wanted ? $ref_to_value
Branch: 2 branches
BranchTrueFalseTotal
$ref_wanted ? : 7 75 82
defined $$ref_to_value ? : 69 6 75
316 : defined($$ref_to_value) ? $$ref_to_value
317 : '';
318  
319 ## use critic
320 } ## end sub traverse
321  
322 0 ▶sub V { return '' }
Subroutine V: called CC=1 SCAR=6.9
Pod documented
323 0 ▶sub A { return }
Subroutine A: called CC=1 SCAR=6.9
Pod documented
324 0 ▶sub H { return }
Subroutine H: called CC=1 SCAR=6.9
Pod documented
325 0 ▶sub HK { return }
Subroutine HK: called CC=1 SCAR=6.9
Pod documented
326 0 ▶sub HV { return }
Subroutine HV: called CC=1 SCAR=6.9
Pod documented
327  
328 sub _compile_sub {
329 76 ▶ my ($self, $outcome) = @_;
Subroutine _compile_sub: called CC=12 SCAR=24.8
330  
331 76 my @warnings;
332 {
333 76 ▶ my $utf8 = $self->{utf8} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'utf8'} ? : 74 2 76
334 76 ▶ my $stdout = $self->{stdout} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'stdout'} ? : 67 9 76
335 76 ▶ local $SIG{__WARN__} = sub { push @warnings, @_ };
Subroutine __ANON__: called CC=1 SCAR=0.0
336 76 my @code;
337 76 push @code, <<'END_OF_CODE';
338 sub {
339 my %variables = %{$self->{variables}};
340 my $V = \%variables; # generic kid, as before by default
341  
342 {
343 my $vars = shift || {};
344 if (ref($vars) eq 'HASH') { # old case
345 %variables = (%variables, %$vars);
346 }
347 else {
348 $V = $vars;
349 %variables = (HASH => { %variables }, REF => $V);
350 }
351 }
352  
353 my $buffer = ''; # output variable
354 my $OFH;
355 END_OF_CODE
356  
357 76 my $handle = '$OFH';
358 76 ▶ if ($stdout) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stdout) { } 67 9 76
359 67 $handle = 'STDOUT';
360 67 push @code, <<'END_OF_CODE';
361 local *STDOUT;
362 open STDOUT, '>', \$buffer or croak "open(): $OS_ERROR";
363 $OFH = select(STDOUT);
364 END_OF_CODE
365 }
366 else {
367 9 push @code, <<'END_OF_CODE';
368 open $OFH, '>', \$buffer or croak "open(): $OS_ERROR";
369 END_OF_CODE
370 }
371  
372 76 ▶ push @code, "binmode $handle, ':encoding(utf8)';\n"
Branch: 1 branch
BranchTrueFalseTotal
if $utf8 74 2 76
373 if $utf8;
374 push @code, "binmode $handle, '$self->{binmode}';\n"
375 76 ▶ if defined $self->{binmode};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"binmode"} 1 75 76
376  
377 # add functions that can be seen only within the compiled code
378 76 push @code, $self->_compile_code_localsubs($handle);
379  
380 76 push @code, <<'END_OF_CODE';
381 { # double closure to free "my" variables
382 my ($buffer, $OFH); # hide external ones
383 END_OF_CODE
384  
385 # the real code! one additional scope indentation to ensure we
386 # can "my" variables again
387 push @code,
388 "{\n", # this enclusure allows using "my" again
389 $outcome->{code_text},
390 76 "}\n}\n\n";
391  
392 76 ▶ push @code, "select(\$OFH);\n" if $stdout;
Branch: 1 branch
BranchTrueFalseTotal
if $stdout 67 9 76
393 76 push @code, "close $handle;\n\n";
394  
395 76 ▶ if ($utf8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($utf8) 74 2 76
396 74 push @code, <<'END_OF_CODE';
397 require Encode;
398 $buffer = Encode::decode(utf8 => $buffer);
399  
400 END_OF_CODE
401 }
402  
403 76 push @code, "return \$buffer;\n}\n";
404  
405 76 my $code = join '', @code;
406 #print {*STDOUT} $code, "\n\n\n\n\n"; exit 0;
407 76 ▶ $outcome->{sub} = eval $code; ## no critic (ProhibitStringyEval)
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
408 76 ▶ return $outcome if $outcome->{sub};
Branch: 1 branch
BranchTrueFalseTotal
if $outcome->{"sub"} 74 2 76
409 }
410  
411 2 my $error = $EVAL_ERROR;
412 2 my ($offset, $starter, $line_no) =
413 $error =~ m{at[ ]'template<(\d+),(\d+)>'[ ]line[ ](\d+)}mxs;
414 2 $line_no -= $offset;
415 s{at[ ]'template<\d+,\d+>'[ ]line[ ](\d+)}
416 3 {'at line ' . ($1 - $offset)}egmxs
417 2 for @warnings, $error;
418 2 ▶ if ($line_no == $starter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line_no == $starter) 0 2 2
419 s{,[ ]near[ ]"[#][ ]line.*?\n\s+}{, near "}gmxs
420 0 for @warnings, $error;
421 }
422  
423 2 my $section = _extract_section($outcome, $line_no);
424 2 $error = join '', @warnings, $error, "\n", $section;
425  
426 2 croak $error;
427 } ## end sub _compile_sub
428  
429 sub _compile_code_localsubs {
430 76 ▶ my ($self, $handle) = @_;
Subroutine _compile_code_localsubs: called CC=2 SCAR=6.9
431 76 my @code;
432 76 push @code, <<'END_OF_CODE';
433  
434 no warnings 'redefine';
435  
436 END_OF_CODE
437  
438 # custom functions to be injected
439 76 ▶ if (defined(my $custom = $self->{functions})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $custom = $self->{"functions"})) 1 75 76
440 push @code, map {
441 1 " local *$_ = \$self->{functions}{$_};\n"
442 } keys %$custom;
443 }
444  
445 # input data structure traversing facility
446 76 push @code, <<'END_OF_CODE';
447  
448 local *V = sub {
449 my $path = scalar(@_) ? shift : [];
450 my $input = scalar(@_) ? shift : $V;
451 return traverse($input, $path, $self);
452 };
453 local *A = sub {
454 my $path = scalar(@_) ? shift : [];
455 my $input = scalar(@_) ? shift : $V;
456 return @{traverse($input, $path, $self) || []};
457 };
458 local *H = sub {
459 my $path = scalar(@_) ? shift : [];
460 my $input = scalar(@_) ? shift : $V;
461 return %{traverse($input, $path, $self) || {}};
462 };
463 local *HK = sub {
464 my $path = scalar(@_) ? shift : [];
465 my $input = scalar(@_) ? shift : $V;
466 return keys %{traverse($input, $path, $self) || {}};
467 };
468 local *HV = sub {
469 my $path = scalar(@_) ? shift : [];
470 my $input = scalar(@_) ? shift : $V;
471 return values %{traverse($input, $path, $self) || {}};
472 };
473  
474 END_OF_CODE
475  
476 # this comes separated because we need $handle
477 76 push @code, <<"END_OF_CODE";
478 local *P = sub { return print $handle \@_; };
479  
480 use warnings 'redefine';
481  
482 END_OF_CODE
483  
484 76 return @code;
485 }
486  
487 sub _extract_section {
488 2 ▶ my ($hash, $line_no) = @_;
Subroutine _extract_section: called CC=4 SCAR=13.9
489 2 $line_no--; # for proper comparison with 0-based array
490 2 my $start = $line_no - ERROR_CONTEXT;
491 2 my $end = $line_no + ERROR_CONTEXT;
492  
493 2 my @lines = split /\n/mxs, $hash->{template};
494 2 ▶ $start = 0 if $start < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $start < 0 1 1 2
495 2 ▶ $end = $#lines if $end > $#lines;
Branch: 1 branch
BranchTrueFalseTotal
if $end > $#lines 1 1 2
496 2 my $n_chars = length($end + 1);
497 return join '', map {
498 2 ▶ sprintf "%s%${n_chars}d| %s\n",
Branch: 1 branch
BranchTrueFalseTotal
$_ == $line_no ? : 2 9 11
499 (($_ == $line_no) ? '>>' : ' '), ($_ + 1), $lines[$_];
500 } $start .. $end;
501 } ## end sub _extract_section
502  
503 sub _simple_text {
504 140 ▶ my $text = shift;
Subroutine _simple_text: called CC=2 SCAR=6.9
505  
506 140 ▶ return "P('$text');\n\n" if $text !~ /[\n'\\]/mxs;
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ /[\n'\\]/msx 34 106 140
507  
508 106 $text =~ s/^/ /gmxs; # indent, trick taken from diff -u
509 106 return <<"END_OF_CHUNK";
510 ### Verbatim text
511 P(do {
512 my \$text = <<'END_OF_INDENTED_TEXT';
513 $text
514 END_OF_INDENTED_TEXT
515 \$text =~ s/^ //gms; # de-indent
516 substr \$text, -1, 1, ''; # get rid of added newline
517 \$text;
518 });
519  
520 END_OF_CHUNK
521 } ## end sub _simple_text
522  
523 sub crumble {
524 139 ▶ my ($input, $allow_partial) = @_;
Subroutine crumble: called CC=15 SCAR=27.1
Pod documented
525 139 ▶ return unless defined $input;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input 0 139 139
526  
527 139 $input =~ s{\A\s+|\s+\z}{}gmxs;
528 139 ▶ return [] unless length $input;
Branch: 1 branch
BranchTrueFalseTotal
unless length $input 1 138 139
529  
530 138 my $sq = qr{(?mxs: ' [^']* ' )}mxs;
531 138 my $dq = qr{(?mxs: " (?:[^\\"] | \\.)* " )}mxs;
532 138 my $ud = qr{(?mxs: \w+ )}mxs;
533 138 my $chunk = qr{(?mxs: $sq | $dq | $ud)+}mxs;
534  
535 # save and reset current pos() on $input
536 138 my $prepos = pos($input);
537 138 pos($input) = undef;
538  
539 138 my @path;
540 ## no critic (RegularExpressions::ProhibitCaptureWithoutTest)
541 138 ▶ push @path, $1 while $input =~ m{\G [.]? ($chunk) }cgmxs;
Branch: 1 branch
BranchTrueFalseTotal
$input =~ /\G [.]? ($chunk) /cgmsx and do { push @path, $1 } 177 138 315
542 ## use critic
543  
544 # save and restore pos() on $input
545 138 my $postpos = pos($input);
546 138 pos($input) = $prepos;
547  
548 138 ▶ return unless defined $postpos;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $postpos 27 111 138
549 111 ▶ return if ($postpos != length($input)) && ! ($allow_partial);
Branch: 1 branch
BranchTrueFalseTotal
if $postpos != length $input and not $allow_partial 26 85 111
Condition100%3/3
$postpos != length $input and not $allow_partial
!ll&&!rl&&r
83226
MC/DC100%2/2
$postpos != length $input and not $allow_partial
$postpos != length $input not $allow_partial
Truth table100%3/3
A and not(B)
A= $postpos != length $inputB= not $allow_partial
ABresult
0X0
100
111
550  
551 # cleanup @path components
552 85 for my $part (@path) {
553 151 my @subparts;
554 151 ▶ while ((pos($part) || 0) < length($part)) {
Branch: 1 branch
BranchTrueFalseTotal
if ((pos $part || 0) < length $part) 151 151 302
Condition100%2/2
pos $part || 0
l!l
151151
MC/DC100%1/1
pos $part || 0
pos $part
Truth table100%2/2
A
A= pos $part
Aresult
11
00
555 151 ▶ if ($part =~ m{\G ($sq) }cgmxs) {
Branch: 3 branches
BranchTrueFalseTotal
if ($part =~ /\G ($sq) /cgmsx) { } 21 130 151
elsif ($part =~ /\G ($dq) /cgmsx) { } 16 114 130
elsif ($part =~ /\G ($ud) /cgmsx) { } 114 0 114
556 21 push @subparts, substr $1, 1, length($1) - 2;
557 }
558 elsif ($part =~ m{\G ($dq) }cgmxs) {
559 16 my $subpart = substr $1, 1, length($1) - 2;
560 16 $subpart =~ s{\\(.)}{$1}gmxs;
561 16 push @subparts, $subpart;
562 }
563 elsif ($part =~ m{\G ($ud) }cgmxs) {
564 114 push @subparts, $1;
565 }
566 else { # shouldn't happen ever
567 0 return;
568 }
569 } ## end while ((pos($part) || 0) ...)
570 151 $part = join '', @subparts;
571 } ## end for my $part (@path)
572  
573 85 ▶ return (\@path, $postpos) if $allow_partial && wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if $allow_partial and wantarray 2 83 85
Condition66%2/3
$allow_partial and wantarray
!ll&&!rl&&r
8302
MC/DC50%1/2
$allow_partial and wantarray
$allow_partial wantarray
Truth table66%2/3
A and B
A= $allow_partialB= wantarray
ABresult
0X0
100
111
574 83 return \@path;
575 } ## end sub crumble
576  
577 sub _variable {
578 32 ▶ my $path = shift;
Subroutine _variable: called CC=1 SCAR=0.0
579 32 my $DQ = q<">; # double quotes
580 32 $path = join ', ', map { $DQ . quotemeta($_) . $DQ } @{$path};
581  
582 32 return <<"END_OF_CHUNK";
583 ### Variable from the stash (\$V)
584 P(V([$path]));
585  
586 END_OF_CHUNK
587 } ## end sub _variable
588  
589 sub _expression {
590 24 ▶ my $expression = shift;
Subroutine _expression: called CC=1 SCAR=0.0
591 24 return <<"END_OF_CHUNK";
592 # Expression to be evaluated and printed out
593 {
594 my \$value = do {{
595 $expression
596 }};
597 P(\$value) if defined \$value;
598 }
599  
600 END_OF_CHUNK
601  
602 } ## end sub _expression
603  
604 14 1;
↑ Summary Build »