lib/HP/Handy.pm

filter: Statementstmt 91.8% 923 / 1005 Branchbran 76.8% 578 / 752 Conditioncond 69.6% 209 / 300 MC/DCmcdc 49.7% 89 / 179 Subroutinesub 96.0% 98 / 102 Podpod 100% 5 / 5 totaltotal 81.1% 1902 / 2343 CC 495 SCAR 40.7
CC
495
Cov
83%
_eval_expr
184.3
_eval_tokens
73.4
_loop_changed
56.0
CRAP 58.8
↑ Summary
1 package HP::Handy;
2 ######################################################################
3 #
4 # HP::Handy - A tiny Jinja2-compatible template engine for Perl 5.5.3+
5 #
6 # https://metacpan.org/dist/HP-Handy
7 #
8 # Copyright (c) 2026 INABA Hitoshi <ina@cpan.org>
9 ######################################################################
10 #
11 # Compatible : Perl 5.005_03 and later
12 # Platform : Windows and UNIX/Linux
13 #
14 ######################################################################
15  
16 13 ▶use 5.00503; # Universal Consensus 1998 for primetools
Subroutine BEGIN: called CC=1 SCAR=0.0
17 # Perl 5.005_03 compatibility for historical toolchains
18 # use 5.008001; # Lancaster Consensus 2013 for toolchains
19  
20 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 0 ▶BEGIN { if ($] < 5.006 && !defined(&warnings::import)) { $INC{'warnings.pm'} = 'stub'; eval 'package warnings; sub import {}' } }
Subroutine BEGIN: called CC=3 SCAR=16.5
Branch: 1 branch
BranchTrueFalseTotal
if ($] < "5.006" and not defined &warnings::import) 0 13 13
Condition33%1/3
$] < "5.006" and not defined &warnings::import
!ll&&!rl&&r
1300
MC/DC0%0/2
$] < "5.006" and not defined &warnings::import
$] < "5.006" not defined &warnings::import
Truth table33%1/3
A and not(B)
A= $] < "5.006"B= not defined &warnings::import
ABresult
0X0
100
111
22 13 ▶use warnings; local $^W = 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 13 ▶BEGIN { pop @INC if $INC[-1] eq '.' }
Subroutine BEGIN: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
if $INC[-1] eq "." 0 13 13
24  
25 13 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 13 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 13 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 13 $VERSION = '0.02';
29 13 $VERSION = $VERSION;
30 # $VERSION self-assignment suppresses "used only once" warning under strict.
31  
32 ###############################################################################
33 # Built-in filters
34 ###############################################################################
35 my %FILTERS = (
36 10 ▶ 'upper' => sub { my $s = defined $_[0] ? $_[0] : ''; $s =~ tr/a-z/A-Z/; $s },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 9 1 10
37 2 ▶ 'lower' => sub { my $s = defined $_[0] ? $_[0] : ''; $s =~ tr/A-Z/a-z/; $s },
Subroutine __ANON__: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 2 0 2
38 7 ▶ 'trim' => sub { my $s = defined $_[0] ? $_[0] : ''; $s =~ s/^\s+|\s+$//g; $s },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 6 1 7
39 7 ▶ 'length' => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
40 7 ▶ return 0 unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 1 6 7
41 3 ▶ ref($_[0]) eq 'ARRAY' ? scalar @{$_[0]}
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 1 2 3
42 6 ▶ : ref($_[0]) eq 'HASH' ? scalar keys %{$_[0]}
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 3 3 6
43 : length($_[0])
44 },
45 5 ▶ 'reverse' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
46 5 ▶ return '' unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 5 5
47 5 ▶ ref($_[0]) eq 'ARRAY' ? [ reverse @{$_[0]} ]
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 3 2 5
48 : scalar reverse("$_[0]")
49 },
50 'escape' => \&_html_escape,
51 'e' => \&_html_escape,
52 21 ▶ 'safe' => sub { bless \(my $s = defined $_[0] ? $_[0] : ''), 'HP::Handy::SafeString' },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 21 0 21
53 6 ▶ 'default' => sub { (defined $_[0] && $_[0] ne '') ? $_[0] : $_[1] },
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] && $_[0] ne '' ? : 1 5 6
Condition100%3/3
defined $_[0] and $_[0] ne ""
!ll&&!rl&&r
411
MC/DC100%2/2
defined $_[0] and $_[0] ne ""
defined $_[0] $_[0] ne ""
Truth table100%3/3
A and B
A= defined $_[0]B= $_[0] ne ""
ABresult
0X0
100
111
54 1 ▶ 'd' => sub { (defined $_[0] && $_[0] ne '') ? $_[0] : $_[1] },
Subroutine __ANON__: called CC=3 SCAR=14.2
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] && $_[0] ne '' ? : 0 1 1
Condition33%1/3
defined $_[0] and $_[0] ne ""
!ll&&!rl&&r
100
MC/DC0%0/2
defined $_[0] and $_[0] ne ""
defined $_[0] $_[0] ne ""
Truth table33%1/3
A and B
A= defined $_[0]B= $_[0] ne ""
ABresult
0X0
100
111
55 5 ▶ 'replace' => sub {
Subroutine __ANON__: called CC=4 SCAR=14.5
56 5 my ($s, $from, $to) = @_;
57 5 ▶ $s = defined $s ? $s : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $s ? : 5 0 5
58 5 ▶ $from = defined $from ? $from : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $from ? : 5 0 5
59 5 ▶ $to = defined $to ? $to : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $to ? : 5 0 5
60 5 $s =~ s/\Q$from\E/$to/g;
61 5 $s
62 },
63 5 ▶ 'truncate' => sub {
Subroutine __ANON__: called CC=5 SCAR=16.3
64 5 my ($s, $len, $end) = @_;
65 5 ▶ $s = defined $s ? $s : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $s ? : 5 0 5
66 5 ▶ $len = defined $len ? int($len) : 255;
Branch: 1 branch
BranchTrueFalseTotal
defined $len ? : 5 0 5
67 5 ▶ $end = defined $end ? $end : '...';
Branch: 1 branch
BranchTrueFalseTotal
defined $end ? : 1 4 5
68 5 ▶ (length($s) > $len) ? substr($s, 0, $len) . $end : $s
Branch: 1 branch
BranchTrueFalseTotal
length $s > $len ? : 3 2 5
69 },
70 47 ▶ 'join' => sub {
Subroutine __ANON__: called CC=4 SCAR=16.9
71 47 my ($list, $sep) = @_;
72 47 ▶ $sep = defined $sep ? $sep : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $sep ? : 47 0 47
73 47 ▶ ref($list) eq 'ARRAY' ? join($sep, @$list) : (defined $list ? $list : '')
Branch: 2 branches
BranchTrueFalseTotal
ref $list eq 'ARRAY' ? : 47 0 47
defined $list ? : 0 0 0
74 },
75 7 ▶ 'first' => sub { ref($_[0]) eq 'ARRAY' && @{$_[0]} ? $_[0][0] : undef },
Subroutine __ANON__: called CC=3 SCAR=13.1
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' && @{$_[0];} ? : 7 0 7
Condition33%1/3
ref $_[0] eq "ARRAY" and @{$_[0];}
!ll&&!rl&&r
007
MC/DC0%0/2
ref $_[0] eq "ARRAY" and @{$_[0];}
ref $_[0] eq "ARRAY" @{$_[0];}
Truth table33%1/3
A and B
A= ref $_[0] eq "ARRAY"B= @{$_[0];}
ABresult
0X0
100
111
76 4 ▶ 'last' => sub { ref($_[0]) eq 'ARRAY' && @{$_[0]} ? $_[0][-1] : undef },
Subroutine __ANON__: called CC=3 SCAR=13.1
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' && @{$_[0];} ? : 4 0 4
Condition33%1/3
ref $_[0] eq "ARRAY" and @{$_[0];}
!ll&&!rl&&r
004
MC/DC0%0/2
ref $_[0] eq "ARRAY" and @{$_[0];}
ref $_[0] eq "ARRAY" @{$_[0];}
Truth table33%1/3
A and B
A= ref $_[0] eq "ARRAY"B= @{$_[0];}
ABresult
0X0
100
111
77 3 ▶ 'list' => sub { ref($_[0]) eq 'ARRAY' ? $_[0] : [defined $_[0] ? $_[0] : ()] },
Subroutine __ANON__: called CC=3 SCAR=11.2
Branch: 2 branches
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 2 1 3
defined $_[0] ? : 1 0 1
78 3 ▶ 'abs' => sub { abs(defined $_[0] ? $_[0] : 0) },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 3 0 3
79 3 ▶ 'int' => sub { int(defined $_[0] ? $_[0] : 0) },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 3 0 3
80 1 ▶ 'float' => sub { defined $_[0] ? ($_[0] + 0.0) : 0.0 },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 1 0 1
81 1 ▶ 'string' => sub { defined $_[0] ? "$_[0]" : '' },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 1 0 1
82 2 ▶ 'title' => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
83 2 ▶ my $s = defined $_[0] ? $_[0] : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 2 0 2
84 2 $s =~ s/\b([a-z])/uc($1)/ge;
85 2 $s
86 },
87 2 ▶ 'capitalize' => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
88 2 ▶ my $s = defined $_[0] ? lc($_[0]) : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 2 0 2
89 2 $s =~ s/^([a-z])/uc($1)/e;
90 2 $s
91 },
92 4 ▶ 'urlencode' => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
93 4 ▶ my $s = defined $_[0] ? $_[0] : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 4 0 4
94 4 $s =~ s/([^A-Za-z0-9\-_.~])/sprintf('%%%02X', ord($1))/ge;
95 4 $s
96 },
97 4 ▶ 'wordcount' => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
98 4 ▶ my $s = defined $_[0] ? $_[0] : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 4 0 4
99 4 my @w = split /\s+/, $s;
100 4 @w = grep { $_ ne '' } @w;
101 scalar @w
102 4 },
103 5 ▶ 'batch' => sub {
Subroutine __ANON__: called CC=7 SCAR=19.6
104 5 my ($list, $size, $fill) = @_;
105 5 ▶ return [] unless ref($list) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $list eq "ARRAY" 0 5 5
106 5 ▶ $size = int($size || 1);
Condition50%1/2
$size || 1
l!l
50
MC/DC0%0/1
$size || 1
$size
Truth table50%1/2
A
A= $size
Aresult
11
00
107 5 ▶ $size = 1 if $size < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $size < 1 0 5 5
108 5 my @result;
109 5 my @flat = @$list;
110 5 ▶ while (@flat) {
Branch: 1 branch
BranchTrueFalseTotal
if (@flat) 11 5 16
111 11 my @chunk = splice(@flat, 0, $size);
112 11 ▶ if (defined $fill) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fill) 2 9 11
113 2 ▶ push @chunk, $fill while @chunk < $size;
Branch: 1 branch
BranchTrueFalseTotal
@chunk < $size and do { push @chunk, $fill } 1 2 3
114 }
115 11 push @result, \@chunk;
116 }
117 \@result
118 5 },
119 4 ▶ 'slice' => sub {
Subroutine __ANON__: called CC=6 SCAR=18.0
120 4 my ($list, $slices) = @_;
121 4 ▶ return [] unless ref($list) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $list eq "ARRAY" 0 4 4
122 4 ▶ $slices = int($slices || 1);
Condition50%1/2
$slices || 1
l!l
40
MC/DC0%0/1
$slices || 1
$slices
Truth table50%1/2
A
A= $slices
Aresult
11
00
123 4 ▶ $slices = 1 if $slices < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $slices < 1 0 4 4
124 4 my $total = scalar @$list;
125 4 my $per = int($total / $slices);
126 4 my $extra = $total % $slices;
127 4 my @result;
128 4 my $idx = 0;
129 4 for my $i (0 .. $slices - 1) {
130 9 ▶ my $n = $per + ($i < $extra ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$i < $extra ? : 1 8 9
131 9 push @result, [ @{$list}[$idx .. $idx + $n - 1] ];
132 9 $idx += $n;
133 }
134 \@result
135 4 },
136 8 ▶ 'sort' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
137 8 my ($list, $attr) = @_;
138 8 ▶ return [] unless ref($list) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $list eq "ARRAY" 0 8 8
139 8 ▶ if (defined $attr) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $attr) 1 7 8
140 1 return [ sort { _get_attr($a, $attr) cmp _get_attr($b, $attr) } @$list ];
141 }
142 7 [ sort @$list ]
143 },
144 3 ▶ 'unique' => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
145 3 ▶ return [] unless ref($_[0]) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "ARRAY" 0 3 3
146 3 my %seen;
147 3 [ grep { !$seen{$_}++ } @{$_[0]} ]
148 },
149 2 ▶ 'min' => sub {
Subroutine __ANON__: called CC=5 SCAR=16.6
150 2 ▶ return undef unless ref($_[0]) eq 'ARRAY' && @{$_[0]};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "ARRAY" and @{$_[0];} 0 2 2
Condition33%1/3
ref $_[0] eq "ARRAY" and @{$_[0];}
!ll&&!rl&&r
002
MC/DC0%0/2
ref $_[0] eq "ARRAY" and @{$_[0];}
ref $_[0] eq "ARRAY" @{$_[0];}
Truth table33%1/3
A and B
A= ref $_[0] eq "ARRAY"B= @{$_[0];}
ABresult
0X0
100
111
151 2 my $m = $_[0][0];
152 2 ▶ for (@{$_[0]}) { $m = $_ if $_ < $m }
Branch: 1 branch
BranchTrueFalseTotal
if $_ < $m 1 5 6
153 $m
154 2 },
155 2 ▶ 'max' => sub {
Subroutine __ANON__: called CC=5 SCAR=16.6
156 2 ▶ return undef unless ref($_[0]) eq 'ARRAY' && @{$_[0]};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "ARRAY" and @{$_[0];} 0 2 2
Condition33%1/3
ref $_[0] eq "ARRAY" and @{$_[0];}
!ll&&!rl&&r
002
MC/DC0%0/2
ref $_[0] eq "ARRAY" and @{$_[0];}
ref $_[0] eq "ARRAY" @{$_[0];}
Truth table33%1/3
A and B
A= ref $_[0] eq "ARRAY"B= @{$_[0];}
ABresult
0X0
100
111
157 2 my $m = $_[0][0];
158 2 ▶ for (@{$_[0]}) { $m = $_ if $_ > $m }
Branch: 1 branch
BranchTrueFalseTotal
if $_ > $m 2 4 6
159 $m
160 2 },
161 3 ▶ 'sum' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
162 3 ▶ return 0 unless ref($_[0]) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "ARRAY" 0 3 3
163 3 my $s = 0; $s += $_ for @{$_[0]}; $s
164 },
165 6 ▶ 'map' => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
166 6 my ($list, $attr) = @_;
167 6 ▶ return [] unless ref($list) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $list eq "ARRAY" 0 6 6
168 6 [ map { _get_attr($_, $attr) } @$list ]
169 },
170 2 ▶ 'select' => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
171 2 my ($list, $attr) = @_;
172 2 ▶ return [] unless ref($list) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $list eq "ARRAY" 0 2 2
173 2 [ grep { _get_attr($_, $attr) } @$list ]
174 },
175 1 ▶ 'reject' => sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
176 1 my ($list, $attr) = @_;
177 1 ▶ return [] unless ref($list) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $list eq "ARRAY" 0 1 1
178 1 [ grep { !_get_attr($_, $attr) } @$list ]
179 },
180 4 ▶ 'count' => sub { ref($_[0]) eq 'ARRAY' ? scalar @{$_[0]} : 0 },
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 4 0 4
181 0 ▶ 'pprint' => sub { _to_json($_[0]) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
182 3 ▶ 'nl2br' => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
183 3 ▶ my $s = defined $_[0] ? $_[0] : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 3 0 3
184 3 $s =~ s/\n/<br>\n/g;
185 3 $s
186 },
187 4 ▶ 'striptags' => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
188 4 ▶ my $s = defined $_[0] ? $_[0] : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 4 0 4
189 4 $s =~ s/<[^>]*>//g;
190 4 $s
191 },
192 11 ▶ 'format' => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
193 11 my ($s, $fmt) = @_;
194 11 ▶ defined $s ? sprintf($fmt, $s) : ''
Branch: 1 branch
BranchTrueFalseTotal
defined $s ? : 11 0 11
195 },
196 3 ▶ 'center' => sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
197 3 my ($s, $width) = @_;
198 3 ▶ $s = defined $s ? $s : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $s ? : 3 0 3
199 3 ▶ $width = defined $width ? int($width) : 80;
Branch: 1 branch
BranchTrueFalseTotal
defined $width ? : 3 0 3
200 3 my $pad = $width - length($s);
201 3 ▶ return $s if $pad <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $pad <= 0 1 2 3
202 2 my $left = int($pad / 2);
203 2 my $right = $pad - $left;
204 2 (' ' x $left) . $s . (' ' x $right)
205 },
206 2 ▶ 'indent' => sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
207 2 my ($s, $width, $first) = @_;
208 2 ▶ $s = defined $s ? $s : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $s ? : 2 0 2
209 2 ▶ $width = defined $width ? int($width) : 4;
Branch: 1 branch
BranchTrueFalseTotal
defined $width ? : 2 0 2
210 2 ▶ $first = defined $first ? $first : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $first ? : 1 1 2
211 2 my $pad = ' ' x $width;
212 2 my @lines = split /\n/, $s, -1;
213 2 for my $i (0 .. $#lines) {
214 5 ▶ $lines[$i] = $pad . $lines[$i] if $i > 0 || $first;
Branch: 1 branch
BranchTrueFalseTotal
if $i > 0 or $first 4 1 5
Condition100%3/3
$i > 0 or $first
l!l&&r!l&&!r
311
MC/DC100%2/2
$i > 0 or $first
$i > 0 $first
Truth table100%3/3
A or B
A= $i > 0B= $first
ABresult
1X1
011
000
215 }
216 2 join("\n", @lines)
217 },
218 3 ▶ 'xmlattr' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
219 3 my $h = $_[0];
220 3 ▶ return '' unless ref($h) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $h eq "HASH" 0 3 3
221 join(' ', map {
222 3 ▶ my $v = defined $h->{$_} ? $h->{$_} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $h->{$_} ? : 3 0 3
223 3 $v =~ s/&/&amp;/g; $v =~ s/"/&quot;/g;
224 3 "$_=\"$v\""
225 } sort keys %$h)
226 },
227 7 ▶ 'tojson' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
228 7 my $v = $_[0];
229 7 _to_json($v)
230 },
231 13 'forceescape' => \&_html_escape,
232 );
233  
234 ###############################################################################
235 # Built-in tests (is_xxx)
236 ###############################################################################
237 my %TESTS = (
238 12 ▶ 'defined' => sub { defined $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
239 7 ▶ 'none' => sub { !defined $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
240 4 ▶ 'string' => sub { defined $_[0] && !ref($_[0]) },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and not ref $_[0] 4 0 4
241 11 ▶ 'number' => sub { defined $_[0] && !ref($_[0]) && $_[0] =~ /^-?(?:\d+\.?\d*|\.\d+)$/ },
Subroutine __ANON__: called CC=3 SCAR=14.2
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and not ref $_[0] and $_[0] =~ /^-?(?:\d+\.?\d*|\.\d+)$/ 11 0 11
Condition33%1/3
defined $_[0] and not ref $_[0]
!ll&&!rl&&r
0011
MC/DC0%0/2
defined $_[0] and not ref $_[0]
defined $_[0] not ref $_[0]
Truth table33%1/3
A and not(B)
A= defined $_[0]B= not ref $_[0]
ABresult
0X0
100
111
242 4 ▶ 'sequence' => sub { ref($_[0]) eq 'ARRAY' },
Subroutine __ANON__: called CC=1 SCAR=0.0
243 2 ▶ 'mapping' => sub { ref($_[0]) eq 'HASH' },
Subroutine __ANON__: called CC=1 SCAR=0.0
244 3 ▶ 'iterable' => sub { ref($_[0]) eq 'ARRAY' || ref($_[0]) eq 'HASH' },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq "ARRAY" or ref $_[0] eq "HASH" 1 2 3
245 3 ▶ 'callable' => sub { ref($_[0]) eq 'CODE' },
Subroutine __ANON__: called CC=1 SCAR=0.0
246 22 ▶ 'odd' => sub { defined $_[0] && int($_[0]) % 2 != 0 },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and int($_[0]) % 2 != 0 21 1 22
247 13 ▶ 'even' => sub { defined $_[0] && int($_[0]) % 2 == 0 },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and int($_[0]) % 2 == 0 13 0 13
248 22 ▶ 'divisibleby' => sub { defined $_[0] && defined $_[1] && $_[1] != 0 && int($_[0]) % int($_[1]) == 0 },
Subroutine __ANON__: called CC=4 SCAR=19.1
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and defined $_[1] and $_[1] != 0 and int($_[0]) % int($_[1]) == 0 22 0 22
Condition33%2/6
defined $_[0] and defined $_[1] and $_[1] != 0
!ll&&!rl&&r
0022
defined $_[0] and defined $_[1]
!ll&&!rl&&r
0022
MC/DC0%0/3
defined $_[0] and defined $_[1] and $_[1] != 0
defined $_[0] defined $_[1] $_[1] != 0
Truth table25%1/4
A and B and C
A= defined $_[0]B= defined $_[1]C= $_[1] != 0
ABCresult
0XX0
10X0
1100
1111
249 8 ▶ 'upper' => sub { defined $_[0] && $_[0] eq uc($_[0]) && $_[0] =~ /[A-Z]/ },
Subroutine __ANON__: called CC=3 SCAR=11.1
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and $_[0] eq uc $_[0] and $_[0] =~ /[A-Z]/ 2 6 8
Condition66%2/3
defined $_[0] and $_[0] eq uc $_[0]
!ll&&!rl&&r
062
MC/DC50%1/2
defined $_[0] and $_[0] eq uc $_[0]
defined $_[0] $_[0] eq uc $_[0]
Truth table66%2/3
A and B
A= defined $_[0]B= $_[0] eq uc $_[0]
ABresult
0X0
100
111
250 3 ▶ 'lower' => sub { defined $_[0] && $_[0] eq lc($_[0]) && $_[0] =~ /[a-z]/ },
Subroutine __ANON__: called CC=3 SCAR=11.1
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and $_[0] eq lc $_[0] and $_[0] =~ /[a-z]/ 1 2 3
Condition66%2/3
defined $_[0] and $_[0] eq lc $_[0]
!ll&&!rl&&r
021
MC/DC50%1/2
defined $_[0] and $_[0] eq lc $_[0]
defined $_[0] $_[0] eq lc $_[0]
Truth table66%2/3
A and B
A= defined $_[0]B= $_[0] eq lc $_[0]
ABresult
0X0
100
111
251 6 ▶ 'equalto' => sub { defined $_[0] && defined $_[1] && $_[0] eq $_[1] },
Subroutine __ANON__: called CC=3 SCAR=14.2
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and defined $_[1] and $_[0] eq $_[1] 6 0 6
Condition33%1/3
defined $_[0] and defined $_[1]
!ll&&!rl&&r
006
MC/DC0%0/2
defined $_[0] and defined $_[1]
defined $_[0] defined $_[1]
Truth table33%1/3
A and B
A= defined $_[0]B= defined $_[1]
ABresult
0X0
100
111
252 4 ▶ 'ne' => sub { !(defined $_[0] && defined $_[1] && $_[0] eq $_[1]) },
Subroutine __ANON__: called CC=3 SCAR=13.1
Condition50%3/6
defined $_[0] && defined $_[1] && $_[0] eq $_[1]
!ll&&!rl&&r
022
defined $_[0] and defined $_[1]
!ll&&!rl&&r
004
MC/DC33%1/3
defined $_[0] && defined $_[1] && $_[0] eq $_[1]
defined $_[0] defined $_[1] $_[0] eq $_[1]
Truth table50%2/4
A and B && C
A= defined $_[0]B= defined $_[1]C= $_[0] eq $_[1]
ABCresult
0XX0
10X0
1100
1111
253 3 ▶ 'lt' => sub { defined $_[0] && defined $_[1] && $_[0] < $_[1] },
Subroutine __ANON__: called CC=3 SCAR=14.2
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and defined $_[1] and $_[0] < $_[1] 3 0 3
Condition33%1/3
defined $_[0] and defined $_[1]
!ll&&!rl&&r
003
MC/DC0%0/2
defined $_[0] and defined $_[1]
defined $_[0] defined $_[1]
Truth table33%1/3
A and B
A= defined $_[0]B= defined $_[1]
ABresult
0X0
100
111
254 3 ▶ 'le' => sub { defined $_[0] && defined $_[1] && $_[0] <= $_[1] },
Subroutine __ANON__: called CC=3 SCAR=14.2
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and defined $_[1] and $_[0] <= $_[1] 3 0 3
Condition33%1/3
defined $_[0] and defined $_[1]
!ll&&!rl&&r
003
MC/DC0%0/2
defined $_[0] and defined $_[1]
defined $_[0] defined $_[1]
Truth table33%1/3
A and B
A= defined $_[0]B= defined $_[1]
ABresult
0X0
100
111
255 2 ▶ 'gt' => sub { defined $_[0] && defined $_[1] && $_[0] > $_[1] },
Subroutine __ANON__: called CC=3 SCAR=14.2
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and defined $_[1] and $_[0] > $_[1] 2 0 2
Condition33%1/3
defined $_[0] and defined $_[1]
!ll&&!rl&&r
002
MC/DC0%0/2
defined $_[0] and defined $_[1]
defined $_[0] defined $_[1]
Truth table33%1/3
A and B
A= defined $_[0]B= defined $_[1]
ABresult
0X0
100
111
256 2 ▶ 'ge' => sub { defined $_[0] && defined $_[1] && $_[0] >= $_[1] },
Subroutine __ANON__: called CC=3 SCAR=14.2
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and defined $_[1] and $_[0] >= $_[1] 2 0 2
Condition33%1/3
defined $_[0] and defined $_[1]
!ll&&!rl&&r
002
MC/DC0%0/2
defined $_[0] and defined $_[1]
defined $_[0] defined $_[1]
Truth table33%1/3
A and B
A= defined $_[0]B= defined $_[1]
ABresult
0X0
100
111
257 23 ▶ 'in' => sub {
Subroutine __ANON__: called CC=9 SCAR=22.0
258 23 my ($val, $container) = @_;
259 23 ▶ return 0 unless defined $val && defined $container;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and defined $container 4 19 23
Condition66%2/3
defined $val and defined $container
!ll&&!rl&&r
4019
MC/DC50%1/2
defined $val and defined $container
defined $val defined $container
Truth table66%2/3
A and B
A= defined $valB= defined $container
ABresult
0X0
100
111
260 19 ▶ if (ref($container) eq 'ARRAY') { return (grep { defined $_ && $_ eq $val } @$container) ? 1 : 0 }
Branch: 2 branches
BranchTrueFalseTotal
if (ref $container eq "ARRAY") 9 10 19
grep({defined $_ and $_ eq $val;} @$container) ? : 6 3 9
Condition66%2/3
defined $_ && $_ eq $val
!ll&&!rl&&r
0186
MC/DC50%1/2
defined $_ && $_ eq $val
defined $_ $_ eq $val
Truth table66%2/3
A && B
A= defined $_B= $_ eq $val
ABresult
0X0
100
111
261 10 ▶ if (ref($container) eq 'HASH') { return exists $container->{$val} ? 1 : 0 }
Branch: 2 branches
BranchTrueFalseTotal
if (ref $container eq "HASH") 4 6 10
exists $container->{$val} ? : 2 2 4
262 6 ▶ return index($container, $val) >= 0 ? 1 : 0
Branch: 1 branch
BranchTrueFalseTotal
index($container, $val) != -1 ? : 3 3 6
263 },
264 13 );
265  
266 ###############################################################################
267 # Constructor
268 ###############################################################################
269 711 ▶sub new {
Subroutine new: called CC=13 SCAR=25.8
Pod documented
270 711 my $class = shift;
271 # new(key => value, ...) or new({ key => value, ... }) (0.02)
272 # Jinja2's Environment and many Perl modules take a hash reference;
273 # accept both so that either style works.
274 711 ▶ my %args = (@_ == 1 && ref($_[0]) eq 'HASH') ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && ref $_[0] eq 'HASH' ? : 5 706 711
Condition66%2/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
70605
MC/DC50%1/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
275 my $self = {
276 template_dir => defined $args{template_dir} ? $args{template_dir} : '.',
277 auto_escape => defined $args{auto_escape} ? $args{auto_escape} : 1,
278 trim_blocks => defined $args{trim_blocks} ? $args{trim_blocks} : 0,
279 lstrip_blocks => defined $args{lstrip_blocks} ? $args{lstrip_blocks} : 0,
280 block_start => defined $args{block_start} ? $args{block_start} : '{%',
281 block_end => defined $args{block_end} ? $args{block_end} : '%}',
282 var_start => defined $args{var_start} ? $args{var_start} : '{{',
283 var_end => defined $args{var_end} ? $args{var_end} : '}}',
284 comment_start => defined $args{comment_start} ? $args{comment_start} : '{#',
285 711 ▶ comment_end => defined $args{comment_end} ? $args{comment_end} : '#}',
Branch: 10 branches
BranchTrueFalseTotal
defined $args{'template_dir'} ? : 17 694 711
defined $args{'auto_escape'} ? : 685 26 711
defined $args{'trim_blocks'} ? : 10 701 711
defined $args{'lstrip_blocks'} ? : 8 703 711
defined $args{'block_start'} ? : 1 710 711
defined $args{'block_end'} ? : 1 710 711
defined $args{'var_start'} ? : 2 709 711
defined $args{'var_end'} ? : 2 709 711
defined $args{'comment_start'} ? : 0 711 711
defined $args{'comment_end'} ? : 0 711 711
286 _filters => { %FILTERS },
287 _tests => { %TESTS },
288 _blocks => {}, # block name => template string (for inheritance)
289 _macros => {}, # macro name => { args=>[], defaults=>[], body=>'' }
290 _extends => '', # parent template name
291 };
292 711 bless $self, $class;
293 711 return $self;
294 }
295  
296 ###############################################################################
297 # add_filter - Register a custom filter
298 ###############################################################################
299 7 ▶sub add_filter {
Subroutine add_filter: called CC=3 SCAR=11.0
Pod documented
300 7 my ($self, $name, $code) = @_;
301 7 ▶ croak "add_filter: name required" unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 7 7
302 7 ▶ croak "add_filter: code must be coderef" unless ref($code) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code eq "CODE" 1 6 7
303 6 $self->{_filters}{$name} = $code;
304 6 return $self;
305 }
306  
307 ###############################################################################
308 # add_test - Register a custom test
309 ###############################################################################
310 4 ▶sub add_test {
Subroutine add_test: called CC=3 SCAR=11.3
Pod documented
311 4 my ($self, $name, $code) = @_;
312 4 ▶ croak "add_test: name required" unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 4 4
313 4 ▶ croak "add_test: code must be coderef" unless ref($code) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code eq "CODE" 0 4 4
314 4 $self->{_tests}{$name} = $code;
315 4 return $self;
316 }
317  
318 ###############################################################################
319 # render_file - Render a template file with variables
320 ###############################################################################
321 12 ▶sub render_file {
Subroutine render_file: called CC=2 SCAR=7.0
Pod documented
322 12 my ($self, $filename, $vars) = @_;
323 12 ▶ $vars = {} unless defined $vars;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $vars 0 12 12
324 12 my $source = $self->_load_file($filename);
325 10 return $self->_render($source, $vars, $filename);
326 }
327  
328 ###############################################################################
329 # render_string - Render a template string with variables
330 ###############################################################################
331 694 ▶sub render_string {
Subroutine render_string: called CC=2 SCAR=6.9
Pod documented
332 694 my ($self, $source, $vars) = @_;
333 694 ▶ $vars = {} unless defined $vars;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $vars 1 693 694
334 694 return $self->_render($source, $vars, '<string>');
335 }
336  
337 ###############################################################################
338 # _load_file - Read template from disk
339 ###############################################################################
340 27 ▶sub _load_file {
Subroutine _load_file: called CC=5 SCAR=16.2
341 27 my ($self, $filename) = @_;
342  
343 # Prevent path traversal
344 27 ▶ if ($filename =~ /\.\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename =~ /\.\./) 1 26 27
345 1 croak "HP::Handy: path traversal not allowed in '$filename'";
346 }
347  
348 26 my $path;
349 26 ▶ if (File::Spec->file_name_is_absolute($filename)) {
Branch: 1 branch
BranchTrueFalseTotal
if ('File::Spec'->file_name_is_absolute($filename)) { } 0 26 26
350 0 $path = $filename;
351 }
352 else {
353 26 $path = File::Spec->catfile($self->{template_dir}, $filename);
354 }
355  
356 26 local *HPHANDY_FH;
357 26 ▶ open(HPHANDY_FH, "< $path") or croak "HP::Handy: cannot open '$path': $!";
Branch: 1 branch
BranchTrueFalseTotal
open HPHANDY_FH, "< $path" or croak("HP::Handy: cannot open '${path}': $!") 24 2 26
358 24 local $/;
359 24 my $source = <HPHANDY_FH>;
360 24 close(HPHANDY_FH);
361 24 ▶ return defined $source ? $source : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $source ? : 24 0 24
362 }
363  
364 ###############################################################################
365 # _render - Core rendering engine
366 ###############################################################################
367 704 ▶sub _render {
Subroutine _render: called CC=10 SCAR=23.9
368 704 my ($self, $source, $vars, $filename) = @_;
369 704 ▶ $filename = defined $filename ? $filename : '<string>';
Branch: 1 branch
BranchTrueFalseTotal
defined $filename ? : 704 0 704
370  
371 # Reset per-render state
372 704 $self->{_blocks} = {};
373 704 $self->{_macros} = {};
374 704 $self->{_extends} = '';
375  
376 # First pass: collect blocks and macros, detect extends
377 704 $source = $self->_preprocess($source, $vars);
378  
379 # If extends was found, do inheritance (iteratively for multi-level)
380 704 ▶ while ($self->{_extends} ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_extends"} ne "") 7 704 711
381 7 my $parent_src = $self->_load_file($self->{_extends});
382 7 $self->{_extends} = '';
383 # Collect parent blocks/macros without overwriting child definitions
384 7 $parent_src = $self->_preprocess_parent($parent_src, $vars);
385 # Apply child blocks to parent template
386 7 $source = $self->_apply_inheritance($parent_src, $vars);
387 # Now resolve all known blocks so deeper levels see merged content.
388 # For each block in _blocks, expand any other known blocks inside it.
389 7 my $merged = 1;
390 7 my $passes = 10;
391 7 ▶ while ($merged && $passes-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($merged and $passes-- > 0) 7 7 14
Condition66%2/3
$merged and $passes-- > 0
!ll&&!rl&&r
707
MC/DC50%1/2
$merged and $passes-- > 0
$merged $passes-- > 0
Truth table66%2/3
A and B
A= $mergedB= $passes-- > 0
ABresult
0X0
100
111
392 7 $merged = 0;
393 7 for my $bname (keys %{$self->{_blocks}}) {
394 10 my $prev = $self->{_blocks}{$bname};
395 10 my $val = $prev;
396 10 my $lim2 = 10;
397 10 ▶ while ($lim2-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lim2-- > 0) 10 0 10
398 10 my $prev2 = $val;
399 10 $val =~ s/\{%-?\s*block\s+(\w+)\s*-?%\}((?:(?!\{%-?\s*block\b)[\s\S])*?)\{%-?\s*endblock(?:\s+\1)?\s*-?%\}/
400 0 ▶ exists $self->{_blocks}{$1} ? $self->{_blocks}{$1} : "\x00BLK\x01$1\x01$2\x00EBLK\x00"
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'_blocks'}{$1} ? : 0 0 0
401 /gse;
402 10 ▶ last if $val eq $prev2;
Branch: 1 branch
BranchTrueFalseTotal
if $val eq $prev2 10 0 10
403 }
404 10 $val =~ s/\x00BLK\x01(\w+)\x01(.*?)\x00EBLK\x00/$2/gs;
405 10 $val =~ s/\{%-?\s*endblock(?:\s+\w+)?\s*-?%\}//g;
406 10 ▶ if ($val ne $prev) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val ne $prev) 0 10 10
407 0 $self->{_blocks}{$bname} = $val;
408 0 $merged = 1;
409 }
410 }
411 }
412 }
413  
414 704 return $self->_eval_template($source, $vars, $filename);
415 }
416  
417 ###############################################################################
418 # _preprocess - Scan for extends/block/macro; strip them for non-inherited use
419 ###############################################################################
420 704 ▶sub _preprocess {
Subroutine _preprocess: called CC=7 SCAR=19.5
421 704 my ($self, $source, $vars) = @_;
422  
423 # Detect {% extends "..." %}
424 704 ▶ if ($source =~ s/\{%-?\s*extends\s+["']([^"']+)["']\s*-?%\}[ \t]*\n?//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source =~ s/\{%-?\s*extends\s+["']([^"']+)["']\s*-?%\}[ \t]*\n?//) 6 698 704
425 6 $self->{_extends} = $1;
426 }
427  
428 # Collect {% macro name(args) %}...{% endmacro %}
429 704 ▶ while ($source =~ s/\{%-?\s*macro\s+(\w+)\s*\(([^)]*)\)\s*-?%\}(.*?)\{%-?\s*endmacro\s*-?%\}//s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source =~ s/\{%-?\s*macro\s+(\w+)\s*\(([^)]*)\)\s*-?%\}(.*?)\{%-?\s*endmacro\s*-?%\}//s) 10 704 714
430 10 my ($mname, $argstr, $body) = ($1, $2, $3);
431 10 my (@args, @defaults);
432 10 for my $a (split /\s*,\s*/, $argstr) {
433 16 $a =~ s/^\s+|\s+$//g;
434 16 ▶ next if $a eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $a eq "" 0 16 16
435 16 ▶ if ($a =~ /^(\w+)\s*=\s*(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a =~ /^(\w+)\s*=\s*(.+)$/) { } 2 14 16
436 2 push @args, $1;
437 2 push @defaults, $2;
438 }
439 else {
440 14 push @args, $a;
441 14 push @defaults, undef;
442 }
443 }
444 10 $self->{_macros}{$mname} = { args => \@args, defaults => \@defaults, body => $body };
445 }
446  
447 # Collect {% block name %}...{% endblock %} from child
448 704 ▶ while ($source =~ s/\{%-?\s*block\s+(\w+)\s*-?%\}(.*?)\{%-?\s*endblock(?:\s+\1)?\s*-?%\}/$self->_store_block($1, $2)/se) {}
Branch: 1 branch
BranchTrueFalseTotal
if ($source =~ s/\{%-?\s*block\s+(\w+)\s*-?%\}(.*?)\{%-?\s*endblock(?:\s+\1)?\s*-?%\}/$self->_store_block($1, $2);/es) 10 704 714
449  
450 704 return $source;
451 }
452  
453 10 ▶sub _store_block {
Subroutine _store_block: called CC=2 SCAR=7.1
454 10 my ($self, $name, $content) = @_;
455 10 ▶ $self->{_blocks}{$name} = $content unless exists $self->{_blocks}{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_blocks"}{$name} 10 0 10
456 10 return '';
457 }
458  
459  
460 # _preprocess_parent - Scan parent for extends/macro; collect NEW blocks only
461 # (does NOT strip block tags -- those must survive for _apply_inheritance)
462 7 ▶sub _preprocess_parent {
Subroutine _preprocess_parent: called CC=8 SCAR=30.6
463 7 my ($self, $source, $vars) = @_;
464  
465 # Detect {% extends "..." %} in parent (for multi-level)
466 7 ▶ if ($source =~ s/\{%-?\s*extends\s+["']([^"']+)["']\s*-?%\}[ \t]*\n?//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source =~ s/\{%-?\s*extends\s+["']([^"']+)["']\s*-?%\}[ \t]*\n?//) 1 6 7
467 1 $self->{_extends} = $1;
468 }
469  
470 # Collect macros from parent (without overwriting child macros)
471 7 ▶ while ($source =~ s/\{%-?\s*macro\s+(\w+)\s*\(([^)]*)\)\s*-?%\}(.*?)\{%-?\s*endmacro\s*-?%\}//s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source =~ s/\{%-?\s*macro\s+(\w+)\s*\(([^)]*)\)\s*-?%\}(.*?)\{%-?\s*endmacro\s*-?%\}//s) 0 7 7
472 0 my ($mname, $argstr, $body) = ($1, $2, $3);
473 0 ▶ next if exists $self->{_macros}{$mname};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_macros"}{$mname} 0 0 0
474 0 my (@args, @defaults);
475 0 for my $a (split /\s*,\s*/, $argstr) {
476 0 $a =~ s/^\s+|\s+$//g;
477 0 ▶ next if $a eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $a eq "" 0 0 0
478 0 ▶ if ($a =~ /^(\w+)\s*=\s*(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a =~ /^(\w+)\s*=\s*(.+)$/) { } 0 0 0
479 0 push @args, $1;
480 0 push @defaults, $2;
481 }
482 else {
483 0 push @args, $a;
484 0 push @defaults, undef;
485 }
486 }
487 0 $self->{_macros}{$mname} = { args => \@args, defaults => \@defaults, body => $body };
488 }
489  
490 # Collect NEW blocks (without overwriting child blocks).
491 # Expand already-known child blocks within the body before storing.
492 7 my $tmp = $source;
493 7 ▶ while ($tmp =~ s/\{%-?\s*block\s+(\w+)\s*-?%\}(.*)\{%-?\s*endblock(?:\s+\1)?\s*-?%\}/
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp =~ s/\{%-?\s*block\s+(\w+)\s*-?%\}(.*)\{%-?\s*endblock(?:\s+\1)?\s*-?%\}/$self->_store_block_expanded($1, $2);/es) 7 7 14
494 7 $self->_store_block_expanded($1, $2)
495 /se) {}
496  
497 7 return $source; # keep block tags intact for _apply_inheritance
498 }
499  
500 7 ▶sub _store_block_expanded {
Subroutine _store_block_expanded: called CC=5 SCAR=16.1
501 7 my ($self, $name, $body) = @_;
502 7 ▶ unless (exists $self->{_blocks}{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_blocks"}{$name}) 1 6 7
503 # Expand already-resolved child blocks inside this body.
504 # Use _apply_inheritance logic on the snippet.
505 1 my $expanded = $body;
506 1 my $lim = 10;
507 1 ▶ while ($lim-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lim-- > 0) 2 0 2
508 2 my $prev = $expanded;
509 # Replace innermost (non-nested) blocks that are known
510 2 $expanded =~ s/\{%-?\s*block\s+(\w+)\s*-?%\}((?:(?!\{%-?\s*block\b)[\s\S])*?)\{%-?\s*endblock(?:\s+\1)?\s*-?%\}/
511 1 ▶ exists $self->{_blocks}{$1} ? $self->{_blocks}{$1} : "__BLK__" . $1 . "__SEP__" . $2 . "__EBLK__"
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'_blocks'}{$1} ? : 1 0 1
512 /gse;
513 2 ▶ last if $expanded eq $prev;
Branch: 1 branch
BranchTrueFalseTotal
if $expanded eq $prev 1 1 2
514 }
515 # Clean up any un-expanded block tags
516 1 $expanded =~ s/__BLK__(\w+)__SEP__(.*?)__EBLK__/$2/gs;
517 1 $expanded =~ s/\{%-?\s*endblock(?:\s+\w+)?\s*-?%\}//g;
518 1 $self->{_blocks}{$name} = $expanded;
519 }
520 7 return '';
521 }
522  
523  
524 ###############################################################################
525 # _apply_inheritance - Render parent with child blocks
526 ###############################################################################
527 7 ▶sub _apply_inheritance {
Subroutine _apply_inheritance: called CC=4 SCAR=13.9
528 7 my ($self, $parent_src, $vars) = @_;
529  
530 # Iteratively substitute innermost (non-nested) blocks first.
531 # A block body that contains no other {% block %} tags is "innermost".
532 7 my $limit = 20;
533 7 ▶ while ($limit-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($limit-- > 0) 15 0 15
534 15 my $changed = 0;
535 15 $parent_src =~ s/\{%-?\s*block\s+(\w+)\s*-?%\}((?:(?!\{%-?\s*block\b)[\s\S])*?)\{%-?\s*endblock(?:\s+\1)?\s*-?%\}/
536 10 ▶ do { $changed = 1; exists $self->{_blocks}{$1} ? $self->{_blocks}{$1} : $2 }
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'_blocks'}{$1} ? : 9 1 10
537 /gse;
538 15 ▶ last unless $changed;
Branch: 1 branch
BranchTrueFalseTotal
unless $changed 7 8 15
539 }
540  
541 # Remove any orphaned {% endblock %} left from outer block substitutions
542 7 $parent_src =~ s/\{%-?\s*endblock(?:\s+\w+)?\s*-?%\}//g;
543  
544 7 return $parent_src;
545 }
546  
547 ###############################################################################
548 # _eval_template - Evaluate template directives recursively
549 ###############################################################################
550 723 ▶sub _eval_template {
Subroutine _eval_template: called CC=7 SCAR=19.5
551 723 my ($self, $source, $vars, $filename) = @_;
552 723 ▶ $filename = defined $filename ? $filename : '<string>';
Branch: 1 branch
BranchTrueFalseTotal
defined $filename ? : 723 0 723
553  
554 # Apply trim_blocks / lstrip_blocks
555 723 ▶ if ($self->{trim_blocks}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"trim_blocks"}) 8 715 723
556 8 $source =~ s/(%\}|#\})[ \t]*\n/$1\n/g; # already minimal; remove newline after tag
557 8 $source =~ s/(%\}|#\})\n/$1/g;
558 }
559 723 ▶ if ($self->{lstrip_blocks}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"lstrip_blocks"}) 8 715 723
560 8 $source =~ s/^[ \t]*(\{[%#])/$1/mg;
561 }
562  
563 723 my $bs = quotemeta($self->{block_start});
564 723 my $be = quotemeta($self->{block_end});
565 723 my $vs = quotemeta($self->{var_start});
566 723 my $ve = quotemeta($self->{var_end});
567 723 my $cs = quotemeta($self->{comment_start});
568 723 my $ce = quotemeta($self->{comment_end});
569  
570 # Protect {% raw %}...{% endraw %} blocks BEFORE comment stripping,
571 # so that {# ... #} inside raw blocks is preserved as-is.
572 723 my @raw_chunks;
573 723 my $raw_ph = "\x00RAW\x00";
574 723 my $bs_re = quotemeta($self->{block_start});
575 723 my $be_re = quotemeta($self->{block_end});
576 723 $source =~ s/${bs_re}-?\s*raw\s*-?${be_re}(.*?)${bs_re}-?\s*endraw\s*-?${be_re}/
577 9 my $idx = scalar @raw_chunks;
578 9 push @raw_chunks, $1;
579 9 "${raw_ph}${idx}\x00"
580 /gse;
581  
582 # Remove comments (after raw protection so {# #} inside raw is safe)
583 723 $source =~ s/$cs.*?$ce//gs;
584  
585 # Tokenise into chunks
586 # Tokens: [ 'text', $str ] or [ 'var', $expr ] or [ 'tag', $stmt ]
587 723 my @tokens = $self->_tokenize($source);
588  
589 # Restore raw chunks in text tokens
590 723 for my $tok (@tokens) {
591 1763 ▶ if ($tok->[0] eq 'text' && $tok->[1] =~ /\Q$raw_ph\E/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tok->[0] eq "text" and $tok->[1] =~ /\Q$raw_ph\E/) 9 1754 1763
Condition100%3/3
$tok->[0] eq "text" and $tok->[1] =~ /\Q$raw_ph\E/
!ll&&!rl&&r
13524029
MC/DC100%2/2
$tok->[0] eq "text" and $tok->[1] =~ /\Q$raw_ph\E/
$tok->[0] eq "text" $tok->[1] =~ /\Q$raw_ph\E/
Truth table100%3/3
A and B
A= $tok->[0] eq "text"B= $tok->[1] =~ /\Q$raw_ph\E/
ABresult
0X0
100
111
592 9 $tok->[1] =~ s/\Q${raw_ph}\E(\d+)\x00/$raw_chunks[$1]/g;
593 }
594 }
595  
596 723 return $self->_eval_tokens(\@tokens, $vars, 0, $filename);
597 }
598  
599 ###############################################################################
600 # _tokenize - Split source into text/var/tag tokens
601 ###############################################################################
602 723 ▶sub _tokenize {
Subroutine _tokenize: called CC=28 SCAR=33.5
603 723 my ($self, $source) = @_;
604  
605 723 my $bs = $self->{block_start}; # {%
606 723 my $be = $self->{block_end}; # %}
607 723 my $vs = $self->{var_start}; # {{
608 723 my $ve = $self->{var_end}; # }}
609  
610 # Build split pattern
611 723 my $qbs = quotemeta($bs); my $qbe = quotemeta($be);
612 723 my $qvs = quotemeta($vs); my $qve = quotemeta($ve);
613  
614 723 my @tokens;
615 723 my $pos = 0;
616 723 my $len = length($source);
617  
618 723 ▶ while ($pos < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < $len) 1404 671 2075
619 # Find next tag start
620 1404 my $var_pos = index($source, $vs, $pos);
621 1404 my $block_pos = index($source, $bs, $pos);
622  
623 # No more tags
624 1404 ▶ if ($var_pos < 0 && $block_pos < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var_pos < 0 and $block_pos < 0) 52 1352 1404
Condition100%3/3
$var_pos < 0 and $block_pos < 0
!ll&&!rl&&r
68666652
MC/DC100%2/2
$var_pos < 0 and $block_pos < 0
$var_pos < 0 $block_pos < 0
Truth table100%3/3
A and B
A= $var_pos < 0B= $block_pos < 0
ABresult
0X0
100
111
625 52 push @tokens, [ 'text', substr($source, $pos) ];
626 52 last;
627 }
628  
629 # Determine which comes first
630 1352 my $next;
631 1352 ▶ if ($var_pos >= 0 && ($block_pos < 0 || $var_pos <= $block_pos)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var_pos >= 0 and $block_pos < 0 || $var_pos <= $block_pos) { } 494 858 1352
Condition100%6/6
$var_pos >= 0 and $block_pos < 0 || $var_pos <= $block_pos
!ll&&!rl&&r
666192494
$block_pos < 0 or $var_pos <= $block_pos
l!l&&r!l&&!r
366128192
MC/DC100%3/3
$var_pos >= 0 and $block_pos < 0 || $var_pos <= $block_pos
$var_pos >= 0 $block_pos < 0 $var_pos <= $block_pos
Truth table100%4/4
A and B or C
A= $var_pos >= 0B= $block_pos < 0C= $var_pos <= $block_pos
ABCresult
0XX0
1000
11X1
1011
632 494 $next = $var_pos;
633 494 my $end = index($source, $ve, $next + length($vs));
634 494 ▶ if ($end < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end < 0) 0 494 494
635 0 push @tokens, [ 'text', substr($source, $pos) ];
636 0 last;
637 }
638 494 ▶ if ($next > $pos) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next > $pos) 38 456 494
639 38 push @tokens, [ 'text', substr($source, $pos, $next - $pos) ];
640 }
641 494 my $raw_expr = substr($source, $next + length($vs), $end - $next - length($vs));
642 # Whitespace control dash: "{{- expr -}}"
643 # The dash must be the very first/last non-whitespace character AND
644 # must be followed/preceded by whitespace (not part of the expression).
645 # Rule: if raw_expr starts with "-" followed by space/end, it's WS control.
646 # "-5" or "-x" is a negative expression, NOT whitespace control.
647 494 my $strip_left = ($raw_expr =~ /^-(?:\s|$)/);
648 494 my $strip_right = ($raw_expr =~ /(?:^|\s)-\s*$/);
649 494 my $expr = $raw_expr;
650 494 $expr =~ s/^\s+|\s+$//g;
651 494 ▶ $expr =~ s/^-\s+// if $strip_left; # remove leading "- "
Branch: 1 branch
BranchTrueFalseTotal
if $strip_left 7 487 494
652 494 ▶ $expr =~ s/\s+-$// if $strip_right; # remove trailing " -"
Branch: 1 branch
BranchTrueFalseTotal
if $strip_right 7 487 494
653 # Strip whitespace from preceding text token if {{- }}
654 494 ▶ if ($strip_left && @tokens && $tokens[-1][0] eq 'text') {
Branch: 1 branch
BranchTrueFalseTotal
if ($strip_left and @tokens and $tokens[-1][0] eq "text") 6 488 494
Condition83%5/6
$strip_left and @tokens and $tokens[-1][0] eq "text"
!ll&&!rl&&r
48806
$strip_left and @tokens
!ll&&!rl&&r
48716
MC/DC66%2/3
$strip_left and @tokens and $tokens[-1][0] eq "text"
$strip_left @tokens $tokens[-1][0] eq "text"
Truth table75%3/4
A and B and C
A= $strip_leftB= @tokensC= $tokens[-1][0] eq "text"
ABCresult
0XX0
10X0
1100
1111
655 6 $tokens[-1][1] =~ s/\s+$//;
656 6 ▶ splice @tokens, -1 if $tokens[-1][1] eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $tokens[-1][1] eq "" 3 3 6
657 }
658 494 push @tokens, [ 'var', $expr, $strip_right ];
659 494 $pos = $end + length($ve);
660 # Strip leading whitespace from next text if {{ -}}
661 494 ▶ if ($strip_right && $pos < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($strip_right and $pos < $len) 6 488 494
Condition100%3/3
$strip_right and $pos < $len
!ll&&!rl&&r
48716
MC/DC100%2/2
$strip_right and $pos < $len
$strip_right $pos < $len
Truth table100%3/3
A and B
A= $strip_rightB= $pos < $len
ABresult
0X0
100
111
662 6 my $rest = substr($source, $pos);
663 6 my $stripped = $rest;
664 6 $stripped =~ s/^\s+//;
665 6 $pos += length($rest) - length($stripped);
666 }
667 }
668 else {
669 858 $next = $block_pos;
670 858 my $end = index($source, $be, $next + length($bs));
671 858 ▶ if ($end < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end < 0) 0 858 858
672 0 push @tokens, [ 'text', substr($source, $pos) ];
673 0 last;
674 }
675 858 ▶ if ($next > $pos) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next > $pos) 325 533 858
676 # Whitespace control: {% - strips preceding whitespace
677 325 my $text_before = substr($source, $pos, $next - $pos);
678 325 my $stmt_raw = substr($source, $next + length($bs), $end - $next - length($bs));
679 325 ▶ if ($stmt_raw =~ /^-/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stmt_raw =~ /^-/) 2 323 325
680 2 $text_before =~ s/\s+$//;
681 }
682 325 ▶ push @tokens, [ 'text', $text_before ] if $text_before ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $text_before ne "" 324 1 325
683 }
684 858 my $stmt_full = substr($source, $next + length($bs), $end - $next - length($bs));
685 858 my $rstrip_tag = ($stmt_full =~ /\s*-\s*$/);
686 858 my $stmt = $stmt_full;
687 858 $stmt =~ s/^\s+|\s+$//g;
688 858 $stmt =~ s/^-\s*|\s*-$//g;
689 858 push @tokens, [ 'tag', $stmt ];
690 858 $pos = $end + length($be);
691 # Whitespace control: -%} strips following whitespace
692 858 ▶ if ($rstrip_tag && $pos < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rstrip_tag and $pos < $len) 2 856 858
Condition66%2/3
$rstrip_tag and $pos < $len
!ll&&!rl&&r
85602
MC/DC50%1/2
$rstrip_tag and $pos < $len
$rstrip_tag $pos < $len
Truth table66%2/3
A and B
A= $rstrip_tagB= $pos < $len
ABresult
0X0
100
111
693 2 substr($source, $pos) =~ s/^\s+//;
694 2 ▶ $pos = $len - length($source) + $pos if $source =~ s/^(\s+)//;
Branch: 1 branch
BranchTrueFalseTotal
if $source =~ s/^(\s+)// 0 2 2
695 # Simpler: just advance pos past leading whitespace
696 2 my $rest = substr($source, $pos);
697 2 my $stripped = $rest;
698 2 $stripped =~ s/^\s+//;
699 2 $pos += length($rest) - length($stripped);
700 }
701 # trim_blocks: eat newline after %}
702 858 ▶ if (!$rstrip_tag && $self->{trim_blocks} && $pos < $len && substr($source, $pos, 1) eq "\n") {
Branch: 1 branch
BranchTrueFalseTotal
if (not $rstrip_tag and $self->{"trim_blocks"} and $pos < $len and substr($source, $pos, 1) eq "\n") 0 858 858
Condition88%8/9
not $rstrip_tag and $self->{"trim_blocks"} and $pos < $len and substr($source, $pos, 1) eq "\n"
!ll&&!rl&&r
847110
not $rstrip_tag and $self->{"trim_blocks"} and $pos < $len
!ll&&!rl&&r
842511
not $rstrip_tag and $self->{"trim_blocks"}
!ll&&!rl&&r
284016
MC/DC0%0/4
not $rstrip_tag and $self->{"trim_blocks"} and $pos < $len and substr($source, $pos, 1) eq "\n"
not $rstrip_tag $self->{"trim_blocks"} $pos < $len substr($source, $pos, 1) eq "\n"
Truth table80%4/5
not(A) and B and C and D
A= not $rstrip_tagB= $self->{"trim_blocks"}C= $pos < $lenD= substr($source, $pos, 1) eq "\n"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
703 0 $pos++;
704 }
705 }
706 }
707 723 return @tokens;
708 }
709  
710 ###############################################################################
711 # _eval_tokens - Execute a token list and return rendered string
712 ###############################################################################
713 1295 ▶sub _eval_tokens {
Subroutine _eval_tokens: called CC=31 SCAR=43.0
714 1295 my ($self, $tokens, $vars, $start, $filename) = @_;
715 1295 ▶ $start = 0 unless defined $start;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $start 0 1295 1295
716  
717 1295 my $out = '';
718 1295 my $i = $start;
719  
720 1295 ▶ while ($i <= $#$tokens) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $tokens->$#*) 1568 1295 2863
721 1568 my ($type, $content) = @{$tokens->[$i]};
722  
723 1568 ▶ if ($type eq 'text') {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'text') { } 390 1178 1568
elsif ($type eq 'var') { } 679 499 1178
elsif ($type eq 'tag') { } 499 0 499
724 390 $out .= $content;
725 390 $i++;
726 }
727 elsif ($type eq 'var') {
728 679 my $val = $self->_eval_expr($content, $vars);
729 679 ▶ if (ref($val) eq 'HP::Handy::SafeString') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $val eq 'HP::Handy::SafeString') { } 20 659 679
elsif ($self->{'auto_escape'} and defined $val and not ref $val) { } 26 1266 1292
730 20 $val = $$val;
731 }
732 ▶ elsif ($self->{auto_escape} && defined $val && !ref($val)) {
Condition66%4/6
$self->{"auto_escape"} and defined $val and not ref $val
!ll&&!rl&&r
633026
$self->{"auto_escape"} and defined $val
!ll&&!rl&&r
633026
MC/DC33%1/3
$self->{"auto_escape"} and defined $val and not ref $val
$self->{"auto_escape"} defined $val not ref $val
Truth table50%2/4
A and B and not(C)
A= $self->{"auto_escape"}B= defined $valC= not ref $val
ABCresult
0XX0
10X0
1100
1111
733 26 $val = _html_escape($val);
734 }
735 679 ▶ $out .= defined $val ? $val : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $val ? : 672 7 679
736 679 $i++;
737 }
738 elsif ($type eq 'tag') {
739 499 my $stmt = $content;
740  
741 # --- set ---
742 499 ▶ if ($stmt =~ /^set\s+(\w+)\s*=\s*(.+)$/) {
Branch: 10 branches
BranchTrueFalseTotal
if ($stmt =~ /^set\s+(\w+)\s*=\s*(.+)$/) { } 54 445 499
elsif ($stmt =~ /^set\s+(\w+)\s*$/) { } 0 445 445
elsif ($stmt =~ /^if\s+(.+)$/) { } 298 147 445
elsif ($stmt =~ /^for\s+(.+?)\s+in\s+(.+?)(?:\s+if\s+(.+))?$/) { } 126 21 147
elsif ($stmt =~ /^include\s+["']([^"']+)["'](?:\s+ignore\s+missing)?$/) { } 8 13 21
elsif ($stmt =~ /^(\w+)\s*\(([^)]*)\)$/ and exists $self->{'_macros'}{$1}) { } 0 13 13
elsif ($stmt eq 'raw') { } 0 13 13
elsif ($stmt =~ /^with(?:\s+(.+))?$/) { } 13 0 13
elsif ($stmt =~ /^block\s+(\w+)$/) { } 0 0 0
elsif ($stmt =~ /^end(if|for|block|macro|raw|with|set)/ or $stmt =~ /^(else|elif)/) { } 0 0 0
743 54 my ($vname, $expr) = ($1, $2);
744 54 $vars->{$vname} = $self->_eval_expr($expr, $vars);
745 54 $i++;
746 }
747 # --- set block (multi-line) ---
748 elsif ($stmt =~ /^set\s+(\w+)\s*$/) {
749 0 my $vname = $1;
750 0 my ($body, $ni) = $self->_collect_until($tokens, $i + 1, 'endset');
751 0 $vars->{$vname} = $self->_eval_tokens($tokens, $vars, $i + 1, $filename);
752 # re-render just the body tokens
753 0 my @body_tokens = @{$tokens}[$i+1 .. $ni-1];
754 0 $vars->{$vname} = $self->_eval_tokens(\@body_tokens, $vars, 0, $filename);
755 0 $i = $ni + 1;
756 }
757 # --- if / elif / else / endif ---
758 elsif ($stmt =~ /^if\s+(.+)$/) {
759 298 my $cond_expr = $1;
760 298 my ($result, $ni) = $self->_eval_if($tokens, $i, $vars, $filename);
761 298 $out .= $result;
762 298 $i = $ni;
763 }
764 # --- for / endfor ---
765 elsif ($stmt =~ /^for\s+(.+?)\s+in\s+(.+?)(?:\s+if\s+(.+))?$/) {
766 126 my ($loop_var, $iter_expr, $cond_expr) = ($1, $2, $3);
767 126 my ($result, $ni) = $self->_eval_for($tokens, $i, $vars, $loop_var, $iter_expr, $cond_expr, $filename);
768 126 $out .= $result;
769 126 $i = $ni;
770 }
771 # --- include ---
772 elsif ($stmt =~ /^include\s+["']([^"']+)["'](?:\s+ignore\s+missing)?$/) {
773 8 my $inc_file = $1;
774 8 my $inc_src;
775 8 eval { $inc_src = $self->_load_file($inc_file) };
776 8 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 7 8
777 1 ▶ $inc_src = '' if $stmt =~ /ignore\s+missing/;
Branch: 1 branch
BranchTrueFalseTotal
if $stmt =~ /ignore\s+missing/ 1 0 1
778 1 ▶ croak $@ unless $stmt =~ /ignore\s+missing/;
Branch: 1 branch
BranchTrueFalseTotal
unless $stmt =~ /ignore\s+missing/ 0 1 1
779 }
780 8 ▶ $out .= $self->_eval_template($inc_src, { %$vars }, $inc_file) if defined $inc_src;
Branch: 1 branch
BranchTrueFalseTotal
if defined $inc_src 8 0 8
781 8 $i++;
782 }
783 # --- call macro ---
784 ▶ elsif ($stmt =~ /^(\w+)\s*\(([^)]*)\)$/ && exists $self->{_macros}{$1}) {
Condition33%1/3
$stmt =~ /^(\w+)\s*\(([^)]*)\)$/ and exists $self->{"_macros"}{$1}
!ll&&!rl&&r
1300
MC/DC0%0/2
$stmt =~ /^(\w+)\s*\(([^)]*)\)$/ and exists $self->{"_macros"}{$1}
$stmt =~ /^(\w+)\s*\(([^)]*)\)$/ exists $self->{"_macros"}{$1}
Truth table33%1/3
A and B
A= $stmt =~ /^(\w+)\s*\(([^)]*)\)$/B= exists $self->{"_macros"}{$1}
ABresult
0X0
100
111
785 0 my ($mname, $argstr) = ($1, $2);
786 0 $out .= $self->_call_macro($mname, $argstr, $vars, $filename);
787 0 $i++;
788 }
789 # --- raw / endraw ---
790 elsif ($stmt eq 'raw') {
791 0 my ($raw, $ni) = $self->_collect_raw($tokens, $i + 1);
792 0 $out .= $raw;
793 0 $i = $ni + 1;
794 }
795 # --- with / endwith ---
796 elsif ($stmt =~ /^with(?:\s+(.+))?$/) {
797 13 my $assigns = $1;
798 13 my %local_vars = %$vars;
799 13 ▶ if (defined $assigns) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $assigns) 13 0 13
800 13 for my $pair (_split_args($assigns)) {
801 15 ▶ if ($pair =~ /^(\w+)\s*=\s*(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pair =~ /^(\w+)\s*=\s*(.+)$/) 15 0 15
802 15 $local_vars{$1} = $self->_eval_expr($2, { %local_vars });
803 }
804 }
805 }
806 13 my ($body_tokens_ref, $ni) = $self->_collect_until($tokens, $i + 1, 'endwith');
807 13 $out .= $self->_eval_tokens($body_tokens_ref, { %local_vars }, 0, $filename);
808 13 $i = $ni + 1;
809 }
810 # --- block (standalone, no inheritance) ---
811 elsif ($stmt =~ /^block\s+(\w+)$/) {
812 0 my $bname = $1;
813 0 my ($body_tokens_ref, $ni) = $self->_collect_until($tokens, $i + 1, "endblock $bname", 'endblock');
814 0 ▶ if (exists $self->{_blocks}{$bname}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'_blocks'}{$bname}) { } 0 0 0
815 0 $out .= $self->_eval_template($self->{_blocks}{$bname}, $vars, $filename);
816 }
817 else {
818 0 $out .= $self->_eval_tokens($body_tokens_ref, $vars, 0, $filename);
819 }
820 0 $i = $ni + 1;
821 }
822 # skip endxxx tags (consumed by their openers above)
823 ▶ elsif ($stmt =~ /^end(if|for|block|macro|raw|with|set)/ || $stmt =~ /^(else|elif)/) {
Condition0%0/3
$stmt =~ /^end(if|for|block|macro|raw|with|set)/ or $stmt =~ /^(else|elif)/
l!l&&r!l&&!r
000
MC/DC0%0/2
$stmt =~ /^end(if|for|block|macro|raw|with|set)/ or $stmt =~ /^(else|elif)/
$stmt =~ /^end(if|for|block|macro|raw|with|set)/ $stmt =~ /^(else|elif)/
Truth table0%0/3
A or B
A= $stmt =~ /^end(if|for|block|macro|raw|with|set)/B= $stmt =~ /^(else|elif)/
ABresult
1X1
011
000
824 0 $i++;
825 }
826 else {
827 # Unknown tag: pass through as-is for forward compatibility
828 0 $out .= $self->{block_start} . ' ' . $stmt . ' ' . $self->{block_end};
829 0 $i++;
830 }
831 }
832 else {
833 0 $i++;
834 }
835 }
836 1295 return $out;
837 }
838  
839 ###############################################################################
840 # _eval_if - Evaluate if/elif/else/endif block
841 ###############################################################################
842 298 ▶sub _eval_if {
Subroutine _eval_if: called CC=15 SCAR=27.1
843 298 my ($self, $tokens, $start, $vars, $filename) = @_;
844  
845 # Collect all branches: [ [cond_expr, [tokens...]], ..., [undef, [tokens...]] ]
846 298 my @branches;
847 298 my $cur_cond = $tokens->[$start][1];
848 298 $cur_cond =~ s/^if\s+//;
849 298 my @cur_body;
850 298 my $depth = 1;
851 298 my $i = $start + 1;
852  
853 298 ▶ while ($i <= $#$tokens) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $tokens->$#*) 703 0 703
854 703 my ($type, $content) = @{$tokens->[$i]};
855 703 ▶ if ($type eq 'tag') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'tag') { } 364 339 703
856 364 ▶ if ($content =~ /^if\b/) {
Branch: 4 branches
BranchTrueFalseTotal
if ($content =~ /^if\b/) { } 8 356 364
elsif ($content eq 'endif') { } 306 50 356
elsif ($depth == 1 and $content =~ /^elif\s+(.+)$/) { } 13 37 50
elsif ($depth == 1 and $content eq 'else') { } 26 11 37
857 8 $depth++;
858 8 push @cur_body, $tokens->[$i];
859 }
860 elsif ($content eq 'endif') {
861 306 $depth--;
862 306 ▶ if ($depth == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth == 0) { } 298 8 306
863 298 push @branches, [ $cur_cond, [@cur_body] ];
864 298 $i++;
865 298 last;
866 }
867 else {
868 8 push @cur_body, $tokens->[$i];
869 }
870 }
871 ▶ elsif ($depth == 1 && $content =~ /^elif\s+(.+)$/) {
Condition100%3/3
$depth == 1 and $content =~ /^elif\s+(.+)$/
!ll&&!rl&&r
33413
MC/DC100%2/2
$depth == 1 and $content =~ /^elif\s+(.+)$/
$depth == 1 $content =~ /^elif\s+(.+)$/
Truth table100%3/3
A and B
A= $depth == 1B= $content =~ /^elif\s+(.+)$/
ABresult
0X0
100
111
872 13 push @branches, [ $cur_cond, [@cur_body] ];
873 13 $cur_cond = $1;
874 13 @cur_body = ();
875 }
876 ▶ elsif ($depth == 1 && $content eq 'else') {
Condition100%3/3
$depth == 1 and $content eq "else"
!ll&&!rl&&r
3826
MC/DC100%2/2
$depth == 1 and $content eq "else"
$depth == 1 $content eq "else"
Truth table100%3/3
A and B
A= $depth == 1B= $content eq "else"
ABresult
0X0
100
111
877 26 push @branches, [ $cur_cond, [@cur_body] ];
878 26 $cur_cond = undef;
879 26 @cur_body = ();
880 }
881 else {
882 11 push @cur_body, $tokens->[$i];
883 }
884 }
885 else {
886 339 push @cur_body, $tokens->[$i];
887 }
888 405 $i++;
889 }
890 298 ▶ push @branches, [ $cur_cond, [@cur_body] ] if @cur_body || !defined $cur_cond;
Branch: 1 branch
BranchTrueFalseTotal
if @cur_body or not defined $cur_cond 298 0 298
Condition33%1/3
@cur_body or not defined $cur_cond
l!l&&r!l&&!r
29800
MC/DC0%0/2
@cur_body or not defined $cur_cond
@cur_body not defined $cur_cond
Truth table33%1/3
A or not(B)
A= @cur_bodyB= not defined $cur_cond
ABresult
1X1
011
000
891  
892 298 my $result = '';
893 298 for my $branch (@branches) {
894 394 my ($cond, $body) = @$branch;
895 394 ▶ if (!defined $cond || _truthy($self->_eval_expr($cond, $vars))) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $cond or _truthy($self->_eval_expr($cond, $vars))) 225 169 394
Condition100%3/3
not defined $cond or _truthy($self->_eval_expr($cond, $vars))
l!l&&r!l&&!r
16209169
MC/DC100%2/2
not defined $cond or _truthy($self->_eval_expr($cond, $vars))
not defined $cond _truthy($self->_eval_expr($cond, $vars))
Truth table100%3/3
not(A) or B
A= not defined $condB= _truthy($self->_eval_expr($cond, $vars))
ABresult
1X1
011
000
896 225 $result = $self->_eval_tokens($body, $vars, 0, $filename);
897 225 last;
898 }
899 }
900 298 return ($result, $i);
901 }
902  
903 ###############################################################################
904 # _truthy - Truth value of a template value (0.02)
905 #
906 # Jinja2 treats an empty list and an empty dict as false. In Perl a
907 # reference is always true, so 0.01 treated [] and {} as true.
908 # Scalars keep Perl's rule (undef, '' and '0' are false).
909 ###############################################################################
910 552 ▶sub _truthy {
Subroutine _truthy: called CC=7 SCAR=19.5
911 552 my ($v) = @_;
912 552 ▶ return 0 unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 9 543 552
913 543 ▶ if (ref($v) eq 'ARRAY') { return scalar(@$v) ? 1 : 0 }
Branch: 2 branches
BranchTrueFalseTotal
if (ref $v eq "ARRAY") 12 531 543
scalar @$v ? : 3 9 12
914 531 ▶ if (ref($v) eq 'HASH') { return scalar(keys %$v) ? 1 : 0 }
Branch: 2 branches
BranchTrueFalseTotal
if (ref $v eq "HASH") 6 525 531
scalar keys %$v ? : 1 5 6
915 525 ▶ return $v ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$v ? : 277 248 525
916 }
917  
918 ###############################################################################
919 # _eval_for - Evaluate for loop
920 ###############################################################################
921 126 ▶sub _eval_for {
Subroutine _eval_for: called CC=22 SCAR=31.0
922 126 my ($self, $tokens, $start, $vars, $loop_var, $iter_expr, $cond_expr, $filename) = @_;
923  
924 # Collect loop body tokens (until endfor at depth 1)
925 126 my (@body_tokens, @else_tokens);
926 126 my $in_else = 0;
927 126 my $depth = 1;
928 126 my $i = $start + 1;
929  
930 126 ▶ while ($i <= $#$tokens) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $tokens->$#*) 390 0 390
931 390 my ($type, $content) = @{$tokens->[$i]};
932 390 ▶ if ($type eq 'tag') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "tag") 194 196 390
933 194 ▶ if ($content =~ /^(?:for|if|block|macro|with)\b/) { $depth++ }
Branch: 3 branches
BranchTrueFalseTotal
if ($content =~ /^(?:for|if|block|macro|with)\b/) { } 25 169 194
elsif ($content =~ /^end(?:for|if|block|macro|with)/) { } 151 18 169
elsif ($depth == 1 and $content eq 'else') { } 10 10 20
934 elsif ($content =~ /^end(?:for|if|block|macro|with)/) {
935 151 $depth--;
936 151 ▶ if ($depth == 0 && $content eq 'endfor') { $i++; last }
Branch: 1 branch
BranchTrueFalseTotal
if ($depth == 0 and $content eq "endfor") 126 25 151
Condition66%2/3
$depth == 0 and $content eq "endfor"
!ll&&!rl&&r
250126
MC/DC50%1/2
$depth == 0 and $content eq "endfor"
$depth == 0 $content eq "endfor"
Truth table66%2/3
A and B
A= $depth == 0B= $content eq "endfor"
ABresult
0X0
100
111
937 }
938 ▶ elsif ($depth == 1 && $content eq 'else') {
Condition100%3/3
$depth == 1 and $content eq "else"
!ll&&!rl&&r
2610
MC/DC100%2/2
$depth == 1 and $content eq "else"
$depth == 1 $content eq "else"
Truth table100%3/3
A and B
A= $depth == 1B= $content eq "else"
ABresult
0X0
100
111
939 10 $in_else = 1; $i++; next;
940 }
941 }
942 254 ▶ if ($in_else) { push @else_tokens, $tokens->[$i] }
Branch: 1 branch
BranchTrueFalseTotal
if ($in_else) { } 10 244 254
943 244 else { push @body_tokens, $tokens->[$i] }
944 254 $i++;
945 }
946  
947 # Evaluate iterable
948 126 my $iter = $self->_eval_expr($iter_expr, $vars);
949 126 my @items;
950 126 ▶ if (ref($iter) eq 'ARRAY') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $iter eq 'ARRAY') { } 115 11 126
elsif (ref $iter eq 'HASH') { } 5 6 11
elsif (defined $iter) { } 5 1 6
951 115 @items = @$iter;
952 }
953 elsif (ref($iter) eq 'HASH') {
954 5 @items = map { [ $_, $iter->{$_} ] } sort keys %$iter;
955 }
956 elsif (defined $iter) {
957 5 @items = ($iter);
958 }
959  
960 # Apply loop filter
961 126 ▶ if (defined $cond_expr) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cond_expr) 12 114 126
962 12 my @filtered;
963 12 for my $item (@items) {
964 39 my %loop_vars = %$vars;
965 39 $self->_assign_loop_var(\%loop_vars, $loop_var, $item);
966 39 ▶ push @filtered, $item if _truthy($self->_eval_expr($cond_expr, \%loop_vars));
Branch: 1 branch
BranchTrueFalseTotal
if _truthy($self->_eval_expr($cond_expr, \%loop_vars)) 22 17 39
967 }
968 12 @items = @filtered;
969 }
970  
971 126 ▶ if (!@items) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@items) 9 117 126
972 9 return ($self->_eval_tokens(\@else_tokens, $vars, 0, $filename), $i);
973 }
974  
975 117 my $total = scalar @items;
976 117 my $result = '';
977  
978 117 for my $idx (0 .. $#items) {
979 325 my %loop_vars = %$vars;
980 325 $self->_assign_loop_var(\%loop_vars, $loop_var, $items[$idx]);
981  
982 # loop special variable
983 $loop_vars{loop} = {
984 index => $idx + 1,
985 index0 => $idx,
986 revindex => $total - $idx,
987 revindex0 => $total - $idx - 1,
988 first => ($idx == 0) ? 1 : 0,
989 last => ($idx == $#items) ? 1 : 0,
990 length => $total,
991 depth => 1,
992 depth0 => 0,
993 odd => ($idx % 2 == 0) ? 0 : 1,
994 even => ($idx % 2 == 0) ? 1 : 0,
995 0 ▶ changed => sub { my $attr = shift; _loop_changed($attr, $items[$idx], $idx > 0 ? $items[$idx-1] : undef) },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$idx > 0 ? : 0 0 0
996 325 ▶ };
Branch: 4 branches
BranchTrueFalseTotal
$idx == 0 ? : 117 208 325
$idx == $#items ? : 117 208 325
$idx % 2 == 0 ? : 201 124 325
$idx % 2 == 0 ? : 201 124 325
997  
998 325 $result .= $self->_eval_tokens(\@body_tokens, \%loop_vars, 0, $filename);
999 }
1000  
1001 117 return ($result, $i);
1002 }
1003  
1004 364 ▶sub _assign_loop_var {
Subroutine _assign_loop_var: called CC=3 SCAR=11.3
1005 364 my ($self, $vars, $loop_var, $item) = @_;
1006 # Tuple unpacking: "k, v" in "for k, v in ..."
1007 364 ▶ if ($loop_var =~ /^(\w+)\s*,\s*(\w+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($loop_var =~ /^(\w+)\s*,\s*(\w+)$/) { } 10 354 364
1008 10 my ($k, $v) = ($1, $2);
1009 10 ▶ if (ref($item) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $item eq 'ARRAY') { } 10 0 10
1010 10 $vars->{$k} = $item->[0];
1011 10 $vars->{$v} = $item->[1];
1012 }
1013 else {
1014 0 $vars->{$k} = $item;
1015 0 $vars->{$v} = undef;
1016 }
1017 }
1018 else {
1019 354 $vars->{$loop_var} = $item;
1020 }
1021 }
1022  
1023 0 ▶sub _loop_changed {
Subroutine _loop_changed: not called CC=7 SCAR=40.3
1024 0 my ($attr, $cur, $prev) = @_;
1025 0 ▶ return 1 unless defined $prev;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prev 0 0 0
1026 0 my $cv = _get_attr($cur, $attr);
1027 0 my $pv = _get_attr($prev, $attr);
1028 0 ▶ return (!defined $cv && !defined $pv) ? 0
Branch: 3 branches
BranchTrueFalseTotal
!(defined $cv || defined $pv) ? : 0 0 0
!(defined $cv && defined $pv) ? : 0 0 0
$cv ne $pv ? : 0 0 0
Condition0%0/6
defined $cv || defined $pv
l!l&&r!l&&!r
000
defined $cv && defined $pv
!ll&&!rl&&r
000
MC/DC0%0/2
defined $cv || defined $pv
defined $cv defined $pv
MC/DC0%0/2
defined $cv && defined $pv
defined $cv defined $pv
Truth table0%0/3
A || B
A= defined $cvB= defined $pv
ABresult
1X1
011
000
Truth table0%0/3
A && B
A= defined $cvB= defined $pv
ABresult
0X0
100
111
1029 : (!defined $cv || !defined $pv) ? 1
1030 : $cv ne $pv ? 1 : 0;
1031 }
1032  
1033 ###############################################################################
1034 # _collect_until - Collect tokens until end tag (depth-aware)
1035 ###############################################################################
1036 13 ▶sub _collect_until {
Subroutine _collect_until: called CC=7 SCAR=19.5
1037 13 my ($self, $tokens, $start, @end_tags) = @_;
1038 13 my %ends = map { $_ => 1 } @end_tags;
1039 13 my @body;
1040 13 my $depth = 1;
1041 13 my $i = $start;
1042  
1043 # Map each opener tag to its closer. We track depth only for
1044 # openers whose closer matches one of the @end_tags we are looking for,
1045 # plus any nested use of the SAME opener.
1046 13 my %opener_to_closer = (
1047 'if' => 'endif',
1048 'for' => 'endfor',
1049 'block' => 'endblock',
1050 'macro' => 'endmacro',
1051 'with' => 'endwith',
1052 'raw' => 'endraw',
1053 'set' => 'endset',
1054 );
1055 13 my %closer_to_opener = reverse %opener_to_closer;
1056  
1057 13 ▶ while ($i <= $#$tokens) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $tokens->$#*) 42 0 42
1058 42 my ($type, $content) = @{$tokens->[$i]};
1059 42 ▶ if ($type eq 'tag') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "tag") 24 18 42
1060 24 my $bare = $content; $bare =~ s/\s.*//;
1061 24 ▶ if (exists $ends{$content}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $ends{$content}) { } 14 10 24
elsif (exists $opener_to_closer{$bare}) { } 6 4 10
1062 14 $depth--;
1063 14 ▶ last if $depth == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $depth == 0 13 1 14
1064 }
1065 elsif (exists $opener_to_closer{$bare}) {
1066 # Push only when the closer of THIS opener is in @end_tags
1067 # (i.e. same kind of nesting as what we're looking for)
1068 6 my $closer = $opener_to_closer{$bare};
1069 6 ▶ if (exists $ends{$closer}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ends{$closer}) 1 5 6
1070 1 $depth++;
1071 }
1072 }
1073 }
1074 29 push @body, $tokens->[$i];
1075 29 $i++;
1076 }
1077 13 return (\@body, $i);
1078 }
1079  
1080 ###############################################################################
1081 # _collect_raw - Collect raw text tokens until endraw
1082 ###############################################################################
1083 0 ▶sub _collect_raw {
Subroutine _collect_raw: not called CC=4 SCAR=30.0
1084 0 my ($self, $tokens, $start) = @_;
1085 0 my $raw = '';
1086 0 my $i = $start;
1087 0 ▶ while ($i <= $#$tokens) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $tokens->$#*) 0 0 0
1088 0 my ($type, $content) = @{$tokens->[$i]};
1089 0 ▶ last if $type eq 'tag' && $content eq 'endraw';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "tag" and $content eq "endraw" 0 0 0
Condition0%0/3
$type eq "tag" and $content eq "endraw"
!ll&&!rl&&r
000
MC/DC0%0/2
$type eq "tag" and $content eq "endraw"
$type eq "tag" $content eq "endraw"
Truth table0%0/3
A and B
A= $type eq "tag"B= $content eq "endraw"
ABresult
0X0
100
111
1090 0 $raw .= $content;
1091 0 $i++;
1092 }
1093 0 return ($raw, $i);
1094 }
1095  
1096 ###############################################################################
1097 # _call_macro - Call a defined macro
1098 ###############################################################################
1099 11 ▶sub _call_macro {
Subroutine _call_macro: called CC=5 SCAR=16.2
1100 11 my ($self, $mname, $argstr, $vars, $filename) = @_;
1101 11 my $macro = $self->{_macros}{$mname};
1102 11 ▶ return '' unless defined $macro;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $macro 0 11 11
1103  
1104 # Parse positional args
1105 11 my @arg_vals = _split_args($argstr);
1106 11 my %macro_vars = %$vars;
1107  
1108 11 my @margs = @{ $macro->{args} };
1109 11 my @mdefaults = @{ $macro->{defaults} };
1110  
1111 11 for my $idx (0 .. $#margs) {
1112 18 ▶ if ($idx < @arg_vals) {
Branch: 2 branches
BranchTrueFalseTotal
if ($idx < @arg_vals) { } 17 1 18
elsif (defined $mdefaults[$idx]) { } 1 0 1
1113 17 $macro_vars{$margs[$idx]} = $self->_eval_expr($arg_vals[$idx], $vars);
1114 }
1115 elsif (defined $mdefaults[$idx]) {
1116 1 $macro_vars{$margs[$idx]} = $self->_eval_expr($mdefaults[$idx], $vars);
1117 }
1118 else {
1119 0 $macro_vars{$margs[$idx]} = undef;
1120 }
1121 }
1122  
1123 # varargs / kwargs accessible as special vars
1124 11 $macro_vars{varargs} = [];
1125 11 $macro_vars{kwargs} = {};
1126  
1127 11 return $self->_eval_template($macro->{body}, { %macro_vars }, $filename);
1128 }
1129  
1130 ###############################################################################
1131 # _eval_expr - Evaluate a Jinja2 expression
1132 ###############################################################################
1133 2755 ▶sub _eval_expr {
Subroutine _eval_expr: called CC=154 SCAR=52.2
1134 2755 my ($self, $expr, $vars) = @_;
1135 2755 $expr =~ s/^\s+|\s+$//g;
1136 2755 ▶ return undef unless defined $expr && $expr ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $expr and $expr ne "" 0 2755 2755
Condition33%1/3
defined $expr and $expr ne ""
!ll&&!rl&&r
002755
MC/DC0%0/2
defined $expr and $expr ne ""
defined $expr $expr ne ""
Truth table33%1/3
A and B
A= defined $exprB= $expr ne ""
ABresult
0X0
100
111
1137  
1138 # Literal: None / True / False
1139 2755 ▶ return undef if $expr eq 'none' || $expr eq 'None' || $expr eq 'undefined';
Branch: 1 branch
BranchTrueFalseTotal
if $expr eq "none" or $expr eq "None" or $expr eq "undefined" 3 2752 2755
Condition83%5/6
$expr eq "none" or $expr eq "None" or $expr eq "undefined"
l!l&&r!l&&!r
302752
$expr eq "none" or $expr eq "None"
l!l&&r!l&&!r
212752
MC/DC66%2/3
$expr eq "none" or $expr eq "None" or $expr eq "undefined"
$expr eq "none" $expr eq "None" $expr eq "undefined"
Truth table75%3/4
A or B or C
A= $expr eq "none"B= $expr eq "None"C= $expr eq "undefined"
ABCresult
1XX1
01X1
0011
0000
1140 2752 ▶ return 1 if $expr eq 'true' || $expr eq 'True';
Branch: 1 branch
BranchTrueFalseTotal
if $expr eq "true" or $expr eq "True" 3 2749 2752
Condition66%2/3
$expr eq "true" or $expr eq "True"
l!l&&r!l&&!r
302749
MC/DC50%1/2
$expr eq "true" or $expr eq "True"
$expr eq "true" $expr eq "True"
Truth table66%2/3
A or B
A= $expr eq "true"B= $expr eq "True"
ABresult
1X1
011
000
1141 2749 ▶ return 0 if $expr eq 'false' || $expr eq 'False';
Branch: 1 branch
BranchTrueFalseTotal
if $expr eq "false" or $expr eq "False" 4 2745 2749
Condition66%2/3
$expr eq "false" or $expr eq "False"
l!l&&r!l&&!r
402745
MC/DC50%1/2
$expr eq "false" or $expr eq "False"
$expr eq "false" $expr eq "False"
Truth table66%2/3
A or B
A= $expr eq "false"B= $expr eq "False"
ABresult
1X1
011
000
1142  
1143 # Literal: integer / float
1144 2745 ▶ return $expr + 0 if $expr =~ /^-?\d+(\.\d+)?$/;
Branch: 1 branch
BranchTrueFalseTotal
if $expr =~ /^-?\d+(\.\d+)?$/ 327 2418 2745
1145  
1146 # Unary minus: -expr (where expr is not a literal number)
1147 2418 ▶ if ($expr =~ /^-(.+)$/ && $1 !~ /^\d/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^-(.+)$/ and not $1 =~ /^\d/) 1 2417 2418
Condition100%3/3
$expr =~ /^-(.+)$/ and not $1 =~ /^\d/
!ll&&!rl&&r
241431
MC/DC100%2/2
$expr =~ /^-(.+)$/ and not $1 =~ /^\d/
$expr =~ /^-(.+)$/ not $1 =~ /^\d/
Truth table100%3/3
A and not(B)
A= $expr =~ /^-(.+)$/B= not $1 =~ /^\d/
ABresult
0X0
100
111
1148 1 my $val = $self->_eval_expr($1, $vars);
1149 1 ▶ return defined $val ? -$val : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $val ? : 1 0 1
1150 }
1151  
1152 # Literal: string
1153 2417 ▶ if ($expr =~ /^"((?:[^"\\]|\\.)*)"$/ || $expr =~ /^'((?:[^'\\]|\\.)*)'$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^"((?:[^"\\]|\\.)*)"$/ or $expr =~ /^'((?:[^'\\]|\\.)*)'$/) 213 2204 2417
Condition100%3/3
$expr =~ /^"((?:[^"\\]|\\.)*)"$/ or $expr =~ /^'((?:[^'\\]|\\.)*)'$/
l!l&&r!l&&!r
20852204
MC/DC100%2/2
$expr =~ /^"((?:[^"\\]|\\.)*)"$/ or $expr =~ /^'((?:[^'\\]|\\.)*)'$/
$expr =~ /^"((?:[^"\\]|\\.)*)"$/ $expr =~ /^'((?:[^'\\]|\\.)*)'$/
Truth table100%3/3
A or B
A= $expr =~ /^"((?:[^"\\]|\\.)*)"$/B= $expr =~ /^'((?:[^'\\]|\\.)*)'$/
ABresult
1X1
011
000
1154 213 my $s = $1;
1155 213 $s =~ s/\\n/\n/g; $s =~ s/\\t/\t/g; $s =~ s/\\r/\r/g;
1156 213 $s =~ s/\\(.)/$1/g;
1157 213 return $s;
1158 }
1159  
1160 # Literal: [] list
1161 2204 ▶ if ($expr =~ /^\[(.+)\]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^\[(.+)\]$/) 7 2197 2204
1162 7 my @elems = _split_args($1);
1163 7 return [ map { $self->_eval_expr($_, $vars) } @elems ];
1164 }
1165  
1166 # Literal: {} dict
1167 2197 ▶ if ($expr =~ /^\{(.+)\}$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^\{(.+)\}$/) 1 2196 2197
1168 1 return $self->_eval_dict($1, $vars);
1169 }
1170  
1171 # Literal: range() -- only when no pipe follows (e.g. range(3) but not range(3)|join)
1172 2196 ▶ if ($expr =~ /^range\(([^|]*)\)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^range\(([^|]*)\)$/) 22 2174 2196
1173 22 return $self->_eval_range($1, $vars);
1174 }
1175  
1176 # Conditional expression: a if cond else b
1177 2174 ▶ if ($expr =~ /^(.+?)\s+if\s+(.+?)\s+else\s+(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^(.+?)\s+if\s+(.+?)\s+else\s+(.+)$/) 34 2140 2174
1178 34 my ($a, $cond, $b) = ($1, $2, $3);
1179 34 ▶ return _truthy($self->_eval_expr($cond, $vars)) ? $self->_eval_expr($a, $vars) : $self->_eval_expr($b, $vars);
Branch: 1 branch
BranchTrueFalseTotal
_truthy($self->_eval_expr($cond, $vars)) ? : 21 13 34
1180 }
1181  
1182 # 'not' operator
1183 2140 ▶ if ($expr =~ /^not\s+(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^not\s+(.+)$/) 69 2071 2140
1184 69 ▶ return _truthy($self->_eval_expr($1, $vars)) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
_truthy($self->_eval_expr($1, $vars)) ? : 14 55 69
1185 }
1186  
1187 # 'in' / 'not in' operator
1188 2071 ▶ if ($expr =~ /^(.+?)\s+not\s+in\s+(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^(.+?)\s+not\s+in\s+(.+)$/) 4 2067 2071
1189 4 ▶ return $TESTS{in}->($self->_eval_expr($1, $vars), $self->_eval_expr($2, $vars)) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$TESTS{'in'}->($self->_eval_expr($1, $vars), $self->_eval_expr($2, $vars)) ? : 2 2 4
1190 }
1191 2067 ▶ if ($expr =~ /^(.+?)\s+in\s+(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^(.+?)\s+in\s+(.+)$/) 19 2048 2067
1192 19 return $TESTS{in}->($self->_eval_expr($1, $vars), $self->_eval_expr($2, $vars));
1193 }
1194  
1195 # Boolean operators: and / or
1196 2048 ▶ if ($expr =~ /^(.+?)\s+or\s+(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^(.+?)\s+or\s+(.+)$/) 14 2034 2048
1197 14 my $a = $self->_eval_expr($1, $vars);
1198 14 ▶ return $a if _truthy($a);
Branch: 1 branch
BranchTrueFalseTotal
if _truthy($a) 4 10 14
1199 10 return $self->_eval_expr($2, $vars);
1200 }
1201 2034 ▶ if ($expr =~ /^(.+?)\s+and\s+(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^(.+?)\s+and\s+(.+)$/) 18 2016 2034
1202 18 my $a = $self->_eval_expr($1, $vars);
1203 18 ▶ return _truthy($a) ? $self->_eval_expr($2, $vars) : $a;
Branch: 1 branch
BranchTrueFalseTotal
_truthy($a) ? : 11 7 18
1204 }
1205  
1206 # Filter pipeline: expr | filter1 | filter2(arg)
1207 # Must be checked before comparison operators so that ">" inside
1208 # filter arg strings (e.g. replace("-->","x")) is not misread.
1209 2016 ▶ if ($expr =~ /^(.+?)\s*\|\s*(\w+)(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^(.+?)\s*\|\s*(\w+)(.*)$/) 241 1775 2016
1210 241 my ($lhs, $fname, $rest) = ($1, $2, $3);
1211 241 my $val = $self->_eval_expr($lhs, $vars);
1212  
1213 241 my $fargs_str = '';
1214 241 my $remaining = '';
1215 241 ▶ if ($rest =~ /^\s*\(/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rest =~ /^\s*\(/) { } 102 139 241
elsif ($rest =~ /^\s*(\|.+)$/) { } 35 104 139
1216 # Find matching closing paren, respecting quotes and nesting
1217 102 my $depth2 = 0; my $in_sq2 = 0; my $in_dq2 = 0;
1218 102 my $found2 = -1;
1219 102 my $rs = $rest; $rs =~ s/^\s*//;
1220 102 for my $ci (0 .. length($rs) - 1) {
1221 577 my $c = substr($rs, $ci, 1);
1222 577 ▶ if (!$in_sq2 && !$in_dq2 && $c eq "'") { $in_sq2 = 1; next }
Branch: 1 branch
BranchTrueFalseTotal
if (not $in_sq2 || $in_dq2 and $c eq "'") 4 573 577
Condition100%6/6
not $in_sq2 || $in_dq2 and $c eq "'"
!ll&&!rl&&r
2513224
$in_sq2 || $in_dq2
l!l&&r!l&&!r
20231326
MC/DC100%3/3
not $in_sq2 || $in_dq2 and $c eq "'"
$in_sq2 $in_dq2 $c eq "'"
Truth table100%4/4
not(A || B) and C
A= $in_sq2B= $in_dq2C= $c eq "'"
ABCresult
1XX0
01X0
0000
0011
1223 573 ▶ if ($in_sq2 && $c eq "'") { $in_sq2 = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($in_sq2 and $c eq "'") 4 569 573
Condition100%3/3
$in_sq2 and $c eq "'"
!ll&&!rl&&r
553164
MC/DC100%2/2
$in_sq2 and $c eq "'"
$in_sq2 $c eq "'"
Truth table100%3/3
A and B
A= $in_sq2B= $c eq "'"
ABresult
0X0
100
111
1224 569 ▶ if (!$in_sq2 && !$in_dq2 && $c eq '"') { $in_dq2 = 1; next }
Branch: 1 branch
BranchTrueFalseTotal
if (not $in_sq2 || $in_dq2 and $c eq "\"") 82 487 569
Condition100%6/6
not $in_sq2 || $in_dq2 and $c eq "\""
!ll&&!rl&&r
24724082
$in_sq2 || $in_dq2
l!l&&r!l&&!r
16231322
MC/DC100%3/3
not $in_sq2 || $in_dq2 and $c eq "\""
$in_sq2 $in_dq2 $c eq "\""
Truth table100%4/4
not(A || B) and C
A= $in_sq2B= $in_dq2C= $c eq "\""
ABCresult
1XX0
01X0
0000
0011
1225 487 ▶ if ($in_dq2 && $c eq '"') { $in_dq2 = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($in_dq2 and $c eq "\"") 82 405 487
Condition100%3/3
$in_dq2 and $c eq "\""
!ll&&!rl&&r
25614982
MC/DC100%2/2
$in_dq2 and $c eq "\""
$in_dq2 $c eq "\""
Truth table100%3/3
A and B
A= $in_dq2B= $c eq "\""
ABresult
0X0
100
111
1226 405 ▶ if (!$in_sq2 && !$in_dq2) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in_sq2 or $in_dq2) 240 165 405
Condition100%3/3
$in_sq2 or $in_dq2
l!l&&r!l&&!r
16149240
MC/DC100%2/2
$in_sq2 or $in_dq2
$in_sq2 $in_dq2
Truth table100%3/3
A or B
A= $in_sq2B= $in_dq2
ABresult
1X1
011
000
1227 240 ▶ if ($c eq '(') { $depth2++ }
Branch: 2 branches
BranchTrueFalseTotal
if ($c eq '(') { } 102 138 240
elsif ($c eq ')') { } 102 36 138
1228 102 ▶ elsif ($c eq ')') { $depth2--; if ($depth2 == 0) { $found2 = $ci; last } }
Branch: 1 branch
BranchTrueFalseTotal
if ($depth2 == 0) 102 0 102
1229 }
1230 }
1231 102 ▶ if ($found2 >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found2 >= 0) 102 0 102
1232 102 $fargs_str = substr($rs, 1, $found2 - 1);
1233 102 $remaining = substr($rs, $found2 + 1);
1234 }
1235 }
1236 elsif ($rest =~ /^\s*(\|.+)$/) {
1237 35 $remaining = $1;
1238 }
1239  
1240 241 my @fargs;
1241 241 ▶ if ($fargs_str ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($fargs_str ne "") 102 139 241
1242 102 @fargs = map { $self->_eval_expr($_, $vars) } _split_args($fargs_str);
1243 }
1244  
1245 241 my $fn = $self->{_filters}{$fname};
1246 241 ▶ $val = $fn ? $fn->($val, @fargs) : $val;
Branch: 1 branch
BranchTrueFalseTotal
$fn ? : 241 0 241
1247  
1248 # Continue pipeline
1249 241 ▶ if ($remaining =~ s/^\s*\|\s*//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remaining =~ s/^\s*\|\s*//) 49 192 241
1250 49 return $self->_eval_expr("__PIPEVAL__ | $remaining", { %$vars, '__PIPEVAL__' => $val });
1251 }
1252 192 return $val;
1253 }
1254  
1255 # Comparison operators
1256 1775 ▶ if ($expr =~ /^(.+?)\s*(==|!=|>=|<=|>|<)\s*(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^(.+?)\s*(==|!=|>=|<=|>|<)\s*(.+)$/) 114 1661 1775
1257 114 my ($lhs, $op, $rhs) = ($1, $2, $3);
1258 114 my $l = $self->_eval_expr($lhs, $vars);
1259 114 my $r = $self->_eval_expr($rhs, $vars);
1260 114 ▶ return 0 unless defined $l && defined $r;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $l and defined $r 0 114 114
Condition33%1/3
defined $l and defined $r
!ll&&!rl&&r
00114
MC/DC0%0/2
defined $l and defined $r
defined $l defined $r
Truth table33%1/3
A and B
A= defined $lB= defined $r
ABresult
0X0
100
111
1261 114 ▶ my $numeric = ($l =~ /^-?(?:\d+\.?\d*|\.\d+)$/ && $r =~ /^-?(?:\d+\.?\d*|\.\d+)$/);
Condition66%2/3
$l =~ /^-?(?:\d+\.?\d*|\.\d+)$/ && $r =~ /^-?(?:\d+\.?\d*|\.\d+)$/
!ll&&!rl&&r
20094
MC/DC50%1/2
$l =~ /^-?(?:\d+\.?\d*|\.\d+)$/ && $r =~ /^-?(?:\d+\.?\d*|\.\d+)$/
$l =~ /^-?(?:\d+\.?\d*|\.\d+)$/ $r =~ /^-?(?:\d+\.?\d*|\.\d+)$/
Truth table66%2/3
A && B
A= $l =~ /^-?(?:\d+\.?\d*|\.\d+)$/B= $r =~ /^-?(?:\d+\.?\d*|\.\d+)$/
ABresult
0X0
100
111
1262 114 ▶ if ($op eq '==') { return $numeric ? ($l == $r ? 1 : 0) : ($l eq $r ? 1 : 0) }
Branch: 4 branches
BranchTrueFalseTotal
if ($op eq "==") 23 91 114
$numeric ? : 15 8 23
$l == $r ? : 7 8 15
$l eq $r ? : 4 4 8
1263 91 ▶ if ($op eq '!=') { return $numeric ? ($l != $r ? 1 : 0) : ($l ne $r ? 1 : 0) }
Branch: 4 branches
BranchTrueFalseTotal
if ($op eq "!=") 15 76 91
$numeric ? : 6 9 15
$l != $r ? : 2 4 6
$l ne $r ? : 6 3 9
1264 # 0.02: ordering compares numbers as numbers and anything else
1265 # as strings (Jinja2). 0.01 always compared numerically, so
1266 # '2026-09-30' < '2026-10-01' was false (both became 2026) and
1267 # warned "isn't numeric".
1268 76 ▶ if ($op eq '>') { return ($numeric ? $l > $r : $l gt $r) ? 1 : 0 }
Branch: 3 branches
BranchTrueFalseTotal
if ($op eq ">") 43 33 76
($numeric ? $l > $r : $l gt $r) ? : 23 20 43
$numeric ? : 42 1 43
1269 33 ▶ if ($op eq '<') { return ($numeric ? $l < $r : $l lt $r) ? 1 : 0 }
Branch: 3 branches
BranchTrueFalseTotal
if ($op eq "<") 12 21 33
($numeric ? $l < $r : $l lt $r) ? : 6 6 12
$numeric ? : 10 2 12
1270 21 ▶ if ($op eq '>=') { return ($numeric ? $l >= $r : $l ge $r) ? 1 : 0 }
Branch: 3 branches
BranchTrueFalseTotal
if ($op eq ">=") 13 8 21
($numeric ? $l >= $r : $l ge $r) ? : 5 8 13
$numeric ? : 13 0 13
1271 8 ▶ if ($op eq '<=') { return ($numeric ? $l <= $r : $l le $r) ? 1 : 0 }
Branch: 3 branches
BranchTrueFalseTotal
if ($op eq "<=") 8 0 8
($numeric ? $l <= $r : $l le $r) ? : 4 4 8
$numeric ? : 8 0 8
1272 }
1273  
1274 # Arithmetic: match ** and // before single-char operators
1275 1661 ▶ if ($expr =~ /^(.+?)\s*(\*\*|\/\/)\s*(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ m[^(.+?)\s*(\*\*|//)\s*(.+)$]) 4 1657 1661
1276 4 my ($lhs, $op, $rhs) = ($1, $2, $3);
1277 4 my $l = $self->_eval_expr($lhs, $vars);
1278 4 my $r = $self->_eval_expr($rhs, $vars);
1279 4 ▶ if (defined $l && defined $r) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $l and defined $r) 4 0 4
Condition33%1/3
defined $l and defined $r
!ll&&!rl&&r
004
MC/DC0%0/2
defined $l and defined $r
defined $l defined $r
Truth table33%1/3
A and B
A= defined $lB= defined $r
ABresult
0X0
100
111
1280 4 ▶ if ($op eq '//') { return $r != 0 ? int($l / $r) : undef }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq "//") 3 1 4
$r != 0 ? : 3 0 3
1281 1 ▶ if ($op eq '**') { return $l ** $r }
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "**") 1 0 1
1282 }
1283 0 return undef;
1284 }
1285  
1286 # Arithmetic: scan for operator at depth-0 (respects parentheses)
1287 {
1288 1657 my $depth3 = 0; my $in_sq3 = 0; my $in_dq3 = 0;
1289 1657 my $op_pos = -1; my $op_char = '';
1290 1657 for my $ci (0 .. length($expr) - 1) {
1291 6639 my $c = substr($expr, $ci, 1);
1292 6639 ▶ if (!$in_sq3 && !$in_dq3 && $c eq "'") { $in_sq3 = 1; next }
Branch: 1 branch
BranchTrueFalseTotal
if (not $in_sq3 || $in_dq3 and $c eq "'") 2 6637 6639
Condition100%6/6
not $in_sq3 || $in_dq3 and $c eq "'"
!ll&&!rl&&r
7265652
$in_sq3 || $in_dq3
l!l&&r!l&&!r
7656567
MC/DC100%3/3
not $in_sq3 || $in_dq3 and $c eq "'"
$in_sq3 $in_dq3 $c eq "'"
Truth table100%4/4
not(A || B) and C
A= $in_sq3B= $in_dq3C= $c eq "'"
ABCresult
1XX0
01X0
0000
0011
1293 6637 ▶ if ($in_sq3 && $c eq "'") { $in_sq3 = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($in_sq3 and $c eq "'") 2 6635 6637
Condition100%3/3
$in_sq3 and $c eq "'"
!ll&&!rl&&r
663052
MC/DC100%2/2
$in_sq3 and $c eq "'"
$in_sq3 $c eq "'"
Truth table100%3/3
A and B
A= $in_sq3B= $c eq "'"
ABresult
0X0
100
111
1294 6635 ▶ if (!$in_sq3 && !$in_dq3 && $c eq '"') { $in_dq3 = 1; next }
Branch: 1 branch
BranchTrueFalseTotal
if (not $in_sq3 || $in_dq3 and $c eq "\"") 26 6609 6635
Condition100%6/6
not $in_sq3 || $in_dq3 and $c eq "\""
!ll&&!rl&&r
70653926
$in_sq3 || $in_dq3
l!l&&r!l&&!r
5656565
MC/DC100%3/3
not $in_sq3 || $in_dq3 and $c eq "\""
$in_sq3 $in_dq3 $c eq "\""
Truth table100%4/4
not(A || B) and C
A= $in_sq3B= $in_dq3C= $c eq "\""
ABCresult
1XX0
01X0
0000
0011
1295 6609 ▶ if ($in_dq3 && $c eq '"') { $in_dq3 = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($in_dq3 and $c eq "\"") 26 6583 6609
Condition100%3/3
$in_dq3 and $c eq "\""
!ll&&!rl&&r
65443926
MC/DC100%2/2
$in_dq3 and $c eq "\""
$in_dq3 $c eq "\""
Truth table100%3/3
A and B
A= $in_dq3B= $c eq "\""
ABresult
0X0
100
111
1296 6583 ▶ next if $in_sq3 || $in_dq3;
Branch: 1 branch
BranchTrueFalseTotal
if $in_sq3 or $in_dq3 44 6539 6583
Condition100%3/3
$in_sq3 or $in_dq3
l!l&&r!l&&!r
5396539
MC/DC100%2/2
$in_sq3 or $in_dq3
$in_sq3 $in_dq3
Truth table100%3/3
A or B
A= $in_sq3B= $in_dq3
ABresult
1X1
011
000
1297 6539 ▶ if ($c eq '(' || $c eq '[' || $c eq '{') { $depth3++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "(" or $c eq "[" or $c eq "{") 82 6457 6539
Condition100%6/6
$c eq "(" or $c eq "[" or $c eq "{"
l!l&&r!l&&!r
8116457
$c eq "(" or $c eq "["
l!l&&r!l&&!r
55266458
MC/DC100%3/3
$c eq "(" or $c eq "[" or $c eq "{"
$c eq "(" $c eq "[" $c eq "{"
Truth table100%4/4
A or B or C
A= $c eq "("B= $c eq "["C= $c eq "{"
ABCresult
1XX1
01X1
0011
0000
1298 6457 ▶ if ($c eq ')' || $c eq ']' || $c eq '}') { $depth3--; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq ")" or $c eq "]" or $c eq "}") 78 6379 6457
Condition100%6/6
$c eq ")" or $c eq "]" or $c eq "}"
l!l&&r!l&&!r
7716379
$c eq ")" or $c eq "]"
l!l&&r!l&&!r
51266380
MC/DC100%3/3
$c eq ")" or $c eq "]" or $c eq "}"
$c eq ")" $c eq "]" $c eq "}"
Truth table100%4/4
A or B or C
A= $c eq ")"B= $c eq "]"C= $c eq "}"
ABCresult
1XX1
01X1
0011
0000
1299 6379 ▶ next if $depth3 != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $depth3 != 0 499 5880 6379
1300 5880 ▶ if ($c =~ /^[+\-*\/%~]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c =~ m[^[+\-*/%~]$]) 59 5821 5880
1301 59 ▶ next if $ci == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $ci == 0 1 58 59
1302 58 my $prev = substr($expr, $ci - 1, 1);
1303 58 ▶ next if $prev =~ /^[+\-*\/%~(]$/ || $prev eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $prev =~ m[^[+\-*/%~(]$] or $prev eq "" 0 58 58
Condition33%1/3
$prev =~ m[^[+\-*/%~(]$] or $prev eq ""
l!l&&r!l&&!r
0058
MC/DC0%0/2
$prev =~ m[^[+\-*/%~(]$] or $prev eq ""
$prev =~ m[^[+\-*/%~(]$] $prev eq ""
Truth table33%1/3
A or B
A= $prev =~ m[^[+\-*/%~(]$]B= $prev eq ""
ABresult
1X1
011
000
1304 58 ▶ next if $c eq '*' && $ci + 1 < length($expr) && substr($expr, $ci + 1, 1) eq '*';
Branch: 1 branch
BranchTrueFalseTotal
if $c eq "*" and $ci + 1 < length $expr and substr($expr, $ci + 1, 1) eq "*" 0 58 58
Condition66%4/6
$c eq "*" and $ci + 1 < length $expr and substr($expr, $ci + 1, 1) eq "*"
!ll&&!rl&&r
46120
$c eq "*" and $ci + 1 < length $expr
!ll&&!rl&&r
46012
MC/DC0%0/3
$c eq "*" and $ci + 1 < length $expr and substr($expr, $ci + 1, 1) eq "*"
$c eq "*" $ci + 1 < length $expr substr($expr, $ci + 1, 1) eq "*"
Truth table50%2/4
A and B and C
A= $c eq "*"B= $ci + 1 < length $exprC= substr($expr, $ci + 1, 1) eq "*"
ABCresult
0XX0
10X0
1100
1111
1305 58 ▶ next if $c eq '/' && $ci + 1 < length($expr) && substr($expr, $ci + 1, 1) eq '/';
Branch: 1 branch
BranchTrueFalseTotal
if $c eq "/" and $ci + 1 < length $expr and substr($expr, $ci + 1, 1) eq "/" 0 58 58
Condition66%4/6
$c eq "/" and $ci + 1 < length $expr and substr($expr, $ci + 1, 1) eq "/"
!ll&&!rl&&r
5710
$c eq "/" and $ci + 1 < length $expr
!ll&&!rl&&r
5701
MC/DC0%0/3
$c eq "/" and $ci + 1 < length $expr and substr($expr, $ci + 1, 1) eq "/"
$c eq "/" $ci + 1 < length $expr substr($expr, $ci + 1, 1) eq "/"
Truth table50%2/4
A and B and C
A= $c eq "/"B= $ci + 1 < length $exprC= substr($expr, $ci + 1, 1) eq "/"
ABCresult
0XX0
10X0
1100
1111
1306 58 $op_pos = $ci; $op_char = $c;
1307 58 last;
1308 }
1309 }
1310 1657 ▶ if ($op_pos > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op_pos > 0) 58 1599 1657
1311 58 my $lhs = substr($expr, 0, $op_pos);
1312 58 my $rhs = substr($expr, $op_pos + 1);
1313 58 $lhs =~ s/\s+$//; $rhs =~ s/^\s+//;
1314 58 my $l = $self->_eval_expr($lhs, $vars);
1315 58 my $r = $self->_eval_expr($rhs, $vars);
1316 58 ▶ if (defined $l && defined $r) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $l and defined $r) 58 0 58
Condition33%1/3
defined $l and defined $r
!ll&&!rl&&r
0058
MC/DC0%0/2
defined $l and defined $r
defined $l defined $r
Truth table33%1/3
A and B
A= defined $lB= defined $r
ABresult
0X0
100
111
1317 58 ▶ if ($op_char eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($op_char eq "+") 26 32 58
1318 26 ▶ return ($l =~ /^-?\d/ && $r =~ /^-?\d/) ? $l + $r : $l . $r;
Branch: 1 branch
BranchTrueFalseTotal
$l =~ /^-?\d/ && $r =~ /^-?\d/ ? : 26 0 26
Condition33%1/3
$l =~ /^-?\d/ and $r =~ /^-?\d/
!ll&&!rl&&r
0026
MC/DC0%0/2
$l =~ /^-?\d/ and $r =~ /^-?\d/
$l =~ /^-?\d/ $r =~ /^-?\d/
Truth table33%1/3
A and B
A= $l =~ /^-?\d/B= $r =~ /^-?\d/
ABresult
0X0
100
111
1319 }
1320 32 ▶ if ($op_char eq '~') { return $l . $r }
Branch: 1 branch
BranchTrueFalseTotal
if ($op_char eq "~") 16 16 32
1321 16 ▶ if ($op_char eq '-') { return $l - $r }
Branch: 1 branch
BranchTrueFalseTotal
if ($op_char eq "-") 2 14 16
1322 14 ▶ if ($op_char eq '*') { return $l * $r }
Branch: 1 branch
BranchTrueFalseTotal
if ($op_char eq "*") 12 2 14
1323 2 ▶ if ($op_char eq '/') { return $r != 0 ? $l / $r : undef }
Branch: 2 branches
BranchTrueFalseTotal
if ($op_char eq "/") 1 1 2
$r != 0 ? : 1 0 1
1324 1 ▶ if ($op_char eq '%') { return $r != 0 ? $l % $r : undef }
Branch: 2 branches
BranchTrueFalseTotal
if ($op_char eq "%") 1 0 1
$r != 0 ? : 1 0 1
1325 }
1326 }
1327 }
1328  
1329 # Parenthesised expression -- must be before is/is not so that
1330 # (expr is test) does not mis-parse the leading "(" as part of lhs.
1331 1599 ▶ if ($expr =~ /^\((.+)\)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^\((.+)\)$/) 35 1564 1599
1332 35 return $self->_eval_expr($1, $vars);
1333 }
1334  
1335 # is / is not test
1336 1564 ▶ if ($expr =~ /^(.+?)\s+is\s+not\b\s+(\w+)(?:\s+(.+))?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^(.+?)\s+is\s+not\b\s+(\w+)(?:\s+(.+))?$/) 9 1555 1564
1337 9 my ($lhs, $test, $arg) = ($1, $2, $3);
1338 9 my $val = $self->_eval_expr($lhs, $vars);
1339 9 ▶ my $targ = defined $arg ? $self->_eval_expr($arg, $vars) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $arg ? : 0 9 9
1340 9 my $fn = $self->{_tests}{$test};
1341 9 ▶ return $fn ? ($fn->($val, $targ) ? 0 : 1) : 1;
Branch: 2 branches
BranchTrueFalseTotal
$fn ? : 9 0 9
&$fn($val, $targ) ? : 2 7 9
1342 }
1343 1555 ▶ if ($expr =~ /^(.+?)\s+is\s+(\w+)(?:\s+(.+))?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^(.+?)\s+is\s+(\w+)(?:\s+(.+))?$/) 134 1421 1555
1344 134 my ($lhs, $test, $arg) = ($1, $2, $3);
1345 134 my $val = $self->_eval_expr($lhs, $vars);
1346 134 ▶ my $targ = defined $arg ? $self->_eval_expr($arg, $vars) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $arg ? : 42 92 134
1347 134 my $fn = $self->{_tests}{$test};
1348 134 ▶ return $fn ? ($fn->($val, $targ) ? 1 : 0) : 0;
Branch: 2 branches
BranchTrueFalseTotal
$fn ? : 134 0 134
&$fn($val, $targ) ? : 63 71 134
1349 }
1350  
1351 # Attribute access: obj.attr or obj["key"] or obj['key']
1352 1421 ▶ if ($expr =~ /^(.+?)\.(\w+)(?:\(([^)]*)\))?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^(.+?)\.(\w+)(?:\(([^)]*)\))?$/) 159 1262 1421
1353 159 my ($obj_expr, $attr, $call_args) = ($1, $2, $3);
1354 159 my $obj = $self->_eval_expr($obj_expr, $vars);
1355 159 ▶ if (defined $call_args) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $call_args) 0 159 159
1356 # Method call (filters on object)
1357 0 my $fn = $self->{_filters}{$attr};
1358 0 my @args = map { $self->_eval_expr($_, $vars) } _split_args($call_args);
1359 0 ▶ return $fn ? $fn->($obj, @args) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$fn ? : 0 0 0
1360 }
1361 159 return _get_attr($obj, $attr);
1362 }
1363  
1364 1262 ▶ if ($expr =~ /^(.+?)\[["'](\w+)["']\]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^(.+?)\[["'](\w+)["']\]$/) 5 1257 1262
1365 5 my ($obj_expr, $key) = ($1, $2);
1366 5 my $obj = $self->_eval_expr($obj_expr, $vars);
1367 5 return _get_attr($obj, $key);
1368 }
1369  
1370 1257 ▶ if ($expr =~ /^(.+?)\[(-?\d+)\]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^(.+?)\[(-?\d+)\]$/) 12 1245 1257
1371 12 my ($obj_expr, $idx) = ($1, $2);
1372 12 my $obj = $self->_eval_expr($obj_expr, $vars);
1373 12 ▶ return ref($obj) eq 'ARRAY' ? $obj->[$idx] : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $obj eq 'ARRAY' ? : 12 0 12
1374 }
1375  
1376 # Slice: list[start:end]
1377 1245 ▶ if ($expr =~ /^(.+?)\[(-?\d*):(-?\d*)\]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^(.+?)\[(-?\d*):(-?\d*)\]$/) 5 1240 1245
1378 5 my ($obj_expr, $s, $e) = ($1, $2, $3);
1379 5 my $obj = $self->_eval_expr($obj_expr, $vars);
1380 5 ▶ return undef unless ref($obj) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj eq "ARRAY" 0 5 5
1381 5 my $len = scalar @$obj;
1382 5 ▶ my $si = ($s ne '') ? int($s) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$s ne '' ? : 4 1 5
1383 5 ▶ my $ei = ($e ne '') ? int($e) : $len;
Branch: 1 branch
BranchTrueFalseTotal
$e ne '' ? : 4 1 5
1384 5 ▶ $si += $len if $si < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $si < 0 0 5 5
1385 5 ▶ $ei += $len if $ei < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $ei < 0 1 4 5
1386 5 ▶ $si = 0 if $si < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $si < 0 0 5 5
1387 5 ▶ $ei = $len if $ei > $len;
Branch: 1 branch
BranchTrueFalseTotal
if $ei > $len 0 5 5
1388 5 return [ @{$obj}[$si .. $ei - 1] ];
1389 }
1390  
1391 # Function/macro call: name(args)
1392 1240 ▶ if ($expr =~ /^(\w+)\s*\(([^)]*)\)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^(\w+)\s*\(([^)]*)\)$/) 11 1229 1240
1393 11 my ($fname, $argstr) = ($1, $2);
1394 # Macro call
1395 11 ▶ if (exists $self->{_macros}{$fname}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_macros"}{$fname}) 11 0 11
1396 11 return $self->_call_macro($fname, $argstr, $vars, '<expr>');
1397 }
1398 # Built-in functions
1399 0 ▶ if ($fname eq 'range') {
Branch: 1 branch
BranchTrueFalseTotal
if ($fname eq "range") 0 0 0
1400 0 return $self->_eval_range($argstr, $vars);
1401 }
1402 }
1403  
1404 # Variable lookup
1405 1229 ▶ if ($expr =~ /^(\w+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^(\w+)$/) 1219 10 1229
1406 1219 ▶ return exists $vars->{$expr} ? $vars->{$expr} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $vars->{$expr} ? : 1191 28 1219
1407 }
1408  
1409 10 return undef;
1410 }
1411  
1412 ###############################################################################
1413 # _eval_dict - Parse and evaluate a dict literal { key: val, ... }
1414 ###############################################################################
1415 1 ▶sub _eval_dict {
Subroutine _eval_dict: called CC=4 SCAR=14.0
1416 1 my ($self, $inner, $vars) = @_;
1417 1 my %h;
1418 # Simple k:v split (no nested dicts)
1419 1 for my $pair (_split_args($inner)) {
1420 1 ▶ if ($pair =~ /^(.+?)\s*:\s*(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pair =~ /^(.+?)\s*:\s*(.+)$/) 1 0 1
1421 1 my $k = $self->_eval_expr($1, $vars);
1422 1 my $v = $self->_eval_expr($2, $vars);
1423 1 ▶ $h{$k} = $v if defined $k;
Branch: 1 branch
BranchTrueFalseTotal
if defined $k 1 0 1
1424 }
1425 }
1426 1 return { %h };
1427 }
1428  
1429 ###############################################################################
1430 # _eval_range - Evaluate range(stop) / range(start, stop[, step])
1431 ###############################################################################
1432 22 ▶sub _eval_range {
Subroutine _eval_range: called CC=8 SCAR=20.8
1433 22 my ($self, $args_str, $vars) = @_;
1434 22 my @args = map { $self->_eval_expr($_, $vars) } _split_args($args_str);
1435 22 my ($start, $stop, $step);
1436 22 ▶ if (@args == 1) { ($start, $stop, $step) = (0, int($args[0]), 1) }
Branch: 2 branches
BranchTrueFalseTotal
if (@args == 1) { } 14 8 22
elsif (@args == 2) { } 4 4 8
1437 4 elsif (@args == 2) { ($start, $stop, $step) = (int($args[0]), int($args[1]), 1) }
1438 4 ▶ else { ($start, $stop, $step) = (int($args[0]), int($args[1]), int($args[2] || 1)) }
Condition50%1/2
$args[2] || 1
l!l
40
MC/DC0%0/1
$args[2] || 1
$args[2]
Truth table50%1/2
A
A= $args[2]
Aresult
11
00
1439 22 ▶ $step = 1 if $step == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $step == 0 0 22 22
1440 22 my @result;
1441 22 ▶ if ($step > 0) { for (my $n = $start; $n < $stop; $n += $step) { push @result, $n } }
Branch: 2 branches
BranchTrueFalseTotal
if ($step > 0) { } 20 2 22
if ($n < $stop) 56 20 76
1442 2 ▶ else { for (my $n = $start; $n > $stop; $n += $step) { push @result, $n } }
Branch: 1 branch
BranchTrueFalseTotal
if ($n > $stop) 10 2 12
1443 22 return [ @result ];
1444 }
1445  
1446 ###############################################################################
1447 # _get_attr - Get attribute from hash or array
1448 ###############################################################################
1449 191 ▶sub _get_attr {
Subroutine _get_attr: called CC=6 SCAR=24.3
1450 191 my ($obj, $attr) = @_;
1451 191 ▶ return undef unless defined $obj;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $obj 0 191 191
1452 191 ▶ if (ref($obj) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $obj eq "HASH") 191 0 191
1453 191 ▶ return exists $obj->{$attr} ? $obj->{$attr} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $obj->{$attr} ? : 190 1 191
1454 }
1455 0 ▶ if (ref($obj) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $obj eq "ARRAY") 0 0 0
1456 0 ▶ return $attr =~ /^-?\d+$/ ? $obj->[$attr] : scalar @$obj;
Branch: 1 branch
BranchTrueFalseTotal
$attr =~ /^-?\d+$/ ? : 0 0 0
1457 }
1458 0 return undef;
1459 }
1460  
1461 ###############################################################################
1462 # _split_args - Split comma-separated arguments (respects nested parens/quotes)
1463 ###############################################################################
1464 156 ▶sub _split_args {
Subroutine _split_args: called CC=25 SCAR=32.3
1465 156 my ($str) = @_;
1466 156 ▶ return () unless defined $str && $str =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str and $str =~ /\S/ 1 155 156
Condition66%2/3
defined $str and $str =~ /\S/
!ll&&!rl&&r
01155
MC/DC50%1/2
defined $str and $str =~ /\S/
defined $str $str =~ /\S/
Truth table66%2/3
A and B
A= defined $strB= $str =~ /\S/
ABresult
0X0
100
111
1467 155 my @args;
1468 155 my $cur = '';
1469 155 my $depth = 0;
1470 155 my $in_sq = 0;
1471 155 my $in_dq = 0;
1472  
1473 155 for my $ch (split //, $str) {
1474 681 ▶ if (!$in_sq && !$in_dq && $ch eq "'") { $in_sq = 1; $cur .= $ch; next }
Branch: 1 branch
BranchTrueFalseTotal
if (not $in_sq || $in_dq and $ch eq "'") 4 677 681
Condition100%6/6
not $in_sq || $in_dq and $ch eq "'"
!ll&&!rl&&r
3033744
$in_sq || $in_dq
l!l&&r!l&&!r
20283378
MC/DC100%3/3
not $in_sq || $in_dq and $ch eq "'"
$in_sq $in_dq $ch eq "'"
Truth table100%4/4
not(A || B) and C
A= $in_sqB= $in_dqC= $ch eq "'"
ABCresult
1XX0
01X0
0000
0011
1475 677 ▶ if ($in_sq && $ch eq "'") { $in_sq = 0; $cur .= $ch; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($in_sq and $ch eq "'") 4 673 677
Condition100%3/3
$in_sq and $ch eq "'"
!ll&&!rl&&r
657164
MC/DC100%2/2
$in_sq and $ch eq "'"
$in_sq $ch eq "'"
Truth table100%3/3
A and B
A= $in_sqB= $ch eq "'"
ABresult
0X0
100
111
1476 673 ▶ if (!$in_sq && !$in_dq && $ch eq '"') { $in_dq = 1; $cur .= $ch; next }
Branch: 1 branch
BranchTrueFalseTotal
if (not $in_sq || $in_dq and $ch eq "\"") 99 574 673
Condition100%6/6
not $in_sq || $in_dq and $ch eq "\""
!ll&&!rl&&r
29927599
$in_sq || $in_dq
l!l&&r!l&&!r
16283374
MC/DC100%3/3
not $in_sq || $in_dq and $ch eq "\""
$in_sq $in_dq $ch eq "\""
Truth table100%4/4
not(A || B) and C
A= $in_sqB= $in_dqC= $ch eq "\""
ABCresult
1XX0
01X0
0000
0011
1477 574 ▶ if ($in_dq && $ch eq '"') { $in_dq = 0; $cur .= $ch; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($in_dq and $ch eq "\"") 99 475 574
Condition100%3/3
$in_dq and $ch eq "\""
!ll&&!rl&&r
29118499
MC/DC100%2/2
$in_dq and $ch eq "\""
$in_dq $ch eq "\""
Truth table100%3/3
A and B
A= $in_dqB= $ch eq "\""
ABresult
0X0
100
111
1478 475 ▶ if (!$in_sq && !$in_dq) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in_sq or $in_dq) 275 200 475
Condition100%3/3
$in_sq or $in_dq
l!l&&r!l&&!r
16184275
MC/DC100%2/2
$in_sq or $in_dq
$in_sq $in_dq
Truth table100%3/3
A or B
A= $in_sqB= $in_dq
ABresult
1X1
011
000
1479 275 ▶ if ($ch eq '(' || $ch eq '[' || $ch eq '{') { $depth++ }
Branch: 3 branches
BranchTrueFalseTotal
if ($ch eq '(' or $ch eq '[' or $ch eq '{') { } 1 274 275
elsif ($ch eq ')' or $ch eq ']' or $ch eq '}') { } 1 273 274
elsif ($ch eq ',' and $depth == 0) { } 42 460 502
Condition66%4/6
$ch eq "(" or $ch eq "[" or $ch eq "{"
l!l&&r!l&&!r
10274
$ch eq "(" or $ch eq "["
l!l&&r!l&&!r
01274
MC/DC33%1/3
$ch eq "(" or $ch eq "[" or $ch eq "{"
$ch eq "(" $ch eq "[" $ch eq "{"
Truth table50%2/4
A or B or C
A= $ch eq "("B= $ch eq "["C= $ch eq "{"
ABCresult
1XX1
01X1
0011
0000
1480 1 ▶ elsif ($ch eq ')' || $ch eq ']' || $ch eq '}') { $depth-- }
Condition66%4/6
$ch eq ")" or $ch eq "]" or $ch eq "}"
l!l&&r!l&&!r
10273
$ch eq ")" or $ch eq "]"
l!l&&r!l&&!r
01273
MC/DC33%1/3
$ch eq ")" or $ch eq "]" or $ch eq "}"
$ch eq ")" $ch eq "]" $ch eq "}"
Truth table50%2/4
A or B or C
A= $ch eq ")"B= $ch eq "]"C= $ch eq "}"
ABCresult
1XX1
01X1
0011
0000
1481 ▶ elsif ($ch eq ',' && $depth == 0) {
Condition100%3/3
$ch eq "," and $depth == 0
!ll&&!rl&&r
229242
MC/DC100%2/2
$ch eq "," and $depth == 0
$ch eq "," $depth == 0
Truth table100%3/3
A and B
A= $ch eq ","B= $depth == 0
ABresult
0X0
100
111
1482 42 $cur =~ s/^\s+|\s+$//g;
1483 42 push @args, $cur;
1484 42 $cur = '';
1485 42 next;
1486 }
1487 }
1488 433 $cur .= $ch;
1489 }
1490 155 $cur =~ s/^\s+|\s+$//g;
1491 155 ▶ push @args, $cur if $cur ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $cur ne "" 155 0 155
1492 155 return @args;
1493 }
1494  
1495 ###############################################################################
1496 # _html_escape - Escape HTML special characters
1497 ###############################################################################
1498 37 ▶sub _html_escape {
Subroutine _html_escape: called CC=2 SCAR=7.0
1499 37 my ($s) = @_;
1500 37 ▶ return '' unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 37 37
1501 37 $s =~ s/&/&amp;/g;
1502 37 $s =~ s/</&lt;/g;
1503 37 $s =~ s/>/&gt;/g;
1504 37 $s =~ s/"/&quot;/g;
1505 37 $s =~ s/'/&#39;/g;
1506 37 return $s;
1507 }
1508  
1509 ###############################################################################
1510 # _to_json - Minimal JSON serializer (no external dependency)
1511 ###############################################################################
1512 13 ▶sub _to_json {
Subroutine _to_json: called CC=9 SCAR=22.6
1513 13 my ($val) = @_;
1514 13 ▶ return 'null' unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 1 12 13
1515 12 ▶ return 'true' if ref($val) eq 'SCALAR' && $$val == 1;
Branch: 1 branch
BranchTrueFalseTotal
if ref $val eq "SCALAR" and $$val == 1 0 12 12
Condition33%1/3
ref $val eq "SCALAR" and $$val == 1
!ll&&!rl&&r
1200
MC/DC0%0/2
ref $val eq "SCALAR" and $$val == 1
ref $val eq "SCALAR" $$val == 1
Truth table33%1/3
A and B
A= ref $val eq "SCALAR"B= $$val == 1
ABresult
0X0
100
111
1516 12 ▶ return 'false' if ref($val) eq 'SCALAR' && $$val == 0;
Branch: 1 branch
BranchTrueFalseTotal
if ref $val eq "SCALAR" and $$val == 0 0 12 12
Condition33%1/3
ref $val eq "SCALAR" and $$val == 0
!ll&&!rl&&r
1200
MC/DC0%0/2
ref $val eq "SCALAR" and $$val == 0
ref $val eq "SCALAR" $$val == 0
Truth table33%1/3
A and B
A= ref $val eq "SCALAR"B= $$val == 0
ABresult
0X0
100
111
1517 12 ▶ if (ref($val) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq "ARRAY") 2 10 12
1518 2 return '[' . join(',', map { _to_json($_) } @$val) . ']';
1519 }
1520 10 ▶ if (ref($val) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq "HASH") 1 9 10
1521 return '{' . join(',', map {
1522 0 _to_json($_) . ':' . _to_json($val->{$_})
1523 } sort keys %$val) . '}';
1524 }
1525 9 ▶ if ($val =~ /^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$/) 6 3 9
1526 6 return $val;
1527 }
1528 3 $val =~ s/\\/\\\\/g;
1529 3 $val =~ s/"/\\"/g;
1530 3 $val =~ s/\n/\\n/g;
1531 3 $val =~ s/\r/\\r/g;
1532 3 $val =~ s/\t/\\t/g;
1533 3 return "\"$val\"";
1534 }
1535  
1536 ###############################################################################
1537 # Back to main package -- demo when run directly
1538 ###############################################################################
1539  
1540 # Run as script: perl lib/HP/Handy.pm
1541 13 ▶unless (caller) {
Branch: 1 branch
BranchTrueFalseTotal
unless (caller) 0 13 13
1542 0 my $tmpl = HP::Handy->new(auto_escape => 1);
1543  
1544 0 my $source = <<'TMPL';
1545 <!DOCTYPE html>
1546 <html>
1547 <head><meta charset="utf-8"><title>HP::Handy Demo</title>
1548 <style>body{font-family:sans-serif;max-width:600px;margin:40px auto;padding:0 20px}
1549 h1{color:#336699}table{border-collapse:collapse;width:100%}
1550 td { padding: 6px 10px; border: 1px solid #ccc }
1551 th { padding: 6px 10px; border: 1px solid #ccc }
1552 tr:nth-child(even) { background: #f8f8f8 }
1553 </style>
1554 </head>
1555 <body>
1556 <h1>HP::Handy Demo</h1>
1557 <p>Version: {{ version }}</p>
1558  
1559 <h2>Variable and Filter</h2>
1560 <p>Hello, {{ name | upper }}!</p>
1561 <p>Escaped: {{ snippet }}</p>
1562  
1563 <h2>For Loop</h2>
1564 <table>
1565 <tr><th>#</th><th>Item</th><th>First?</th><th>Last?</th></tr>
1566 {% for item in items %}
1567 <tr>
1568 <td>{{ loop.index }}</td>
1569 <td>{{ item }}</td>
1570 <td>{{ "yes" if loop.first else "no" }}</td>
1571 <td>{{ "yes" if loop.last else "no" }}</td>
1572 </tr>
1573 {% endfor %}
1574 </table>
1575  
1576 <h2>Conditional</h2>
1577 {% if score >= 90 %}
1578 <p>Grade: A</p>
1579 {% elif score >= 70 %}
1580 <p>Grade: B</p>
1581 {% else %}
1582 <p>Grade: C</p>
1583 {% endif %}
1584  
1585 <h2>Set and Default Filter</h2>
1586 {% set greeting = "Konnichiwa" %}
1587 <p>{{ greeting | default("Hello") }}</p>
1588 <p>Missing: {{ missing | default("(none)") }}</p>
1589 </body>
1590 </html>
1591 TMPL
1592  
1593 0 my $html = $tmpl->render_string($source, {
1594 version => $HP::Handy::VERSION,
1595 name => 'World',
1596 snippet => '<script>alert("xss")</script>',
1597 items => [ 'Perl', 'Python', 'Ruby', 'JavaScript' ],
1598 score => 85,
1599 });
1600  
1601 0 print $html;
1602 }
1603  
1604  
1605 ###############################################################################
1606 # HP::Handy::SafeString - trusted HTML string (bypasses auto_escape)
1607 ###############################################################################
1608 package HP::Handy::SafeString;
1609 use overload
1610 2 ▶ q("") => sub { ${$_[0]} },
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
1611 13 fallback => 1;
1612  
1613 package HP::Handy;
1614  
1615 13 1;
1616  
1617 __END__
↑ Summary