blib/lib/Template/Perlish.pm

filter: Statementstmt 97.1% 302 / 311 Branchbran 79.8% 123 / 154 Conditioncond 89.8% 44 / 49 MC/DCmcdc 80.0% 24 / 30 Subroutinesub 100% 50 / 50 Podpod 100% 13 / 13 totaltotal 91.6% 556 / 607 SCAR 47.0
CC
103
Cov
91%
traverse
57.0
crumble
15.1
_compile_sub
12.0
CRAP 110.1
↑ 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.60'; }
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=48 SCAR=40.4
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 '' unless $ref_wanted; # don't bother going on
Branch: 1 branch
BranchTrueFalseTotal
unless $ref_wanted 1 6 7
253  
254 # auto-vivification requested! $key will tell us how to
255 # proceed further, hopefully
256 7 $ref = ref $key;
257 } ## end if (!$ref)
258  
259 # if $key is a reference, it will tell us what's expected now
260 169 ▶ if (my $key_ref = ref $key) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $key_ref = ref $key) 9 159 168
261  
262 # if $key_ref is not the same as $ref there is a mismatch
263 # between what's available ($ref) and what' expected ($key_ref)
264 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
265  
266 # OK, data and expectations agree. Get the "real" key
267 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
268 1 $key = $crumb->[0]; # it's an array, key is (only) element
269 }
270 elsif ($key_ref eq 'HASH') {
271 9 ($key) = keys %$crumb; # hash... key is (only) key
272 }
273 } ## end if (my $key_ref = ref ...)
274  
275 # if $ref is still not true at this point, we're doing
276 # auto-vivification and we have a plain key. Some guessing
277 # will be needed! Plain non-negative integers resolve to ARRAY,
278 # otherwise we'll consider $key as a HASH key
279 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
280  
281 # time to actually do the next step
282 168 my $is_blessed = blessed $$ref_to_value;
283 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
284 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
285 2 ▶ return($ref_wanted ? undef : '') unless $method;
Branch: 2 branches
BranchTrueFalseTotal
unless $method 1 0 1
$ref_wanted ? : 0 1 1
286 0 $ref_to_value = \($$ref_to_value->$method());
287 }
288 ▶ 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
289 2 $ref_to_value = \($$ref_to_value->$method());
290 }
291 ▶ 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
292 134 $ref_to_value = \($$ref_to_value->{$key});
293 }
294 ▶ 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
295 17 $ref_to_value = \($$ref_to_value->[$key]);
296 }
297 ▶ 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
298 1 $ref_to_value = \($$ref_to_value->$method());
299 }
300 # autovivification goes here eventually
301 elsif ($ref eq 'HASH') {
302 10 $ref_to_value = \($$ref_to_value->{$key});
303 }
304 elsif ($ref eq 'ARRAY') {
305 2 $ref_to_value = \($$ref_to_value->[$key]);
306 }
307 else { # don't know what to do with other references!
308 0 ▶ return $ref_wanted ? undef : '';
Branch: 1 branch
BranchTrueFalseTotal
$ref_wanted ? : 0 0 0
309 }
310 } ## end for my $crumb (@$crumbs)
311  
312 # normalize output, substitute undef with '' unless $ref_wanted
313 return
314 82 ▶ $ref_wanted ? $ref_to_value
Branch: 2 branches
BranchTrueFalseTotal
$ref_wanted ? : 7 75 82
defined $$ref_to_value ? : 69 6 75
315 : defined($$ref_to_value) ? $$ref_to_value
316 : '';
317  
318 ## use critic
319 } ## end sub traverse
320  
321 0 ▶sub V { return '' }
Subroutine V: called CC=1 SCAR=6.9
Pod documented
322 0 ▶sub A { return }
Subroutine A: called CC=1 SCAR=6.9
Pod documented
323 0 ▶sub H { return }
Subroutine H: called CC=1 SCAR=6.9
Pod documented
324 0 ▶sub HK { return }
Subroutine HK: called CC=1 SCAR=6.9
Pod documented
325 0 ▶sub HV { return }
Subroutine HV: called CC=1 SCAR=6.9
Pod documented
326  
327 sub _compile_sub {
328 76 ▶ my ($self, $outcome) = @_;
Subroutine _compile_sub: called CC=12 SCAR=24.8
329  
330 76 my @warnings;
331 {
332 76 ▶ my $utf8 = $self->{utf8} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'utf8'} ? : 74 2 76
333 76 ▶ my $stdout = $self->{stdout} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'stdout'} ? : 67 9 76
334 76 ▶ local $SIG{__WARN__} = sub { push @warnings, @_ };
Subroutine __ANON__: called CC=1 SCAR=0.0
335 76 my @code;
336 76 push @code, <<'END_OF_CODE';
337 sub {
338 my %variables = %{$self->{variables}};
339 my $V = \%variables; # generic kid, as before by default
340  
341 {
342 my $vars = shift || {};
343 if (ref($vars) eq 'HASH') { # old case
344 %variables = (%variables, %$vars);
345 }
346 else {
347 $V = $vars;
348 %variables = (HASH => { %variables }, REF => $V);
349 }
350 }
351  
352 my $buffer = ''; # output variable
353 my $OFH;
354 END_OF_CODE
355  
356 76 my $handle = '$OFH';
357 76 ▶ if ($stdout) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stdout) { } 67 9 76
358 67 $handle = 'STDOUT';
359 67 push @code, <<'END_OF_CODE';
360 local *STDOUT;
361 open STDOUT, '>', \$buffer or croak "open(): $OS_ERROR";
362 $OFH = select(STDOUT);
363 END_OF_CODE
364 }
365 else {
366 9 push @code, <<'END_OF_CODE';
367 open $OFH, '>', \$buffer or croak "open(): $OS_ERROR";
368 END_OF_CODE
369 }
370  
371 76 ▶ push @code, "binmode $handle, ':encoding(utf8)';\n"
Branch: 1 branch
BranchTrueFalseTotal
if $utf8 74 2 76
372 if $utf8;
373 push @code, "binmode $handle, '$self->{binmode}';\n"
374 76 ▶ if defined $self->{binmode};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"binmode"} 1 75 76
375  
376 # add functions that can be seen only within the compiled code
377 76 push @code, $self->_compile_code_localsubs($handle);
378  
379 76 push @code, <<'END_OF_CODE';
380 { # double closure to free "my" variables
381 my ($buffer, $OFH); # hide external ones
382 END_OF_CODE
383  
384 # the real code! one additional scope indentation to ensure we
385 # can "my" variables again
386 push @code,
387 "{\n", # this enclusure allows using "my" again
388 $outcome->{code_text},
389 76 "}\n}\n\n";
390  
391 76 ▶ push @code, "select(\$OFH);\n" if $stdout;
Branch: 1 branch
BranchTrueFalseTotal
if $stdout 67 9 76
392 76 push @code, "close $handle;\n\n";
393  
394 76 ▶ if ($utf8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($utf8) 74 2 76
395 74 push @code, <<'END_OF_CODE';
396 require Encode;
397 $buffer = Encode::decode(utf8 => $buffer);
398  
399 END_OF_CODE
400 }
401  
402 76 push @code, "return \$buffer;\n}\n";
403  
404 76 my $code = join '', @code;
405 #print {*STDOUT} $code, "\n\n\n\n\n"; exit 0;
406 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
407 76 ▶ return $outcome if $outcome->{sub};
Branch: 1 branch
BranchTrueFalseTotal
if $outcome->{"sub"} 74 2 76
408 }
409  
410 2 my $error = $EVAL_ERROR;
411 2 my ($offset, $starter, $line_no) =
412 $error =~ m{at[ ]'template<(\d+),(\d+)>'[ ]line[ ](\d+)}mxs;
413 2 $line_no -= $offset;
414 s{at[ ]'template<\d+,\d+>'[ ]line[ ](\d+)}
415 3 {'at line ' . ($1 - $offset)}egmxs
416 2 for @warnings, $error;
417 2 ▶ if ($line_no == $starter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line_no == $starter) 0 2 2
418 s{,[ ]near[ ]"[#][ ]line.*?\n\s+}{, near "}gmxs
419 0 for @warnings, $error;
420 }
421  
422 2 my $section = _extract_section($outcome, $line_no);
423 2 $error = join '', @warnings, $error, "\n", $section;
424  
425 2 croak $error;
426 } ## end sub _compile_sub
427  
428 sub _compile_code_localsubs {
429 76 ▶ my ($self, $handle) = @_;
Subroutine _compile_code_localsubs: called CC=2 SCAR=6.9
430 76 my @code;
431 76 push @code, <<'END_OF_CODE';
432  
433 no warnings 'redefine';
434  
435 END_OF_CODE
436  
437 # custom functions to be injected
438 76 ▶ if (defined(my $custom = $self->{functions})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $custom = $self->{"functions"})) 1 75 76
439 push @code, map {
440 1 " local *$_ = \$self->{functions}{$_};\n"
441 } keys %$custom;
442 }
443  
444 # input data structure traversing facility
445 76 push @code, <<'END_OF_CODE';
446  
447 local *V = sub {
448 my $path = scalar(@_) ? shift : [];
449 my $input = scalar(@_) ? shift : $V;
450 return traverse($input, $path, $self);
451 };
452 local *A = sub {
453 my $path = scalar(@_) ? shift : [];
454 my $input = scalar(@_) ? shift : $V;
455 return @{traverse($input, $path, $self) || []};
456 };
457 local *H = sub {
458 my $path = scalar(@_) ? shift : [];
459 my $input = scalar(@_) ? shift : $V;
460 return %{traverse($input, $path, $self) || {}};
461 };
462 local *HK = sub {
463 my $path = scalar(@_) ? shift : [];
464 my $input = scalar(@_) ? shift : $V;
465 return keys %{traverse($input, $path, $self) || {}};
466 };
467 local *HV = sub {
468 my $path = scalar(@_) ? shift : [];
469 my $input = scalar(@_) ? shift : $V;
470 return values %{traverse($input, $path, $self) || {}};
471 };
472  
473 END_OF_CODE
474  
475 # this comes separated because we need $handle
476 76 push @code, <<"END_OF_CODE";
477 local *P = sub { return print $handle \@_; };
478  
479 use warnings 'redefine';
480  
481 END_OF_CODE
482  
483 76 return @code;
484 }
485  
486 sub _extract_section {
487 2 ▶ my ($hash, $line_no) = @_;
Subroutine _extract_section: called CC=4 SCAR=13.9
488 2 $line_no--; # for proper comparison with 0-based array
489 2 my $start = $line_no - ERROR_CONTEXT;
490 2 my $end = $line_no + ERROR_CONTEXT;
491  
492 2 my @lines = split /\n/mxs, $hash->{template};
493 2 ▶ $start = 0 if $start < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $start < 0 1 1 2
494 2 ▶ $end = $#lines if $end > $#lines;
Branch: 1 branch
BranchTrueFalseTotal
if $end > $#lines 1 1 2
495 2 my $n_chars = length($end + 1);
496 return join '', map {
497 2 ▶ sprintf "%s%${n_chars}d| %s\n",
Branch: 1 branch
BranchTrueFalseTotal
$_ == $line_no ? : 2 9 11
498 (($_ == $line_no) ? '>>' : ' '), ($_ + 1), $lines[$_];
499 } $start .. $end;
500 } ## end sub _extract_section
501  
502 sub _simple_text {
503 140 ▶ my $text = shift;
Subroutine _simple_text: called CC=2 SCAR=6.9
504  
505 140 ▶ return "P('$text');\n\n" if $text !~ /[\n'\\]/mxs;
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ /[\n'\\]/msx 34 106 140
506  
507 106 $text =~ s/^/ /gmxs; # indent, trick taken from diff -u
508 106 return <<"END_OF_CHUNK";
509 ### Verbatim text
510 P(do {
511 my \$text = <<'END_OF_INDENTED_TEXT';
512 $text
513 END_OF_INDENTED_TEXT
514 \$text =~ s/^ //gms; # de-indent
515 substr \$text, -1, 1, ''; # get rid of added newline
516 \$text;
517 });
518  
519 END_OF_CHUNK
520 } ## end sub _simple_text
521  
522 sub crumble {
523 139 ▶ my ($input, $allow_partial) = @_;
Subroutine crumble: called CC=15 SCAR=27.1
Pod documented
524 139 ▶ return unless defined $input;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input 0 139 139
525  
526 139 $input =~ s{\A\s+|\s+\z}{}gmxs;
527 139 ▶ return [] unless length $input;
Branch: 1 branch
BranchTrueFalseTotal
unless length $input 1 138 139
528  
529 138 my $sq = qr{(?mxs: ' [^']* ' )}mxs;
530 138 my $dq = qr{(?mxs: " (?:[^\\"] | \\.)* " )}mxs;
531 138 my $ud = qr{(?mxs: \w+ )}mxs;
532 138 my $chunk = qr{(?mxs: $sq | $dq | $ud)+}mxs;
533  
534 # save and reset current pos() on $input
535 138 my $prepos = pos($input);
536 138 pos($input) = undef;
537  
538 138 my @path;
539 ## no critic (RegularExpressions::ProhibitCaptureWithoutTest)
540 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
541 ## use critic
542  
543 # save and restore pos() on $input
544 138 my $postpos = pos($input);
545 138 pos($input) = $prepos;
546  
547 138 ▶ return unless defined $postpos;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $postpos 27 111 138
548 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
549  
550 # cleanup @path components
551 85 for my $part (@path) {
552 151 my @subparts;
553 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
554 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
555 21 push @subparts, substr $1, 1, length($1) - 2;
556 }
557 elsif ($part =~ m{\G ($dq) }cgmxs) {
558 16 my $subpart = substr $1, 1, length($1) - 2;
559 16 $subpart =~ s{\\(.)}{$1}gmxs;
560 16 push @subparts, $subpart;
561 }
562 elsif ($part =~ m{\G ($ud) }cgmxs) {
563 114 push @subparts, $1;
564 }
565 else { # shouldn't happen ever
566 0 return;
567 }
568 } ## end while ((pos($part) || 0) ...)
569 151 $part = join '', @subparts;
570 } ## end for my $part (@path)
571  
572 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
573 83 return \@path;
574 } ## end sub crumble
575  
576 sub _variable {
577 32 ▶ my $path = shift;
Subroutine _variable: called CC=1 SCAR=0.0
578 32 my $DQ = q<">; # double quotes
579 32 $path = join ', ', map { $DQ . quotemeta($_) . $DQ } @{$path};
580  
581 32 return <<"END_OF_CHUNK";
582 ### Variable from the stash (\$V)
583 P(V([$path]));
584  
585 END_OF_CHUNK
586 } ## end sub _variable
587  
588 sub _expression {
589 24 ▶ my $expression = shift;
Subroutine _expression: called CC=1 SCAR=0.0
590 24 return <<"END_OF_CHUNK";
591 # Expression to be evaluated and printed out
592 {
593 my \$value = do {{
594 $expression
595 }};
596 P(\$value) if defined \$value;
597 }
598  
599 END_OF_CHUNK
600  
601 } ## end sub _expression
602  
603 14 1;
↑ Summary Build »