Positron/DataTemplate.pm

filter: Statementstmt 93.3% 280 / 300 Branchbran 87.9% 146 / 166 Conditioncond 65.2% 45 / 69 MC/DCmcdc 45.4% 20 / 44 Subroutinesub 100% 14 / 14 Podpod 0.0% 0 / 3 totaltotal 84.7% 505 / 596 CC 120 SCAR 38.0
CC
120
Cov
88%
_process_array
72.3
_process_hash
35.4
_process_text
24.2
CRAP 44.5
↑ Summary Positron/Expression.pm »
1 package Positron::DataTemplate;
2 14 our $VERSION = 'v0.1.3'; # VERSION
3  
4 =head1 NAME
5  
6 Positron::DataTemplate - templating plain data to plain data
7  
8 =head1 VERSION
9  
10 version v0.1.3
11  
12 =head1 SYNOPSIS
13  
14 my $engine = Positron::DataTemplate->new();
15 my $template = { contents => ['@list', '$title'] };
16 my $data = { list => [
17 { title => 'first title', url => '/first-title.html' },
18 { title => 'second title', url => '/second-title.html' },
19 ] };
20 my $result = $engine->process($template, $data);
21 # { contents => [ 'first title', 'second title' ] }
22  
23 =head1 DESCRIPTION
24  
25 C<Positron::DataTemplate> is a templating engine. Unlike most templating engines,
26 though, it does not work on text, but on raw data: the template is (typically)
27 a hash or array reference, and the result is one, too.
28  
29 This module rose from a script that regularly produced HTML snippets on disk,
30 using regular, text-based templates. Each use case used the same data, but a different
31 template. For one use case, however, the output was needed in
32 JSON format, not HTML. One solution would have been to use the text-based
33 templating system to produce a valid JSON document (quite risky). The other solution,
34 which was taken at that time, was to transform the input data into the desired
35 output structure in code, and use a JSON serializer on that, bypassing the template
36 output.
37  
38 The third solution would have been to provide a template that did not directly
39 produce the serialised JSON text, but described the data structure transformation
40 in an on-disc format. By working only with structured data, and never with text,
41 the serialized output must always be valid JSON.
42  
43 This (minus the serialization) is the domain of C<Positron::DataTemplate>.
44  
45 =head1 EXAMPLES
46  
47 This code is still being worked on. This includes the documentation. In the meanwhile,
48 please use the following examples (and some trial & error) to gain a first look.
49 Alternatively, if you have access to the tests of this distribution, these also
50 give some examples.
51  
52 =head2 Text replacement
53  
54 [ '$one', '{$two}', 'and {$three}' ] + { one => 1, two => 2, three => 3 }
55 -> [ '1', '2', 'and 3' ]
56  
57 =head2 Direct inclusion
58  
59 [ '&this', '&that' ] + { this => [1, 2], that => { 3 => 4 } }
60 -> [ [1, 2], { 3 => 4} ]
61  
62 =head2 Loops
63  
64 { titles => ['@list', '{$id}: {$title}'] }
65 + { list => [ { id => 1, title => 'one' }, { id => 2, title => 'two' } ] }
66 -> { titles => [ '1: one', '2: two' ] }
67  
68 =head2 Conditions
69  
70 { checked => ['?active', 'yes', 'no] } + { active => 1 }
71 -> { checked => 'yes' }
72  
73 =head2 Interpolation (works with a lot of constructs)
74  
75 [1, '&list', 4] + { list => [2, 3] }
76 -> [1, [2, 3], 4]
77 [1, '&-list', 4] + { list => [2, 3] }
78 -> [1, 2, 3, 4]
79 [1, '<', '&list', 4] + { list => [2, 3] }
80 -> [1, 2, 3, 4]
81  
82 { '< 1' => { a => 'b' }, '< 2' => { c => 'd', e => 'f' }
83 -> { a => 'b', c => 'd', e => 'f' }
84 { '< 1' => '&hash', two => 2 } + { hash => { one => 1 } }
85 -> { one => 1, two => 2 }
86  
87 =head2 Comments
88  
89 'this is {#not} a comment' -> 'this is a comment'
90 [1, '#comment', 2, 3] -> [1, 2, 3]
91 [1, '/comment', 2, 3] -> [1, 3]
92 [1, '//comment', 2, 3] -> [1]
93 { 1 => 2, '#3' => 4 } -> { 1 => 2, '' => 4 }
94 { 1 => 2, '/3' => 4 } -> { 1 => 2 }
95  
96 =head2 File inclusion (requires L<JSON> and L<File::Slurp>)
97  
98 [1, '. "/tmp/data.json"', 3] + '{ key: "value"}'
99 -> [1, { key => 'value' }, 3]
100  
101 =head2 File wrapping (also requires L<JSON> and L<File::Slurp>)
102  
103 [1, ': "/tmp/wrap.json"', { animal => 'dog' }, 3]
104 + '{ key: "value", contents: ":"}'
105 -> [1, { key => 'value', contents => { animal => 'dog' }, 3]
106  
107 =head2 Funtions on data
108  
109 [1, '^len', "abcde", 2] + { len => \&CORE::length }
110 -> [1, 5, 2]
111  
112 =head2 Assignment
113  
114 [1, '= title object.name', 'My {$title} and {$count}' ]
115 + { object => { name => 'Name', count => 10 } }
116 -> [1, 'My Name and']
117  
118 =head2 Escaping other constructs
119  
120 [ '~?cond', 'Talking about {{~}$templates}', '~.htaccess' ]
121 -> [ '?cond', 'Talking about {$templates}', '.htaccess' ]
122 =cut
123  
124 14 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
125 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
126 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
127  
128 14 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
129 14 ▶use Data::Dump qw(dump);
Subroutine BEGIN: called CC=1 SCAR=0.0
130 14 ▶use Positron::Environment;
Subroutine BEGIN: called CC=1 SCAR=0.0
131 14 ▶use Positron::Expression;
Subroutine BEGIN: called CC=1 SCAR=0.0
132  
133 13 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
134 # Note: no Moose; we have no inheritance or attributes to speak of.
135 13 my ($class) = @_;
136 13 my $self = {
137 include_paths => ['.'],
138 };
139 13 return bless($self, $class);
140 }
141  
142 130 ▶sub process {
Subroutine process: called CC=5 SCAR=17.3
Pod undocumented
143 130 my ($self, $template, $env) = @_;
144 # Returns (undef) in list context - is this correct?
145 130 ▶ return undef unless defined $template;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $template 2 128 130
146 128 $env = Positron::Environment->new($env);
147 128 my ($return, $interpolate) = $self->_process($template, $env);
148 # $return may be an interpolating construct,
149 # which depends on the context here.
150 127 ▶ if (wantarray and $interpolate and ref($return) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray and $interpolate and ref $return eq 'ARRAY') { } 0 127 127
Condition50%3/6
wantarray and $interpolate and ref $return eq "ARRAY"
!ll&&!rl&&r
12700
wantarray and $interpolate
!ll&&!rl&&r
41230
MC/DC0%0/3
wantarray and $interpolate and ref $return eq "ARRAY"
wantarray $interpolate ref $return eq "ARRAY"
Truth table50%2/4
A and B and C
A= wantarrayB= $interpolateC= ref $return eq "ARRAY"
ABCresult
0XX0
10X0
1100
1111
151 0 return @$return;
152 } else {
153 127 return $return;
154 }
155 }
156  
157 677 ▶sub _process {
Subroutine _process: called CC=4 SCAR=14.0
158 677 my ($self, $template, $env) = @_;
159 677 ▶ if (not ref($template)) {
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $template) { } 459 218 677
elsif (ref $template eq 'ARRAY') { } 141 77 218
elsif (ref $template eq 'HASH') { } 77 0 77
160 459 return $self->_process_text($template, $env);
161 } elsif (ref($template) eq 'ARRAY') {
162 141 return $self->_process_array($template, $env);
163 } elsif (ref($template) eq 'HASH') {
164 77 return $self->_process_hash($template, $env);
165 }
166 0 return $template; # TODO: deep copy?
167 }
168  
169 459 ▶sub _process_text {
Subroutine _process_text: called CC=24 SCAR=31.9
170 459 my ($self, $template, $env) = @_;
171 459 ▶ return ($template, 0) unless $template; # undef, '', 0, or '0'
Branch: 1 branch
BranchTrueFalseTotal
unless $template 10 449 459
172 449 my $interpolate = 0;
173 449 ▶ if ($template =~ m{ \A [&,] (-?) (.*) \z}xms) {
Branch: 7 branches
BranchTrueFalseTotal
if ($template =~ / \A [&,] (-?) (.*) \z/msx) { } 49 400 449
elsif ($template =~ / \A \$ (.*) \z/msx) { } 70 330 400
elsif ($template =~ / \A \x23 (\+?) /msx) { } 4 326 330
elsif ($template =~ / \A = \s* (\w+) \s+ (.*) /msx) { } 1 325 326
elsif ($template =~ / \A ([.:]) (-?) \s* ([^\s-].*) /msx) { } 9 316 325
elsif ($template =~ / \A \: (-?) \s* \z /msx) { } 12 304 316
elsif ($template =~ / \A \^ (-?) \s* (.*) /msx) { } 3 301 304
174 49 ▶ if ($1) { $interpolate = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($1) 6 43 49
175 49 my $expr = $2;
176 49 ▶ if ($expr eq ':') {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr eq ':') { } 3 46 49
177 # Special case: internal wrap evaluation
178 3 my ($return, $i) = $self->_process($env->get(':'), $env);
179 3 ▶ $interpolate ||= $i;
Condition33%1/3
$interpolate ||= $i
l!l&&r!l&&!r
003
MC/DC0%0/2
$interpolate ||= $i
$interpolate $i
Truth table33%1/3
A ||= B
A= $interpolateB= $i
ABresult
1X1
011
000
180 3 return ($return, $interpolate);
181 } else {
182 46 return (Positron::Expression::evaluate($expr, $env), $interpolate);
183 }
184 } elsif ($template =~ m{ \A \$ (.*) \z}xms) {
185 70 ▶ my $value = Positron::Expression::evaluate($1, $env) // '';
Condition100%2/2
Positron::Expression::evaluate($1, $env) // ""
l!l
682
MC/DC100%1/1
Positron::Expression::evaluate($1, $env) // ""
Positron::Expression::evaluate($1, $env)
Truth table100%2/2
A
A= Positron::Expression::evaluate($1, $env)
Aresult
11
00
186 70 return ("$value", 0);
187 } elsif ($template =~ m{ \A \x23 (\+?) }xms) {
188 4 ▶ return ('', ($1 ? 0 : 1));
Branch: 1 branch
BranchTrueFalseTotal
$1 ? : 1 3 4
189 } elsif ($template =~ m{ \A = \s* (\w+) \s+ (.*) }xms) {
190 # Always interpolates, the new identifier means nothing
191 1 Positron::Expression::evaluate($2, $env); # still perform it, means nothing
192 1 return ('', 1);
193 } elsif ($template =~ m{ \A ([.:]) (-?) \s* ([^\s-].*) }xms) {
194 9 my $filename_expr = $3;
195 9 ▶ if ($2) { $interpolate = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($2) 1 8 9
196 9 my $new_env = $env;
197 9 ▶ if ($1 eq ':') {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq ":") 2 7 9
198 # A wrap in text context, explicitly unset ':'.
199 2 $new_env = Positron::Environment->new({ ':' => undef }, { parent => $env });
200 }
201 9 my $filename = Positron::Expression::evaluate($filename_expr, $new_env);
202 9 require JSON;
203 9 require File::Slurp;
204 9 my $json = JSON->new();
205 9 my $file = undef;
206 9 foreach my $path (@{$self->{include_paths}}) {
207 18 ▶ if (-f $path . $filename) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $path . $filename) 9 9 18
208 9 $file = $path . $filename; # TODO: platform-independent chaining
209 }
210 }
211 9 ▶ if ($file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) { } 9 0 9
212 9 my $result = $json->decode(scalar(File::Slurp::read_file($file)));
213 9 my ($return, $i) = $self->_process($result, $new_env);
214 9 ▶ $interpolate ||= $i;
Condition66%2/3
$interpolate ||= $i
l!l&&r!l&&!r
108
MC/DC50%1/2
$interpolate ||= $i
$interpolate $i
Truth table66%2/3
A ||= B
A= $interpolateB= $i
ABresult
1X1
011
000
215 9 return ($return, $interpolate);
216 } else {
217 0 croak "Can't find template '$filename' in " . join(':', @{$self->{include_paths}});
218 }
219 } elsif ($template =~ m{ \A \: (-?) \s* \z }xms) {
220 # wrap evaluation
221 12 ▶ if ($1) { $interpolate = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($1) 3 9 12
222 12 my ($return, $i) = $self->_process($env->get(':'), $env);
223 12 ▶ $interpolate ||= $i;
Condition100%3/3
$interpolate ||= $i
l!l&&r!l&&!r
327
MC/DC100%2/2
$interpolate ||= $i
$interpolate $i
Truth table100%3/3
A ||= B
A= $interpolateB= $i
ABresult
1X1
011
000
224 12 return ($return, $interpolate);
225 } elsif ($template =~ m{ \A \^ (-?) \s* (.*) }xms) {
226 # Special non-list case, e.g. hash value (not key)
227 # cannot interpolate
228 3 my $function = Positron::Expression::evaluate($2, $env);
229 3 return (scalar($function->()), 0);
230 } else {
231 301 $template =~ s{
232 \{ \$ ([^\}]*) \}
233 }{
234 11 ▶ my $replacement = Positron::Expression::evaluate($1, $env) // '';
Condition50%1/2
Positron::Expression::evaluate($1, $env) // ""
l!l
110
MC/DC0%0/1
Positron::Expression::evaluate($1, $env) // ""
Positron::Expression::evaluate($1, $env)
Truth table50%1/2
A
A= Positron::Expression::evaluate($1, $env)
Aresult
11
00
235 11 "$replacement";
236 }xmseg;
237 301 $template =~ s{
238 (\s*) \{ \x23 (-?) ([^\}]*) \} (\s*)
239 }{
240 3 ▶ $2 ? '' : $1 . $4;
Branch: 1 branch
BranchTrueFalseTotal
$2 ? : 1 2 3
241 }xmseg;
242 # At the very end: get rid of escaping tildes (one layer)
243 301 $template =~ s{ \A ~ }{}xms;
244 301 $template =~ s{ \{ ~ \} }{}xmsg;
245 301 return ($template, 0);
246 }
247 }
248  
249 167 ▶sub _process_array {
Subroutine _process_array: called CC=56 SCAR=42.8
250 167 my ($self, $template, $env) = @_;
251 167 my $interpolate = 0;
252 167 ▶ return ([], 0) unless @$template;
Branch: 1 branch
BranchTrueFalseTotal
unless @$template 0 167 167
253 167 my @elements = @$template;
254 167 ▶ if ($elements[0] =~ m{ \A \@ (-?) (.*) \z}xms) {
Branch: 2 branches
BranchTrueFalseTotal
if ($elements[0] =~ / \A \@ (-?) (.*) \z/msx) { } 13 154 167
elsif ($elements[0] =~ / \A \? (-?) (.*) \z/msx) { } 21 133 154
255 # list iteration
256 13 ▶ if ($1) { $interpolate = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($1) 5 8 13
257 13 my $clause = $2;
258 13 shift @elements;
259 13 my $result = [];
260 13 my $list = Positron::Expression::evaluate($clause, $env);
261 13 ▶ if (not ref($list) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $list eq "ARRAY") 0 13 13
262 # If it's not a list, make it a one-element list.
263 # Useful for forcing interpolation via '[@- ""]' or aliasing (to be introduced)
264 0 $list = [$list];
265 }
266 13 foreach my $el (@$list) {
267 26 my $new_env = Positron::Environment->new( $el, { parent => $env } );
268 # evaluate rest of list as array,
269 26 my ($return, undef) = $self->_process_array(\@elements, $new_env);
270 # and flatten
271 26 push @$result, @$return;
272 }
273 13 return ($result, $interpolate);
274 } elsif ($elements[0] =~ m{ \A \? (-?) (.*) \z}xms) {
275 # conditional
276 21 ▶ if ($1) { $interpolate = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($1) 2 19 21
277 21 my $clause = $2;
278 21 shift @elements;
279 21 ▶ my $has_else = (@elements > 1) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
@elements > 1 ? : 19 2 21
280 21 my $cond = Positron::Expression::evaluate($clause, $env); # can be anything!
281 # for Positron, empty lists and hashes are false!
282 21 $cond = Positron::Expression::true($cond);
283 21 ▶ if (not $cond and not $has_else) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cond or $has_else) 1 20 21
Condition100%3/3
$cond or $has_else
l!l&&r!l&&!r
1551
MC/DC100%2/2
$cond or $has_else
$cond $has_else
Truth table100%3/3
A or B
A= $condB= $has_else
ABresult
1X1
011
000
284 # no else clause, return empty on false
285 # (please interpolate!)
286 1 return ('', 1);
287 }
288 20 my $then = shift @elements;
289 20 my $else = shift @elements;
290 20 ▶ my $result = $cond ? $then : $else;
Branch: 1 branch
BranchTrueFalseTotal
$cond ? : 15 5 20
291 20 my ($return, $i) = $self->_process($result, $env);
292 20 ▶ $interpolate ||= $i;
Condition66%2/3
$interpolate ||= $i
l!l&&r!l&&!r
2018
MC/DC50%1/2
$interpolate ||= $i
$interpolate $i
Truth table66%2/3
A ||= B
A= $interpolateB= $i
ABresult
1X1
011
000
293 20 return ($return, $interpolate);
294 } else {
295 133 my $return = [];
296 # potential structural comments
297 133 my $skip_next = 0;
298 133 my $capturing_function = 0;
299 133 my $capturing_wrap = 0;
300 133 my $capturing_wrap_interpolates = 0;
301 133 my $interpolate_next = 0; # actual count
302 133 my $is_first_element = 1;
303 133 foreach my $element (@elements) {
304 342 ▶ if ($element =~ m{ \A // (-?) }xms) {
Branch: 9 branches
BranchTrueFalseTotal
if ($element =~ m[ \A // (-?) ]msx) { } 4 338 342
elsif ($element =~ m[ \A / (-?) ]msx) { } 3 335 338
elsif ($skip_next) { } 3 332 335
elsif ($element =~ / \A \^ (-?) \s* ([^\s-].*) /msx) { } 6 326 332
elsif ($element =~ / \A \: (-?) \s* ([^\s-].*) /msx) { } 11 315 326
elsif ($element =~ / \A = (-?) \s* (\w+) \s+ (.*) /msx) { } 9 306 315
elsif ($capturing_function) { } 5 301 306
elsif ($capturing_wrap) { } 11 290 301
elsif ($element =~ / \A < /msx) { } 13 277 290
305 4 ▶ if ($is_first_element and $1) { $interpolate = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($is_first_element and $1) 1 3 4
Condition100%3/3
$is_first_element and $1
!ll&&!rl&&r
121
MC/DC100%2/2
$is_first_element and $1
$is_first_element $1
Truth table100%3/3
A and B
A= $is_first_elementB= $1
ABresult
0X0
100
111
306 4 last; # nothing more
307 } elsif ($element =~ m{ \A / (-?) }xms) {
308 3 ▶ if ($is_first_element and $1) { $interpolate = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($is_first_element and $1) 1 2 3
Condition100%3/3
$is_first_element and $1
!ll&&!rl&&r
111
MC/DC100%2/2
$is_first_element and $1
$is_first_element $1
Truth table100%3/3
A and B
A= $is_first_elementB= $1
ABresult
0X0
100
111
309 3 $skip_next = 1;
310 } elsif ($skip_next) {
311 3 $skip_next = 0;
312 } elsif ($element =~ m{ \A \^ (-?) \s* ([^\s-].*) }xms) {
313 6 ▶ if ($is_first_element and $1) { $interpolate = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($is_first_element and $1) 1 5 6
Condition100%3/3
$is_first_element and $1
!ll&&!rl&&r
141
MC/DC100%2/2
$is_first_element and $1
$is_first_element $1
Truth table100%3/3
A and B
A= $is_first_elementB= $1
ABresult
0X0
100
111
314 6 $capturing_function = Positron::Expression::evaluate($2, $env);
315 # do not push!
316 } elsif ($element =~ m{ \A \: (-?) \s* ([^\s-].*) }xms) {
317 11 ▶ $capturing_wrap_interpolates = $1 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$1 ? : 6 5 11
318 11 my $filename = Positron::Expression::evaluate($2, $env);
319 11 ▶ if (!$filename) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($filename) 0 11 11
320 0 warn "# no filename in expression '$element'?";
321 }
322 11 require JSON;
323 11 require File::Slurp;
324 11 my $json = JSON->new();
325 11 my $file = undef;
326 11 foreach my $path (@{$self->{include_paths}}) {
327 22 ▶ if (-f $path . $filename) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $path . $filename) 11 11 22
328 11 $file = $path . $filename; # TODO: platform-independent chaining
329 }
330 }
331 11 ▶ if ($file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) { } 11 0 11
332 11 my $contents = File::Slurp::read_file($file);
333 11 $capturing_wrap = $json->decode($contents);
334 } else {
335 0 croak "Can't find template '$filename' in " . join(':', @{$self->{include_paths}});
336 }
337 # do not push!
338 } elsif ($element =~ m{ \A = (-?) \s* (\w+) \s+ (.*) }xms) {
339 9 ▶ if ($is_first_element and $1) { $interpolate = 1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($is_first_element and $1) 3 6 9
Condition100%3/3
$is_first_element and $1
!ll&&!rl&&r
333
MC/DC100%2/2
$is_first_element and $1
$is_first_element $1
Truth table100%3/3
A and B
A= $is_first_elementB= $1
ABresult
0X0
100
111
340 9 my $new_key = $2;
341 9 my $new_value = Positron::Expression::evaluate($3, $env);
342 # We change env here!
343 9 $env = Positron::Environment->new({}, { parent => $env });
344 9 $env->set($new_key, $new_value); # Handles '_' on either side
345 } elsif ($capturing_function) {
346 # we have a capturing function waiting for input
347 5 my ($arg, $i) = $self->_process($element, $env);
348 # interpolate: could be ['@- ""', arg1, arg2]
349 5 ▶ if (ref($arg) eq 'ARRAY' and $i) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $arg eq 'ARRAY' and $i) { } 0 5 5
elsif (ref $arg eq 'HASH' and $i) { } 0 5 5
Condition33%1/3
ref $arg eq "ARRAY" and $i
!ll&&!rl&&r
500
MC/DC0%0/2
ref $arg eq "ARRAY" and $i
ref $arg eq "ARRAY" $i
Truth table33%1/3
A and B
A= ref $arg eq "ARRAY"B= $i
ABresult
0X0
100
111
350 0 push @$return, $capturing_function->(@$arg);
351 ▶ } elsif (ref($arg) eq 'HASH' and $i) {
Condition33%1/3
ref $arg eq "HASH" and $i
!ll&&!rl&&r
500
MC/DC0%0/2
ref $arg eq "HASH" and $i
ref $arg eq "HASH" $i
Truth table33%1/3
A and B
A= ref $arg eq "HASH"B= $i
ABresult
0X0
100
111
352 0 push @$return, $capturing_function->(%$arg);
353 } else {
354 5 push @$return, $capturing_function->($arg);
355 }
356 # no more waiting function
357 5 $capturing_function = 0;
358 } elsif ($capturing_wrap) {
359 # we have a capturing wrap file waiting for input
360 # Note: neither the wrap nor the element have been evaluated yet!
361 11 my $new_env = Positron::Environment->new({ ':' => $element }, { parent => $env });
362 11 my ($result, $i) = $self->_process($capturing_wrap, $new_env);
363 11 ▶ $i ||= $capturing_wrap_interpolates;
Condition66%2/3
$i ||= $capturing_wrap_interpolates
l!l&&r!l&&!r
065
MC/DC50%1/2
$i ||= $capturing_wrap_interpolates
$i $capturing_wrap_interpolates
Truth table66%2/3
A ||= B
A= $iB= $capturing_wrap_interpolates
ABresult
1X1
011
000
364 # interpolate: could be ['@- ""', arg1, arg2]
365 # or [1, ':- file', 'contents', 2]
366 11 ▶ if (ref($result) eq 'ARRAY' and $i) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $result eq 'ARRAY' and $i) { } 6 5 11
elsif (ref $result eq 'HASH' and $i) { } 0 5 5
Condition100%3/3
ref $result eq "ARRAY" and $i
!ll&&!rl&&r
236
MC/DC100%2/2
ref $result eq "ARRAY" and $i
ref $result eq "ARRAY" $i
Truth table100%3/3
A and B
A= ref $result eq "ARRAY"B= $i
ABresult
0X0
100
111
367 6 push @$return, @$result;
368 ▶ } elsif (ref($result) eq 'HASH' and $i) {
Condition66%2/3
ref $result eq "HASH" and $i
!ll&&!rl&&r
320
MC/DC0%0/2
ref $result eq "HASH" and $i
ref $result eq "HASH" $i
Truth table66%2/3
A and B
A= ref $result eq "HASH"B= $i
ABresult
0X0
100
111
369 0 push @$return, %$result;
370 } else {
371 5 push @$return, $result;
372 }
373 # no more waiting wrap
374 11 $capturing_wrap = 0;
375 } elsif ($element =~ m{ \A < }xms) {
376 13 $interpolate_next += 1; # actual count
377 } else {
378 277 my ($result, $interpolate_me) = $self->_process($element, $env);
379 277 my @results = ($result);
380 277 $interpolate_next += $interpolate_me;
381 277 ▶ while ($interpolate_next > 0 and @results) {
Branch: 1 branch
BranchTrueFalseTotal
if ($interpolate_next > 0 and @results) 38 274 312
Condition66%2/3
$interpolate_next > 0 and @results
!ll&&!rl&&r
274038
MC/DC50%1/2
$interpolate_next > 0 and @results
$interpolate_next > 0 @results
Truth table66%2/3
A and B
A= $interpolate_next > 0B= @results
ABresult
0X0
100
111
382 38 ▶ if (ref($results[0]) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $results[0] eq 'ARRAY') { } 32 6 38
elsif (($results[0] // '') eq '') { } 3 3 6
383 32 my $array = shift @results;
384 32 unshift @results, @$array;
385 ▶ } elsif (($results[0] // '') eq '') {
Condition50%1/2
$results[0] // ""
l!l
60
MC/DC0%0/1
$results[0] // ""
$results[0]
Truth table50%1/2
A
A= $results[0]
Aresult
11
00
386 # Note: the empty string, if it wants to interpolate, becomes the empty list
387 # i.e. just drop it.
388 3 shift @results;
389 } else {
390 3 last; # conditions can't match any more
391 }
392 35 $interpolate_next--;
393 }
394 277 $interpolate_next = 0;
395 277 push @$return, @results;
396 }
397 338 $is_first_element = 0; # not anymore
398 }
399 133 ▶ if ($capturing_function) {
Branch: 1 branch
BranchTrueFalseTotal
if ($capturing_function) 1 132 133
400 # Oh no, a function waiting for args?
401 1 push @$return, $capturing_function->();
402 }
403 133 ▶ if ($capturing_wrap) {
Branch: 1 branch
BranchTrueFalseTotal
if ($capturing_wrap) 0 133 133
404 # Oh no, a wrap waiting for args?
405 0 my $new_env = Positron::Environment->new({ ':' => undef }, { parent => $env });
406 0 my ($result, $i) = $self->_process($capturing_wrap, $new_env);
407 0 ▶ if (ref($result) eq 'ARRAY' and $i) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $result eq 'ARRAY' and $i) { } 0 0 0
elsif (ref $result eq 'HASH' and $i) { } 0 0 0
Condition0%0/3
ref $result eq "ARRAY" and $i
!ll&&!rl&&r
000
MC/DC0%0/2
ref $result eq "ARRAY" and $i
ref $result eq "ARRAY" $i
Truth table0%0/3
A and B
A= ref $result eq "ARRAY"B= $i
ABresult
0X0
100
111
408 0 push @$return, @$result;
409 ▶ } elsif (ref($result) eq 'HASH' and $i) {
Condition0%0/3
ref $result eq "HASH" and $i
!ll&&!rl&&r
000
MC/DC0%0/2
ref $result eq "HASH" and $i
ref $result eq "HASH" $i
Truth table0%0/3
A and B
A= ref $result eq "HASH"B= $i
ABresult
0X0
100
111
410 0 push @$return, %$result;
411 } else {
412 0 push @$return, $result;
413 }
414 }
415 133 return ($return, $interpolate);
416 }
417 }
418 77 ▶sub _process_hash {
Subroutine _process_hash: called CC=35 SCAR=35.7
419 77 my ($self, $template, $env) = @_;
420 77 ▶ return ({}, 0) unless %$template;
Branch: 1 branch
BranchTrueFalseTotal
unless %$template 1 76 77
421 76 my %result = ();
422 76 my $hash_construct = undef;
423 76 my $switch_construct = undef;
424 76 foreach my $key (keys %$template) {
425 119 ▶ if ($key =~ m{ \A \% (.*) \z }xms) {
Branch: 2 branches
BranchTrueFalseTotal
if ($key =~ / \A \% (.*) \z /msx) { } 2 117 119
elsif ($key =~ / \A \| (.*) \z /msx) { } 8 109 117
426 2 $hash_construct = [$key, $1]; last;
427 } elsif ($key =~ m{ \A \| (.*) \z }xms) {
428 # basically auto-interpolates
429 8 $switch_construct = [$key, $1]; last;
430 }
431 }
432 76 ▶ if ($hash_construct) {
Branch: 2 branches
BranchTrueFalseTotal
if ($hash_construct) { } 2 74 76
elsif ($switch_construct) { } 8 66 74
433 2 my $e_content = Positron::Expression::evaluate($hash_construct->[1], $env);
434 2 ▶ croak "Error: result of expression '".$hash_construct->[1]."' must be hash" unless ref($e_content) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $e_content eq "HASH" 0 2 2
435 2 ▶ while (my ($key, $value) = each %$e_content) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$e_content) 4 2 6
436 4 my $new_env = Positron::Environment->new( { key => $key, value => $value }, { parent => $env } );
437 4 my ($t_content, undef) = $self->_process( $template->{$hash_construct->[0]}, $new_env);
438 4 ▶ croak "Error: content of % construct must be hash" unless ref($t_content) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $t_content eq "HASH" 0 4 4
439 # copy into result (automatically interpolates)
440 4 foreach my $k (keys %$t_content) {
441 6 $result{$k} = $t_content->{$k};
442 }
443 }
444 } elsif ($switch_construct) {
445 8 my $e_content = Positron::Expression::evaluate($switch_construct->[1], $env); # The switch key
446 # escape the '|' by adding another one!
447 8 ▶ my $qe_content = ( defined $e_content and $e_content =~m{ \A \|}xms ) ? "|$e_content" : $e_content;
Branch: 1 branch
BranchTrueFalseTotal
defined $e_content && $e_content =~ / \A \|/msx ? : 0 8 8
Condition66%2/3
defined $e_content and $e_content =~ / \A \|/msx
!ll&&!rl&&r
440
MC/DC0%0/2
defined $e_content and $e_content =~ / \A \|/msx
defined $e_content $e_content =~ / \A \|/msx
Truth table66%2/3
A and B
A= defined $e_contentB= $e_content =~ / \A \|/msx
ABresult
0X0
100
111
448 8 ▶ if (defined $e_content and exists $template->{$switch_construct->[0]}->{$qe_content}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $e_content and exists $$template{$switch_construct->[0]}{$qe_content}) { } 4 4 8
elsif (exists $$template{$switch_construct->[0]}{'|'}) { } 3 1 4
Condition66%2/3
defined $e_content and exists $$template{$switch_construct->[0]}{$qe_content}
!ll&&!rl&&r
404
MC/DC50%1/2
defined $e_content and exists $$template{$switch_construct->[0]}{$qe_content}
defined $e_content exists $$template{$switch_construct->[0]}{$qe_content}
Truth table66%2/3
A and B
A= defined $e_contentB= exists $$template{$switch_construct->[0]}{$qe_content}
ABresult
0X0
100
111
449 # We have no interpolation of our own, just pass the below up.
450 4 return $self->_process($template->{$switch_construct->[0]}->{$qe_content}, $env);
451 } elsif (exists $template->{$switch_construct->[0]}->{'|'}) {
452 3 return $self->_process($template->{$switch_construct->[0]}->{'|'}, $env);
453 } else {
454 1 return ('', 1);
455 }
456 } else {
457 # simple copy
458 # '<': find first, and interpolate
459 # do by sorting keys alphabetically
460 my @keys = sort {
461 66 ▶ if($a =~ m{ \A < }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a =~ / \A < /msx) { } 3 47 50
462 3 ▶ if ($b =~ m{ \A < }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b =~ / \A < /msx) { } 0 3 3
463 0 return $a cmp $b;
464 } else {
465 3 return -1;
466 }
467 } else {
468 47 ▶ if ($b =~ m{ \A < }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b =~ / \A < /msx) { } 3 44 47
469 3 return 1;
470 } else {
471 44 return $a cmp $b;
472 }
473 }
474 } keys %$template;
475 66 foreach my $key (@keys) {
476 109 my $value = $template->{$key};
477 109 ▶ if ($key =~ m{ \A < }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ / \A < /msx) 6 103 109
478 # interpolate
479 6 my ($values, $interpolate) = $self->_process($value, $env);
480 6 %result = (%result, %$values);
481 6 next;
482 }
483 103 ▶ if ($key =~ m{ \A / }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ m[ \A / ]msx) 1 102 103
484 # structural comment
485 1 next;
486 }
487 102 ▶ if ($value =~ m{ \A / }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ m[ \A / ]msx) 1 101 102
488 # structural comment (forbidden on values)
489 1 croak "Cannot comment out a value";
490 }
491 101 ▶ if ($key =~ m{ \A \^ \s* (.*)}xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ / \A \^ \s* (.*)/msx) 1 100 101
492 # consuming function call (interpolates)
493 1 my $func = Positron::Expression::evaluate($1, $env);
494 1 my ($value_in, undef) = $self->_process($value, $env);
495 1 my $hash_out = $func->($value_in);
496 # interpolate
497 1 foreach my $k (keys %$hash_out) {
498 1 $result{$k} = $hash_out->{$k};
499 }
500 1 next;
501 }
502 100 ▶ if ($key =~ m{ \A : (-?) \s* (.+) }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ / \A : (-?) \s* (.+) /msx) 2 98 100
503 # consuming wrap (interpolates in any case)
504 2 my $capturing_wrap;
505 2 my $filename = Positron::Expression::evaluate($2, $env);
506 2 require JSON;
507 2 require File::Slurp;
508 2 my $json = JSON->new();
509 2 my $file = undef;
510 2 foreach my $path (@{$self->{include_paths}}) {
511 4 ▶ if (-f $path . $filename) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $path . $filename) 2 2 4
512 2 $file = $path . $filename; # TODO: platform-independent chaining
513 }
514 }
515 2 ▶ if ($file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) { } 2 0 2
516 2 my $contents = File::Slurp::read_file($file);
517 2 $capturing_wrap = $json->decode($contents);
518 } else {
519 0 croak "Can't find template '$filename' in " . join(':', @{$self->{include_paths}});
520 }
521 2 my $new_env = Positron::Environment->new({ ':' => $value }, { parent => $env });
522 2 my ($hash_out, undef) = $self->_process($capturing_wrap, $new_env);
523 # interpolate
524 2 foreach my $k (keys %$hash_out) {
525 6 $result{$k} = $hash_out->{$k};
526 }
527 2 next;
528 }
529 98 ▶ if ($key =~ m{ \A = (-?) \s* (\w+) \s+ (.*) }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ / \A = (-?) \s* (\w+) \s+ (.*) /msx) 1 97 98
530 # assignment (always interpolates)
531 1 my $new_key = $2;
532 1 my $new_value = Positron::Expression::evaluate($3, $env);
533 # We change env here!
534 1 my $new_env = Positron::Environment->new({}, { parent => $env });
535 1 $new_env->set($new_key, $new_value); # Handles '_' on either side
536 1 my ($hash_out, undef) = $self->_process($value, $new_env);
537 # interpolate
538 1 foreach my $k (keys %$hash_out) {
539 1 $result{$k} = $hash_out->{$k};
540 }
541 1 next;
542 }
543 97 ▶ if ($key =~ m{ \A \? \s* (.*)}xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ / \A \? \s* (.*)/msx) 2 95 97
544 # "conditional key", syntactic sugar that interpolates the hash
545 # Short for { '< 1' => ['?cond', { ... }, {}], ... }
546 2 my $cond = Positron::Expression::evaluate($1, $env);
547 2 ▶ if ($cond) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cond) { } 1 1 2
548 1 my ($hash_out, undef) = $self->_process($value, $env);
549 # interpolate
550 1 foreach my $k (keys %$hash_out) {
551 1 $result{$k} = $hash_out->{$k};
552 }
553 } else {
554 # nothing!
555 }
556 2 next;
557 }
558 95 ($key, undef) = $self->_process($key, $env);
559 95 ($value, undef) = $self->_process($value, $env);
560 95 $result{$key} = $value;
561 }
562 }
563 67 return (\%result, 0);
564 }
565  
566 3 ▶sub add_include_paths {
Subroutine add_include_paths: called CC=1 SCAR=0.0
Pod undocumented
567 3 my ($self, @paths) = @_;
568 3 push @{$self->{'include_paths'}}, @paths;
569 }
570  
571 14 1; # End of Positron::DataTemplate
572  
573 __END__
↑ Summary Positron/Expression.pm »