lib/HP/Handy.pm

filter: Statementstmt 91.7% 914 / 996 Branchbran 76.4% 558 / 730 Conditioncond 69.7% 207 / 297 MC/DCmcdc 49.7% 88 / 177 Subroutinesub 96.0% 97 / 101 Podpod 100% 5 / 5 totaltotal 81.0% 1869 / 2306 CC 483 SCAR 40.4
CC
483
Cov
83%
_eval_expr
177.2
_eval_tokens
73.4
_loop_changed
56.0
CRAP 56.8
« blib/lib/HP/Handy.pm ↑ 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 12 ▶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 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 12 ▶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 12 12
Condition33%1/3
$] < "5.006" and not defined &warnings::import
!ll&&!rl&&r
1200
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 12 ▶use warnings; local $^W = 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 12 ▶BEGIN { pop @INC if $INC[-1] eq '.' }
Subroutine BEGIN: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
if $INC[-1] eq "." 0 12 12
24  
25 12 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 12 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 12 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 12 $VERSION = '0.01';
29 12 $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 12 '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 12 );
265  
266 ###############################################################################
267 # Constructor
268 ###############################################################################
269 683 ▶sub new {
Subroutine new: called CC=11 SCAR=24.0
Pod documented
270 683 my ($class, %args) = @_;
271 my $self = {
272 template_dir => defined $args{template_dir} ? $args{template_dir} : '.',
273 auto_escape => defined $args{auto_escape} ? $args{auto_escape} : 1,
274 trim_blocks => defined $args{trim_blocks} ? $args{trim_blocks} : 0,
275 lstrip_blocks => defined $args{lstrip_blocks} ? $args{lstrip_blocks} : 0,
276 block_start => defined $args{block_start} ? $args{block_start} : '{%',
277 block_end => defined $args{block_end} ? $args{block_end} : '%}',
278 var_start => defined $args{var_start} ? $args{var_start} : '{{',
279 var_end => defined $args{var_end} ? $args{var_end} : '}}',
280 comment_start => defined $args{comment_start} ? $args{comment_start} : '{#',
281 683 ▶ comment_end => defined $args{comment_end} ? $args{comment_end} : '#}',
Branch: 10 branches
BranchTrueFalseTotal
defined $args{'template_dir'} ? : 16 667 683
defined $args{'auto_escape'} ? : 675 8 683
defined $args{'trim_blocks'} ? : 9 674 683
defined $args{'lstrip_blocks'} ? : 8 675 683
defined $args{'block_start'} ? : 1 682 683
defined $args{'block_end'} ? : 1 682 683
defined $args{'var_start'} ? : 1 682 683
defined $args{'var_end'} ? : 1 682 683
defined $args{'comment_start'} ? : 0 683 683
defined $args{'comment_end'} ? : 0 683 683
282 _filters => { %FILTERS },
283 _tests => { %TESTS },
284 _blocks => {}, # block name => template string (for inheritance)
285 _macros => {}, # macro name => { args=>[], defaults=>[], body=>'' }
286 _extends => '', # parent template name
287 };
288 683 bless $self, $class;
289 683 return $self;
290 }
291  
292 ###############################################################################
293 # add_filter - Register a custom filter
294 ###############################################################################
295 7 ▶sub add_filter {
Subroutine add_filter: called CC=3 SCAR=11.0
Pod documented
296 7 my ($self, $name, $code) = @_;
297 7 ▶ croak "add_filter: name required" unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 7 7
298 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
299 6 $self->{_filters}{$name} = $code;
300 6 return $self;
301 }
302  
303 ###############################################################################
304 # add_test - Register a custom test
305 ###############################################################################
306 4 ▶sub add_test {
Subroutine add_test: called CC=3 SCAR=11.3
Pod documented
307 4 my ($self, $name, $code) = @_;
308 4 ▶ croak "add_test: name required" unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 4 4
309 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
310 4 $self->{_tests}{$name} = $code;
311 4 return $self;
312 }
313  
314 ###############################################################################
315 # render_file - Render a template file with variables
316 ###############################################################################
317 11 ▶sub render_file {
Subroutine render_file: called CC=2 SCAR=7.0
Pod documented
318 11 my ($self, $filename, $vars) = @_;
319 11 ▶ $vars = {} unless defined $vars;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $vars 0 11 11
320 11 my $source = $self->_load_file($filename);
321 9 return $self->_render($source, $vars, $filename);
322 }
323  
324 ###############################################################################
325 # render_string - Render a template string with variables
326 ###############################################################################
327 669 ▶sub render_string {
Subroutine render_string: called CC=2 SCAR=6.9
Pod documented
328 669 my ($self, $source, $vars) = @_;
329 669 ▶ $vars = {} unless defined $vars;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $vars 1 668 669
330 669 return $self->_render($source, $vars, '<string>');
331 }
332  
333 ###############################################################################
334 # _load_file - Read template from disk
335 ###############################################################################
336 26 ▶sub _load_file {
Subroutine _load_file: called CC=5 SCAR=16.2
337 26 my ($self, $filename) = @_;
338  
339 # Prevent path traversal
340 26 ▶ if ($filename =~ /\.\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename =~ /\.\./) 1 25 26
341 1 croak "HP::Handy: path traversal not allowed in '$filename'";
342 }
343  
344 25 my $path;
345 25 ▶ if (File::Spec->file_name_is_absolute($filename)) {
Branch: 1 branch
BranchTrueFalseTotal
if ('File::Spec'->file_name_is_absolute($filename)) { } 0 25 25
346 0 $path = $filename;
347 }
348 else {
349 25 $path = File::Spec->catfile($self->{template_dir}, $filename);
350 }
351  
352 25 local *HPHANDY_FH;
353 25 ▶ 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}': $!") 23 2 25
354 23 local $/;
355 23 my $source = <HPHANDY_FH>;
356 23 close(HPHANDY_FH);
357 23 ▶ return defined $source ? $source : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $source ? : 23 0 23
358 }
359  
360 ###############################################################################
361 # _render - Core rendering engine
362 ###############################################################################
363 678 ▶sub _render {
Subroutine _render: called CC=10 SCAR=23.9
364 678 my ($self, $source, $vars, $filename) = @_;
365 678 ▶ $filename = defined $filename ? $filename : '<string>';
Branch: 1 branch
BranchTrueFalseTotal
defined $filename ? : 678 0 678
366  
367 # Reset per-render state
368 678 $self->{_blocks} = {};
369 678 $self->{_macros} = {};
370 678 $self->{_extends} = '';
371  
372 # First pass: collect blocks and macros, detect extends
373 678 $source = $self->_preprocess($source, $vars);
374  
375 # If extends was found, do inheritance (iteratively for multi-level)
376 678 ▶ while ($self->{_extends} ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_extends"} ne "") 7 678 685
377 7 my $parent_src = $self->_load_file($self->{_extends});
378 7 $self->{_extends} = '';
379 # Collect parent blocks/macros without overwriting child definitions
380 7 $parent_src = $self->_preprocess_parent($parent_src, $vars);
381 # Apply child blocks to parent template
382 7 $source = $self->_apply_inheritance($parent_src, $vars);
383 # Now resolve all known blocks so deeper levels see merged content.
384 # For each block in _blocks, expand any other known blocks inside it.
385 7 my $merged = 1;
386 7 my $passes = 10;
387 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
388 7 $merged = 0;
389 7 for my $bname (keys %{$self->{_blocks}}) {
390 10 my $prev = $self->{_blocks}{$bname};
391 10 my $val = $prev;
392 10 my $lim2 = 10;
393 10 ▶ while ($lim2-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lim2-- > 0) 10 0 10
394 10 my $prev2 = $val;
395 10 $val =~ s/\{%-?\s*block\s+(\w+)\s*-?%\}((?:(?!\{%-?\s*block\b)[\s\S])*?)\{%-?\s*endblock(?:\s+\1)?\s*-?%\}/
396 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
397 /gse;
398 10 ▶ last if $val eq $prev2;
Branch: 1 branch
BranchTrueFalseTotal
if $val eq $prev2 10 0 10
399 }
400 10 $val =~ s/\x00BLK\x01(\w+)\x01(.*?)\x00EBLK\x00/$2/gs;
401 10 $val =~ s/\{%-?\s*endblock(?:\s+\w+)?\s*-?%\}//g;
402 10 ▶ if ($val ne $prev) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val ne $prev) 0 10 10
403 0 $self->{_blocks}{$bname} = $val;
404 0 $merged = 1;
405 }
406 }
407 }
408 }
409  
410 678 return $self->_eval_template($source, $vars, $filename);
411 }
412  
413 ###############################################################################
414 # _preprocess - Scan for extends/block/macro; strip them for non-inherited use
415 ###############################################################################
416 678 ▶sub _preprocess {
Subroutine _preprocess: called CC=7 SCAR=19.5
417 678 my ($self, $source, $vars) = @_;
418  
419 # Detect {% extends "..." %}
420 678 ▶ if ($source =~ s/\{%-?\s*extends\s+["']([^"']+)["']\s*-?%\}[ \t]*\n?//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source =~ s/\{%-?\s*extends\s+["']([^"']+)["']\s*-?%\}[ \t]*\n?//) 6 672 678
421 6 $self->{_extends} = $1;
422 }
423  
424 # Collect {% macro name(args) %}...{% endmacro %}
425 678 ▶ 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 678 688
426 10 my ($mname, $argstr, $body) = ($1, $2, $3);
427 10 my (@args, @defaults);
428 10 for my $a (split /\s*,\s*/, $argstr) {
429 16 $a =~ s/^\s+|\s+$//g;
430 16 ▶ next if $a eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $a eq "" 0 16 16
431 16 ▶ if ($a =~ /^(\w+)\s*=\s*(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a =~ /^(\w+)\s*=\s*(.+)$/) { } 2 14 16
432 2 push @args, $1;
433 2 push @defaults, $2;
434 }
435 else {
436 14 push @args, $a;
437 14 push @defaults, undef;
438 }
439 }
440 10 $self->{_macros}{$mname} = { args => \@args, defaults => \@defaults, body => $body };
441 }
442  
443 # Collect {% block name %}...{% endblock %} from child
444 678 ▶ 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 678 688
445  
446 678 return $source;
447 }
448  
449 10 ▶sub _store_block {
Subroutine _store_block: called CC=2 SCAR=7.1
450 10 my ($self, $name, $content) = @_;
451 10 ▶ $self->{_blocks}{$name} = $content unless exists $self->{_blocks}{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_blocks"}{$name} 10 0 10
452 10 return '';
453 }
454  
455  
456 # _preprocess_parent - Scan parent for extends/macro; collect NEW blocks only
457 # (does NOT strip block tags -- those must survive for _apply_inheritance)
458 7 ▶sub _preprocess_parent {
Subroutine _preprocess_parent: called CC=8 SCAR=30.6
459 7 my ($self, $source, $vars) = @_;
460  
461 # Detect {% extends "..." %} in parent (for multi-level)
462 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
463 1 $self->{_extends} = $1;
464 }
465  
466 # Collect macros from parent (without overwriting child macros)
467 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
468 0 my ($mname, $argstr, $body) = ($1, $2, $3);
469 0 ▶ next if exists $self->{_macros}{$mname};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_macros"}{$mname} 0 0 0
470 0 my (@args, @defaults);
471 0 for my $a (split /\s*,\s*/, $argstr) {
472 0 $a =~ s/^\s+|\s+$//g;
473 0 ▶ next if $a eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $a eq "" 0 0 0
474 0 ▶ if ($a =~ /^(\w+)\s*=\s*(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a =~ /^(\w+)\s*=\s*(.+)$/) { } 0 0 0
475 0 push @args, $1;
476 0 push @defaults, $2;
477 }
478 else {
479 0 push @args, $a;
480 0 push @defaults, undef;
481 }
482 }
483 0 $self->{_macros}{$mname} = { args => \@args, defaults => \@defaults, body => $body };
484 }
485  
486 # Collect NEW blocks (without overwriting child blocks).
487 # Expand already-known child blocks within the body before storing.
488 7 my $tmp = $source;
489 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
490 7 $self->_store_block_expanded($1, $2)
491 /se) {}
492  
493 7 return $source; # keep block tags intact for _apply_inheritance
494 }
495  
496 7 ▶sub _store_block_expanded {
Subroutine _store_block_expanded: called CC=5 SCAR=16.1
497 7 my ($self, $name, $body) = @_;
498 7 ▶ unless (exists $self->{_blocks}{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_blocks"}{$name}) 1 6 7
499 # Expand already-resolved child blocks inside this body.
500 # Use _apply_inheritance logic on the snippet.
501 1 my $expanded = $body;
502 1 my $lim = 10;
503 1 ▶ while ($lim-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lim-- > 0) 2 0 2
504 2 my $prev = $expanded;
505 # Replace innermost (non-nested) blocks that are known
506 2 $expanded =~ s/\{%-?\s*block\s+(\w+)\s*-?%\}((?:(?!\{%-?\s*block\b)[\s\S])*?)\{%-?\s*endblock(?:\s+\1)?\s*-?%\}/
507 1 ▶ exists $self->{_blocks}{$1} ? $self->{_blocks}{$1} : "__BLK__" . $1 . "__SEP__" . $2 . "__EBLK__"
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'_blocks'}{$1} ? : 1 0 1
508 /gse;
509 2 ▶ last if $expanded eq $prev;
Branch: 1 branch
BranchTrueFalseTotal
if $expanded eq $prev 1 1 2
510 }
511 # Clean up any un-expanded block tags
512 1 $expanded =~ s/__BLK__(\w+)__SEP__(.*?)__EBLK__/$2/gs;
513 1 $expanded =~ s/\{%-?\s*endblock(?:\s+\w+)?\s*-?%\}//g;
514 1 $self->{_blocks}{$name} = $expanded;
515 }
516 7 return '';
517 }
518  
519  
520 ###############################################################################
521 # _apply_inheritance - Render parent with child blocks
522 ###############################################################################
523 7 ▶sub _apply_inheritance {
Subroutine _apply_inheritance: called CC=4 SCAR=13.9
524 7 my ($self, $parent_src, $vars) = @_;
525  
526 # Iteratively substitute innermost (non-nested) blocks first.
527 # A block body that contains no other {% block %} tags is "innermost".
528 7 my $limit = 20;
529 7 ▶ while ($limit-- > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($limit-- > 0) 15 0 15
530 15 my $changed = 0;
531 15 $parent_src =~ s/\{%-?\s*block\s+(\w+)\s*-?%\}((?:(?!\{%-?\s*block\b)[\s\S])*?)\{%-?\s*endblock(?:\s+\1)?\s*-?%\}/
532 10 ▶ do { $changed = 1; exists $self->{_blocks}{$1} ? $self->{_blocks}{$1} : $2 }
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'_blocks'}{$1} ? : 9 1 10
533 /gse;
534 15 ▶ last unless $changed;
Branch: 1 branch
BranchTrueFalseTotal
unless $changed 7 8 15
535 }
536  
537 # Remove any orphaned {% endblock %} left from outer block substitutions
538 7 $parent_src =~ s/\{%-?\s*endblock(?:\s+\w+)?\s*-?%\}//g;
539  
540 7 return $parent_src;
541 }
542  
543 ###############################################################################
544 # _eval_template - Evaluate template directives recursively
545 ###############################################################################
546 697 ▶sub _eval_template {
Subroutine _eval_template: called CC=7 SCAR=19.5
547 697 my ($self, $source, $vars, $filename) = @_;
548 697 ▶ $filename = defined $filename ? $filename : '<string>';
Branch: 1 branch
BranchTrueFalseTotal
defined $filename ? : 697 0 697
549  
550 # Apply trim_blocks / lstrip_blocks
551 697 ▶ if ($self->{trim_blocks}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"trim_blocks"}) 8 689 697
552 8 $source =~ s/(%\}|#\})[ \t]*\n/$1\n/g; # already minimal; remove newline after tag
553 8 $source =~ s/(%\}|#\})\n/$1/g;
554 }
555 697 ▶ if ($self->{lstrip_blocks}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"lstrip_blocks"}) 8 689 697
556 8 $source =~ s/^[ \t]*(\{[%#])/$1/mg;
557 }
558  
559 697 my $bs = quotemeta($self->{block_start});
560 697 my $be = quotemeta($self->{block_end});
561 697 my $vs = quotemeta($self->{var_start});
562 697 my $ve = quotemeta($self->{var_end});
563 697 my $cs = quotemeta($self->{comment_start});
564 697 my $ce = quotemeta($self->{comment_end});
565  
566 # Protect {% raw %}...{% endraw %} blocks BEFORE comment stripping,
567 # so that {# ... #} inside raw blocks is preserved as-is.
568 697 my @raw_chunks;
569 697 my $raw_ph = "\x00RAW\x00";
570 697 my $bs_re = quotemeta($self->{block_start});
571 697 my $be_re = quotemeta($self->{block_end});
572 697 $source =~ s/${bs_re}-?\s*raw\s*-?${be_re}(.*?)${bs_re}-?\s*endraw\s*-?${be_re}/
573 9 my $idx = scalar @raw_chunks;
574 9 push @raw_chunks, $1;
575 9 "${raw_ph}${idx}\x00"
576 /gse;
577  
578 # Remove comments (after raw protection so {# #} inside raw is safe)
579 697 $source =~ s/$cs.*?$ce//gs;
580  
581 # Tokenise into chunks
582 # Tokens: [ 'text', $str ] or [ 'var', $expr ] or [ 'tag', $stmt ]
583 697 my @tokens = $self->_tokenize($source);
584  
585 # Restore raw chunks in text tokens
586 697 for my $tok (@tokens) {
587 1692 ▶ 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 1683 1692
Condition100%3/3
$tok->[0] eq "text" and $tok->[1] =~ /\Q$raw_ph\E/
!ll&&!rl&&r
13033809
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
588 9 $tok->[1] =~ s/\Q${raw_ph}\E(\d+)\x00/$raw_chunks[$1]/g;
589 }
590 }
591  
592 697 return $self->_eval_tokens(\@tokens, $vars, 0, $filename);
593 }
594  
595 ###############################################################################
596 # _tokenize - Split source into text/var/tag tokens
597 ###############################################################################
598 697 ▶sub _tokenize {
Subroutine _tokenize: called CC=28 SCAR=33.5
599 697 my ($self, $source) = @_;
600  
601 697 my $bs = $self->{block_start}; # {%
602 697 my $be = $self->{block_end}; # %}
603 697 my $vs = $self->{var_start}; # {{
604 697 my $ve = $self->{var_end}; # }}
605  
606 # Build split pattern
607 697 my $qbs = quotemeta($bs); my $qbe = quotemeta($be);
608 697 my $qvs = quotemeta($vs); my $qve = quotemeta($ve);
609  
610 697 my @tokens;
611 697 my $pos = 0;
612 697 my $len = length($source);
613  
614 697 ▶ while ($pos < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < $len) 1355 645 2000
615 # Find next tag start
616 1355 my $var_pos = index($source, $vs, $pos);
617 1355 my $block_pos = index($source, $bs, $pos);
618  
619 # No more tags
620 1355 ▶ if ($var_pos < 0 && $block_pos < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($var_pos < 0 and $block_pos < 0) 52 1303 1355
Condition100%3/3
$var_pos < 0 and $block_pos < 0
!ll&&!rl&&r
67063352
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
621 52 push @tokens, [ 'text', substr($source, $pos) ];
622 52 last;
623 }
624  
625 # Determine which comes first
626 1303 my $next;
627 1303 ▶ 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) { } 479 824 1303
Condition100%6/6
$var_pos >= 0 and $block_pos < 0 || $var_pos <= $block_pos
!ll&&!rl&&r
633191479
$block_pos < 0 or $var_pos <= $block_pos
l!l&&r!l&&!r
352127191
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
628 479 $next = $var_pos;
629 479 my $end = index($source, $ve, $next + length($vs));
630 479 ▶ if ($end < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end < 0) 0 479 479
631 0 push @tokens, [ 'text', substr($source, $pos) ];
632 0 last;
633 }
634 479 ▶ if ($next > $pos) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next > $pos) 37 442 479
635 37 push @tokens, [ 'text', substr($source, $pos, $next - $pos) ];
636 }
637 479 my $raw_expr = substr($source, $next + length($vs), $end - $next - length($vs));
638 # Whitespace control dash: "{{- expr -}}"
639 # The dash must be the very first/last non-whitespace character AND
640 # must be followed/preceded by whitespace (not part of the expression).
641 # Rule: if raw_expr starts with "-" followed by space/end, it's WS control.
642 # "-5" or "-x" is a negative expression, NOT whitespace control.
643 479 my $strip_left = ($raw_expr =~ /^-(?:\s|$)/);
644 479 my $strip_right = ($raw_expr =~ /(?:^|\s)-\s*$/);
645 479 my $expr = $raw_expr;
646 479 $expr =~ s/^\s+|\s+$//g;
647 479 ▶ $expr =~ s/^-\s+// if $strip_left; # remove leading "- "
Branch: 1 branch
BranchTrueFalseTotal
if $strip_left 7 472 479
648 479 ▶ $expr =~ s/\s+-$// if $strip_right; # remove trailing " -"
Branch: 1 branch
BranchTrueFalseTotal
if $strip_right 7 472 479
649 # Strip whitespace from preceding text token if {{- }}
650 479 ▶ 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 473 479
Condition83%5/6
$strip_left and @tokens and $tokens[-1][0] eq "text"
!ll&&!rl&&r
47306
$strip_left and @tokens
!ll&&!rl&&r
47216
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
651 6 $tokens[-1][1] =~ s/\s+$//;
652 6 ▶ splice @tokens, -1 if $tokens[-1][1] eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $tokens[-1][1] eq "" 3 3 6
653 }
654 479 push @tokens, [ 'var', $expr, $strip_right ];
655 479 $pos = $end + length($ve);
656 # Strip leading whitespace from next text if {{ -}}
657 479 ▶ if ($strip_right && $pos < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($strip_right and $pos < $len) 6 473 479
Condition100%3/3
$strip_right and $pos < $len
!ll&&!rl&&r
47216
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
658 6 my $rest = substr($source, $pos);
659 6 my $stripped = $rest;
660 6 $stripped =~ s/^\s+//;
661 6 $pos += length($rest) - length($stripped);
662 }
663 }
664 else {
665 824 $next = $block_pos;
666 824 my $end = index($source, $be, $next + length($bs));
667 824 ▶ if ($end < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end < 0) 0 824 824
668 0 push @tokens, [ 'text', substr($source, $pos) ];
669 0 last;
670 }
671 824 ▶ if ($next > $pos) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next > $pos) 304 520 824
672 # Whitespace control: {% - strips preceding whitespace
673 304 my $text_before = substr($source, $pos, $next - $pos);
674 304 my $stmt_raw = substr($source, $next + length($bs), $end - $next - length($bs));
675 304 ▶ if ($stmt_raw =~ /^-/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stmt_raw =~ /^-/) 2 302 304
676 2 $text_before =~ s/\s+$//;
677 }
678 304 ▶ push @tokens, [ 'text', $text_before ] if $text_before ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $text_before ne "" 303 1 304
679 }
680 824 my $stmt_full = substr($source, $next + length($bs), $end - $next - length($bs));
681 824 my $rstrip_tag = ($stmt_full =~ /\s*-\s*$/);
682 824 my $stmt = $stmt_full;
683 824 $stmt =~ s/^\s+|\s+$//g;
684 824 $stmt =~ s/^-\s*|\s*-$//g;
685 824 push @tokens, [ 'tag', $stmt ];
686 824 $pos = $end + length($be);
687 # Whitespace control: -%} strips following whitespace
688 824 ▶ if ($rstrip_tag && $pos < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rstrip_tag and $pos < $len) 2 822 824
Condition66%2/3
$rstrip_tag and $pos < $len
!ll&&!rl&&r
82202
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
689 2 substr($source, $pos) =~ s/^\s+//;
690 2 ▶ $pos = $len - length($source) + $pos if $source =~ s/^(\s+)//;
Branch: 1 branch
BranchTrueFalseTotal
if $source =~ s/^(\s+)// 0 2 2
691 # Simpler: just advance pos past leading whitespace
692 2 my $rest = substr($source, $pos);
693 2 my $stripped = $rest;
694 2 $stripped =~ s/^\s+//;
695 2 $pos += length($rest) - length($stripped);
696 }
697 # trim_blocks: eat newline after %}
698 824 ▶ 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 824 824
Condition88%8/9
not $rstrip_tag and $self->{"trim_blocks"} and $pos < $len and substr($source, $pos, 1) eq "\n"
!ll&&!rl&&r
813110
not $rstrip_tag and $self->{"trim_blocks"} and $pos < $len
!ll&&!rl&&r
808511
not $rstrip_tag and $self->{"trim_blocks"}
!ll&&!rl&&r
280616
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
699 0 $pos++;
700 }
701 }
702 }
703 697 return @tokens;
704 }
705  
706 ###############################################################################
707 # _eval_tokens - Execute a token list and return rendered string
708 ###############################################################################
709 1256 ▶sub _eval_tokens {
Subroutine _eval_tokens: called CC=31 SCAR=43.0
710 1256 my ($self, $tokens, $vars, $start, $filename) = @_;
711 1256 ▶ $start = 0 unless defined $start;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $start 0 1256 1256
712  
713 1256 my $out = '';
714 1256 my $i = $start;
715  
716 1256 ▶ while ($i <= $#$tokens) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $tokens->$#*) 1528 1256 2784
717 1528 my ($type, $content) = @{$tokens->[$i]};
718  
719 1528 ▶ if ($type eq 'text') {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'text') { } 378 1150 1528
elsif ($type eq 'var') { } 663 487 1150
elsif ($type eq 'tag') { } 487 0 487
720 378 $out .= $content;
721 378 $i++;
722 }
723 elsif ($type eq 'var') {
724 663 my $val = $self->_eval_expr($content, $vars);
725 663 ▶ if (ref($val) eq 'HP::Handy::SafeString') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $val eq 'HP::Handy::SafeString') { } 20 643 663
elsif ($self->{'auto_escape'} and defined $val and not ref $val) { } 15 1256 1271
726 20 $val = $$val;
727 }
728 ▶ elsif ($self->{auto_escape} && defined $val && !ref($val)) {
Condition66%4/6
$self->{"auto_escape"} and defined $val and not ref $val
!ll&&!rl&&r
628015
$self->{"auto_escape"} and defined $val
!ll&&!rl&&r
628015
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
729 15 $val = _html_escape($val);
730 }
731 663 ▶ $out .= defined $val ? $val : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $val ? : 656 7 663
732 663 $i++;
733 }
734 elsif ($type eq 'tag') {
735 487 my $stmt = $content;
736  
737 # --- set ---
738 487 ▶ if ($stmt =~ /^set\s+(\w+)\s*=\s*(.+)$/) {
Branch: 10 branches
BranchTrueFalseTotal
if ($stmt =~ /^set\s+(\w+)\s*=\s*(.+)$/) { } 54 433 487
elsif ($stmt =~ /^set\s+(\w+)\s*$/) { } 0 433 433
elsif ($stmt =~ /^if\s+(.+)$/) { } 287 146 433
elsif ($stmt =~ /^for\s+(.+?)\s+in\s+(.+?)(?:\s+if\s+(.+))?$/) { } 125 21 146
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
739 54 my ($vname, $expr) = ($1, $2);
740 54 $vars->{$vname} = $self->_eval_expr($expr, $vars);
741 54 $i++;
742 }
743 # --- set block (multi-line) ---
744 elsif ($stmt =~ /^set\s+(\w+)\s*$/) {
745 0 my $vname = $1;
746 0 my ($body, $ni) = $self->_collect_until($tokens, $i + 1, 'endset');
747 0 $vars->{$vname} = $self->_eval_tokens($tokens, $vars, $i + 1, $filename);
748 # re-render just the body tokens
749 0 my @body_tokens = @{$tokens}[$i+1 .. $ni-1];
750 0 $vars->{$vname} = $self->_eval_tokens(\@body_tokens, $vars, 0, $filename);
751 0 $i = $ni + 1;
752 }
753 # --- if / elif / else / endif ---
754 elsif ($stmt =~ /^if\s+(.+)$/) {
755 287 my $cond_expr = $1;
756 287 my ($result, $ni) = $self->_eval_if($tokens, $i, $vars, $filename);
757 287 $out .= $result;
758 287 $i = $ni;
759 }
760 # --- for / endfor ---
761 elsif ($stmt =~ /^for\s+(.+?)\s+in\s+(.+?)(?:\s+if\s+(.+))?$/) {
762 125 my ($loop_var, $iter_expr, $cond_expr) = ($1, $2, $3);
763 125 my ($result, $ni) = $self->_eval_for($tokens, $i, $vars, $loop_var, $iter_expr, $cond_expr, $filename);
764 125 $out .= $result;
765 125 $i = $ni;
766 }
767 # --- include ---
768 elsif ($stmt =~ /^include\s+["']([^"']+)["'](?:\s+ignore\s+missing)?$/) {
769 8 my $inc_file = $1;
770 8 my $inc_src;
771 8 eval { $inc_src = $self->_load_file($inc_file) };
772 8 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 7 8
773 1 ▶ $inc_src = '' if $stmt =~ /ignore\s+missing/;
Branch: 1 branch
BranchTrueFalseTotal
if $stmt =~ /ignore\s+missing/ 1 0 1
774 1 ▶ croak $@ unless $stmt =~ /ignore\s+missing/;
Branch: 1 branch
BranchTrueFalseTotal
unless $stmt =~ /ignore\s+missing/ 0 1 1
775 }
776 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
777 8 $i++;
778 }
779 # --- call macro ---
780 ▶ 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
781 0 my ($mname, $argstr) = ($1, $2);
782 0 $out .= $self->_call_macro($mname, $argstr, $vars, $filename);
783 0 $i++;
784 }
785 # --- raw / endraw ---
786 elsif ($stmt eq 'raw') {
787 0 my ($raw, $ni) = $self->_collect_raw($tokens, $i + 1);
788 0 $out .= $raw;
789 0 $i = $ni + 1;
790 }
791 # --- with / endwith ---
792 elsif ($stmt =~ /^with(?:\s+(.+))?$/) {
793 13 my $assigns = $1;
794 13 my %local_vars = %$vars;
795 13 ▶ if (defined $assigns) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $assigns) 13 0 13
796 13 for my $pair (_split_args($assigns)) {
797 15 ▶ if ($pair =~ /^(\w+)\s*=\s*(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pair =~ /^(\w+)\s*=\s*(.+)$/) 15 0 15
798 15 $local_vars{$1} = $self->_eval_expr($2, { %local_vars });
799 }
800 }
801 }
802 13 my ($body_tokens_ref, $ni) = $self->_collect_until($tokens, $i + 1, 'endwith');
803 13 $out .= $self->_eval_tokens($body_tokens_ref, { %local_vars }, 0, $filename);
804 13 $i = $ni + 1;
805 }
806 # --- block (standalone, no inheritance) ---
807 elsif ($stmt =~ /^block\s+(\w+)$/) {
808 0 my $bname = $1;
809 0 my ($body_tokens_ref, $ni) = $self->_collect_until($tokens, $i + 1, "endblock $bname", 'endblock');
810 0 ▶ if (exists $self->{_blocks}{$bname}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'_blocks'}{$bname}) { } 0 0 0
811 0 $out .= $self->_eval_template($self->{_blocks}{$bname}, $vars, $filename);
812 }
813 else {
814 0 $out .= $self->_eval_tokens($body_tokens_ref, $vars, 0, $filename);
815 }
816 0 $i = $ni + 1;
817 }
818 # skip endxxx tags (consumed by their openers above)
819 ▶ 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
820 0 $i++;
821 }
822 else {
823 # Unknown tag: pass through as-is for forward compatibility
824 0 $out .= $self->{block_start} . ' ' . $stmt . ' ' . $self->{block_end};
825 0 $i++;
826 }
827 }
828 else {
829 0 $i++;
830 }
831 }
832 1256 return $out;
833 }
834  
835 ###############################################################################
836 # _eval_if - Evaluate if/elif/else/endif block
837 ###############################################################################
838 287 ▶sub _eval_if {
Subroutine _eval_if: called CC=15 SCAR=27.1
839 287 my ($self, $tokens, $start, $vars, $filename) = @_;
840  
841 # Collect all branches: [ [cond_expr, [tokens...]], ..., [undef, [tokens...]] ]
842 287 my @branches;
843 287 my $cur_cond = $tokens->[$start][1];
844 287 $cur_cond =~ s/^if\s+//;
845 287 my @cur_body;
846 287 my $depth = 1;
847 287 my $i = $start + 1;
848  
849 287 ▶ while ($i <= $#$tokens) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $tokens->$#*) 661 0 661
850 661 my ($type, $content) = @{$tokens->[$i]};
851 661 ▶ if ($type eq 'tag') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'tag') { } 343 318 661
852 343 ▶ if ($content =~ /^if\b/) {
Branch: 4 branches
BranchTrueFalseTotal
if ($content =~ /^if\b/) { } 8 335 343
elsif ($content eq 'endif') { } 295 40 335
elsif ($depth == 1 and $content =~ /^elif\s+(.+)$/) { } 13 27 40
elsif ($depth == 1 and $content eq 'else') { } 16 11 27
853 8 $depth++;
854 8 push @cur_body, $tokens->[$i];
855 }
856 elsif ($content eq 'endif') {
857 295 $depth--;
858 295 ▶ if ($depth == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth == 0) { } 287 8 295
859 287 push @branches, [ $cur_cond, [@cur_body] ];
860 287 $i++;
861 287 last;
862 }
863 else {
864 8 push @cur_body, $tokens->[$i];
865 }
866 }
867 ▶ elsif ($depth == 1 && $content =~ /^elif\s+(.+)$/) {
Condition100%3/3
$depth == 1 and $content =~ /^elif\s+(.+)$/
!ll&&!rl&&r
32413
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
868 13 push @branches, [ $cur_cond, [@cur_body] ];
869 13 $cur_cond = $1;
870 13 @cur_body = ();
871 }
872 ▶ elsif ($depth == 1 && $content eq 'else') {
Condition100%3/3
$depth == 1 and $content eq "else"
!ll&&!rl&&r
3816
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
873 16 push @branches, [ $cur_cond, [@cur_body] ];
874 16 $cur_cond = undef;
875 16 @cur_body = ();
876 }
877 else {
878 11 push @cur_body, $tokens->[$i];
879 }
880 }
881 else {
882 318 push @cur_body, $tokens->[$i];
883 }
884 374 $i++;
885 }
886 287 ▶ push @branches, [ $cur_cond, [@cur_body] ] if @cur_body || !defined $cur_cond;
Branch: 1 branch
BranchTrueFalseTotal
if @cur_body or not defined $cur_cond 287 0 287
Condition33%1/3
@cur_body or not defined $cur_cond
l!l&&r!l&&!r
28700
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
887  
888 287 my $result = '';
889 287 for my $branch (@branches) {
890 376 my ($cond, $body) = @$branch;
891 376 ▶ if (!defined $cond || $self->_eval_expr($cond, $vars)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $cond or $self->_eval_expr($cond, $vars)) 214 162 376
Condition100%3/3
not defined $cond or $self->_eval_expr($cond, $vars)
l!l&&r!l&&!r
9205162
MC/DC100%2/2
not defined $cond or $self->_eval_expr($cond, $vars)
not defined $cond $self->_eval_expr($cond, $vars)
Truth table100%3/3
not(A) or B
A= not defined $condB= $self->_eval_expr($cond, $vars)
ABresult
1X1
011
000
892 214 $result = $self->_eval_tokens($body, $vars, 0, $filename);
893 214 last;
894 }
895 }
896 287 return ($result, $i);
897 }
898  
899 ###############################################################################
900 # _eval_for - Evaluate for loop
901 ###############################################################################
902 125 ▶sub _eval_for {
Subroutine _eval_for: called CC=22 SCAR=31.0
903 125 my ($self, $tokens, $start, $vars, $loop_var, $iter_expr, $cond_expr, $filename) = @_;
904  
905 # Collect loop body tokens (until endfor at depth 1)
906 125 my (@body_tokens, @else_tokens);
907 125 my $in_else = 0;
908 125 my $depth = 1;
909 125 my $i = $start + 1;
910  
911 125 ▶ while ($i <= $#$tokens) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $tokens->$#*) 388 0 388
912 388 my ($type, $content) = @{$tokens->[$i]};
913 388 ▶ if ($type eq 'tag') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "tag") 193 195 388
914 193 ▶ if ($content =~ /^(?:for|if|block|macro|with)\b/) { $depth++ }
Branch: 3 branches
BranchTrueFalseTotal
if ($content =~ /^(?:for|if|block|macro|with)\b/) { } 25 168 193
elsif ($content =~ /^end(?:for|if|block|macro|with)/) { } 150 18 168
elsif ($depth == 1 and $content eq 'else') { } 10 10 20
915 elsif ($content =~ /^end(?:for|if|block|macro|with)/) {
916 150 $depth--;
917 150 ▶ if ($depth == 0 && $content eq 'endfor') { $i++; last }
Branch: 1 branch
BranchTrueFalseTotal
if ($depth == 0 and $content eq "endfor") 125 25 150
Condition66%2/3
$depth == 0 and $content eq "endfor"
!ll&&!rl&&r
250125
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
918 }
919 ▶ 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
920 10 $in_else = 1; $i++; next;
921 }
922 }
923 253 ▶ if ($in_else) { push @else_tokens, $tokens->[$i] }
Branch: 1 branch
BranchTrueFalseTotal
if ($in_else) { } 10 243 253
924 243 else { push @body_tokens, $tokens->[$i] }
925 253 $i++;
926 }
927  
928 # Evaluate iterable
929 125 my $iter = $self->_eval_expr($iter_expr, $vars);
930 125 my @items;
931 125 ▶ if (ref($iter) eq 'ARRAY') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $iter eq 'ARRAY') { } 114 11 125
elsif (ref $iter eq 'HASH') { } 5 6 11
elsif (defined $iter) { } 5 1 6
932 114 @items = @$iter;
933 }
934 elsif (ref($iter) eq 'HASH') {
935 5 @items = map { [ $_, $iter->{$_} ] } sort keys %$iter;
936 }
937 elsif (defined $iter) {
938 5 @items = ($iter);
939 }
940  
941 # Apply loop filter
942 125 ▶ if (defined $cond_expr) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cond_expr) 11 114 125
943 11 my @filtered;
944 11 for my $item (@items) {
945 35 my %loop_vars = %$vars;
946 35 $self->_assign_loop_var(\%loop_vars, $loop_var, $item);
947 35 ▶ push @filtered, $item if $self->_eval_expr($cond_expr, \%loop_vars);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_eval_expr($cond_expr, \%loop_vars) 20 15 35
948 }
949 11 @items = @filtered;
950 }
951  
952 125 ▶ if (!@items) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@items) 9 116 125
953 9 return ($self->_eval_tokens(\@else_tokens, $vars, 0, $filename), $i);
954 }
955  
956 116 my $total = scalar @items;
957 116 my $result = '';
958  
959 116 for my $idx (0 .. $#items) {
960 323 my %loop_vars = %$vars;
961 323 $self->_assign_loop_var(\%loop_vars, $loop_var, $items[$idx]);
962  
963 # loop special variable
964 $loop_vars{loop} = {
965 index => $idx + 1,
966 index0 => $idx,
967 revindex => $total - $idx,
968 revindex0 => $total - $idx - 1,
969 first => ($idx == 0) ? 1 : 0,
970 last => ($idx == $#items) ? 1 : 0,
971 length => $total,
972 depth => 1,
973 depth0 => 0,
974 odd => ($idx % 2 == 0) ? 0 : 1,
975 even => ($idx % 2 == 0) ? 1 : 0,
976 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
977 323 ▶ };
Branch: 4 branches
BranchTrueFalseTotal
$idx == 0 ? : 116 207 323
$idx == $#items ? : 116 207 323
$idx % 2 == 0 ? : 200 123 323
$idx % 2 == 0 ? : 200 123 323
978  
979 323 $result .= $self->_eval_tokens(\@body_tokens, \%loop_vars, 0, $filename);
980 }
981  
982 116 return ($result, $i);
983 }
984  
985 358 ▶sub _assign_loop_var {
Subroutine _assign_loop_var: called CC=3 SCAR=11.3
986 358 my ($self, $vars, $loop_var, $item) = @_;
987 # Tuple unpacking: "k, v" in "for k, v in ..."
988 358 ▶ if ($loop_var =~ /^(\w+)\s*,\s*(\w+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($loop_var =~ /^(\w+)\s*,\s*(\w+)$/) { } 10 348 358
989 10 my ($k, $v) = ($1, $2);
990 10 ▶ if (ref($item) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $item eq 'ARRAY') { } 10 0 10
991 10 $vars->{$k} = $item->[0];
992 10 $vars->{$v} = $item->[1];
993 }
994 else {
995 0 $vars->{$k} = $item;
996 0 $vars->{$v} = undef;
997 }
998 }
999 else {
1000 348 $vars->{$loop_var} = $item;
1001 }
1002 }
1003  
1004 0 ▶sub _loop_changed {
Subroutine _loop_changed: not called CC=7 SCAR=40.3
1005 0 my ($attr, $cur, $prev) = @_;
1006 0 ▶ return 1 unless defined $prev;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prev 0 0 0
1007 0 my $cv = _get_attr($cur, $attr);
1008 0 my $pv = _get_attr($prev, $attr);
1009 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
1010 : (!defined $cv || !defined $pv) ? 1
1011 : $cv ne $pv ? 1 : 0;
1012 }
1013  
1014 ###############################################################################
1015 # _collect_until - Collect tokens until end tag (depth-aware)
1016 ###############################################################################
1017 13 ▶sub _collect_until {
Subroutine _collect_until: called CC=7 SCAR=19.5
1018 13 my ($self, $tokens, $start, @end_tags) = @_;
1019 13 my %ends = map { $_ => 1 } @end_tags;
1020 13 my @body;
1021 13 my $depth = 1;
1022 13 my $i = $start;
1023  
1024 # Map each opener tag to its closer. We track depth only for
1025 # openers whose closer matches one of the @end_tags we are looking for,
1026 # plus any nested use of the SAME opener.
1027 13 my %opener_to_closer = (
1028 'if' => 'endif',
1029 'for' => 'endfor',
1030 'block' => 'endblock',
1031 'macro' => 'endmacro',
1032 'with' => 'endwith',
1033 'raw' => 'endraw',
1034 'set' => 'endset',
1035 );
1036 13 my %closer_to_opener = reverse %opener_to_closer;
1037  
1038 13 ▶ while ($i <= $#$tokens) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $tokens->$#*) 42 0 42
1039 42 my ($type, $content) = @{$tokens->[$i]};
1040 42 ▶ if ($type eq 'tag') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "tag") 24 18 42
1041 24 my $bare = $content; $bare =~ s/\s.*//;
1042 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
1043 14 $depth--;
1044 14 ▶ last if $depth == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $depth == 0 13 1 14
1045 }
1046 elsif (exists $opener_to_closer{$bare}) {
1047 # Push only when the closer of THIS opener is in @end_tags
1048 # (i.e. same kind of nesting as what we're looking for)
1049 6 my $closer = $opener_to_closer{$bare};
1050 6 ▶ if (exists $ends{$closer}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ends{$closer}) 1 5 6
1051 1 $depth++;
1052 }
1053 }
1054 }
1055 29 push @body, $tokens->[$i];
1056 29 $i++;
1057 }
1058 13 return (\@body, $i);
1059 }
1060  
1061 ###############################################################################
1062 # _collect_raw - Collect raw text tokens until endraw
1063 ###############################################################################
1064 0 ▶sub _collect_raw {
Subroutine _collect_raw: not called CC=4 SCAR=30.0
1065 0 my ($self, $tokens, $start) = @_;
1066 0 my $raw = '';
1067 0 my $i = $start;
1068 0 ▶ while ($i <= $#$tokens) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $tokens->$#*) 0 0 0
1069 0 my ($type, $content) = @{$tokens->[$i]};
1070 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
1071 0 $raw .= $content;
1072 0 $i++;
1073 }
1074 0 return ($raw, $i);
1075 }
1076  
1077 ###############################################################################
1078 # _call_macro - Call a defined macro
1079 ###############################################################################
1080 11 ▶sub _call_macro {
Subroutine _call_macro: called CC=5 SCAR=16.2
1081 11 my ($self, $mname, $argstr, $vars, $filename) = @_;
1082 11 my $macro = $self->{_macros}{$mname};
1083 11 ▶ return '' unless defined $macro;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $macro 0 11 11
1084  
1085 # Parse positional args
1086 11 my @arg_vals = _split_args($argstr);
1087 11 my %macro_vars = %$vars;
1088  
1089 11 my @margs = @{ $macro->{args} };
1090 11 my @mdefaults = @{ $macro->{defaults} };
1091  
1092 11 for my $idx (0 .. $#margs) {
1093 18 ▶ if ($idx < @arg_vals) {
Branch: 2 branches
BranchTrueFalseTotal
if ($idx < @arg_vals) { } 17 1 18
elsif (defined $mdefaults[$idx]) { } 1 0 1
1094 17 $macro_vars{$margs[$idx]} = $self->_eval_expr($arg_vals[$idx], $vars);
1095 }
1096 elsif (defined $mdefaults[$idx]) {
1097 1 $macro_vars{$margs[$idx]} = $self->_eval_expr($mdefaults[$idx], $vars);
1098 }
1099 else {
1100 0 $macro_vars{$margs[$idx]} = undef;
1101 }
1102 }
1103  
1104 # varargs / kwargs accessible as special vars
1105 11 $macro_vars{varargs} = [];
1106 11 $macro_vars{kwargs} = {};
1107  
1108 11 return $self->_eval_template($macro->{body}, { %macro_vars }, $filename);
1109 }
1110  
1111 ###############################################################################
1112 # _eval_expr - Evaluate a Jinja2 expression
1113 ###############################################################################
1114 2681 ▶sub _eval_expr {
Subroutine _eval_expr: called CC=150 SCAR=51.8
1115 2681 my ($self, $expr, $vars) = @_;
1116 2681 $expr =~ s/^\s+|\s+$//g;
1117 2681 ▶ return undef unless defined $expr && $expr ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $expr and $expr ne "" 0 2681 2681
Condition33%1/3
defined $expr and $expr ne ""
!ll&&!rl&&r
002681
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
1118  
1119 # Literal: None / True / False
1120 2681 ▶ 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 2678 2681
Condition83%5/6
$expr eq "none" or $expr eq "None" or $expr eq "undefined"
l!l&&r!l&&!r
302678
$expr eq "none" or $expr eq "None"
l!l&&r!l&&!r
212678
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
1121 2678 ▶ return 1 if $expr eq 'true' || $expr eq 'True';
Branch: 1 branch
BranchTrueFalseTotal
if $expr eq "true" or $expr eq "True" 3 2675 2678
Condition66%2/3
$expr eq "true" or $expr eq "True"
l!l&&r!l&&!r
302675
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
1122 2675 ▶ return 0 if $expr eq 'false' || $expr eq 'False';
Branch: 1 branch
BranchTrueFalseTotal
if $expr eq "false" or $expr eq "False" 4 2671 2675
Condition66%2/3
$expr eq "false" or $expr eq "False"
l!l&&r!l&&!r
402671
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
1123  
1124 # Literal: integer / float
1125 2671 ▶ return $expr + 0 if $expr =~ /^-?\d+(\.\d+)?$/;
Branch: 1 branch
BranchTrueFalseTotal
if $expr =~ /^-?\d+(\.\d+)?$/ 327 2344 2671
1126  
1127 # Unary minus: -expr (where expr is not a literal number)
1128 2344 ▶ if ($expr =~ /^-(.+)$/ && $1 !~ /^\d/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^-(.+)$/ and not $1 =~ /^\d/) 1 2343 2344
Condition100%3/3
$expr =~ /^-(.+)$/ and not $1 =~ /^\d/
!ll&&!rl&&r
234031
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
1129 1 my $val = $self->_eval_expr($1, $vars);
1130 1 ▶ return defined $val ? -$val : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $val ? : 1 0 1
1131 }
1132  
1133 # Literal: string
1134 2343 ▶ if ($expr =~ /^"((?:[^"\\]|\\.)*)"$/ || $expr =~ /^'((?:[^'\\]|\\.)*)'$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^"((?:[^"\\]|\\.)*)"$/ or $expr =~ /^'((?:[^'\\]|\\.)*)'$/) 203 2140 2343
Condition100%3/3
$expr =~ /^"((?:[^"\\]|\\.)*)"$/ or $expr =~ /^'((?:[^'\\]|\\.)*)'$/
l!l&&r!l&&!r
19852140
MC/DC100%2/2
$expr =~ /^"((?:[^"\\]|\\.)*)"$/ or $expr =~ /^'((?:[^'\\]|\\.)*)'$/
$expr =~ /^"((?:[^"\\]|\\.)*)"$/ $expr =~ /^'((?:[^'\\]|\\.)*)'$/
Truth table100%3/3
A or B
A= $expr =~ /^"((?:[^"\\]|\\.)*)"$/B= $expr =~ /^'((?:[^'\\]|\\.)*)'$/
ABresult
1X1
011
000
1135 203 my $s = $1;
1136 203 $s =~ s/\\n/\n/g; $s =~ s/\\t/\t/g; $s =~ s/\\r/\r/g;
1137 203 $s =~ s/\\(.)/$1/g;
1138 203 return $s;
1139 }
1140  
1141 # Literal: [] list
1142 2140 ▶ if ($expr =~ /^\[(.+)\]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^\[(.+)\]$/) 7 2133 2140
1143 7 my @elems = _split_args($1);
1144 7 return [ map { $self->_eval_expr($_, $vars) } @elems ];
1145 }
1146  
1147 # Literal: {} dict
1148 2133 ▶ if ($expr =~ /^\{(.+)\}$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^\{(.+)\}$/) 1 2132 2133
1149 1 return $self->_eval_dict($1, $vars);
1150 }
1151  
1152 # Literal: range() -- only when no pipe follows (e.g. range(3) but not range(3)|join)
1153 2132 ▶ if ($expr =~ /^range\(([^|]*)\)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^range\(([^|]*)\)$/) 22 2110 2132
1154 22 return $self->_eval_range($1, $vars);
1155 }
1156  
1157 # Conditional expression: a if cond else b
1158 2110 ▶ if ($expr =~ /^(.+?)\s+if\s+(.+?)\s+else\s+(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^(.+?)\s+if\s+(.+?)\s+else\s+(.+)$/) 25 2085 2110
1159 25 my ($a, $cond, $b) = ($1, $2, $3);
1160 25 ▶ return $self->_eval_expr($cond, $vars) ? $self->_eval_expr($a, $vars) : $self->_eval_expr($b, $vars);
Branch: 1 branch
BranchTrueFalseTotal
$self->_eval_expr($cond, $vars) ? : 14 11 25
1161 }
1162  
1163 # 'not' operator
1164 2085 ▶ if ($expr =~ /^not\s+(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^not\s+(.+)$/) 67 2018 2085
1165 67 ▶ return $self->_eval_expr($1, $vars) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->_eval_expr($1, $vars) ? : 14 53 67
1166 }
1167  
1168 # 'in' / 'not in' operator
1169 2018 ▶ if ($expr =~ /^(.+?)\s+not\s+in\s+(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^(.+?)\s+not\s+in\s+(.+)$/) 4 2014 2018
1170 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
1171 }
1172 2014 ▶ if ($expr =~ /^(.+?)\s+in\s+(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^(.+?)\s+in\s+(.+)$/) 19 1995 2014
1173 19 return $TESTS{in}->($self->_eval_expr($1, $vars), $self->_eval_expr($2, $vars));
1174 }
1175  
1176 # Boolean operators: and / or
1177 1995 ▶ if ($expr =~ /^(.+?)\s+or\s+(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^(.+?)\s+or\s+(.+)$/) 12 1983 1995
1178 12 my $a = $self->_eval_expr($1, $vars);
1179 12 ▶ return $a if $a;
Branch: 1 branch
BranchTrueFalseTotal
if $a 4 8 12
1180 8 return $self->_eval_expr($2, $vars);
1181 }
1182 1983 ▶ if ($expr =~ /^(.+?)\s+and\s+(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^(.+?)\s+and\s+(.+)$/) 17 1966 1983
1183 17 my $a = $self->_eval_expr($1, $vars);
1184 17 ▶ return $a ? $self->_eval_expr($2, $vars) : $a;
Branch: 1 branch
BranchTrueFalseTotal
$a ? : 10 7 17
1185 }
1186  
1187 # Filter pipeline: expr | filter1 | filter2(arg)
1188 # Must be checked before comparison operators so that ">" inside
1189 # filter arg strings (e.g. replace("-->","x")) is not misread.
1190 1966 ▶ if ($expr =~ /^(.+?)\s*\|\s*(\w+)(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^(.+?)\s*\|\s*(\w+)(.*)$/) 241 1725 1966
1191 241 my ($lhs, $fname, $rest) = ($1, $2, $3);
1192 241 my $val = $self->_eval_expr($lhs, $vars);
1193  
1194 241 my $fargs_str = '';
1195 241 my $remaining = '';
1196 241 ▶ if ($rest =~ /^\s*\(/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rest =~ /^\s*\(/) { } 102 139 241
elsif ($rest =~ /^\s*(\|.+)$/) { } 35 104 139
1197 # Find matching closing paren, respecting quotes and nesting
1198 102 my $depth2 = 0; my $in_sq2 = 0; my $in_dq2 = 0;
1199 102 my $found2 = -1;
1200 102 my $rs = $rest; $rs =~ s/^\s*//;
1201 102 for my $ci (0 .. length($rs) - 1) {
1202 577 my $c = substr($rs, $ci, 1);
1203 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
1204 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
1205 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
1206 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
1207 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
1208 240 ▶ if ($c eq '(') { $depth2++ }
Branch: 2 branches
BranchTrueFalseTotal
if ($c eq '(') { } 102 138 240
elsif ($c eq ')') { } 102 36 138
1209 102 ▶ elsif ($c eq ')') { $depth2--; if ($depth2 == 0) { $found2 = $ci; last } }
Branch: 1 branch
BranchTrueFalseTotal
if ($depth2 == 0) 102 0 102
1210 }
1211 }
1212 102 ▶ if ($found2 >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($found2 >= 0) 102 0 102
1213 102 $fargs_str = substr($rs, 1, $found2 - 1);
1214 102 $remaining = substr($rs, $found2 + 1);
1215 }
1216 }
1217 elsif ($rest =~ /^\s*(\|.+)$/) {
1218 35 $remaining = $1;
1219 }
1220  
1221 241 my @fargs;
1222 241 ▶ if ($fargs_str ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($fargs_str ne "") 102 139 241
1223 102 @fargs = map { $self->_eval_expr($_, $vars) } _split_args($fargs_str);
1224 }
1225  
1226 241 my $fn = $self->{_filters}{$fname};
1227 241 ▶ $val = $fn ? $fn->($val, @fargs) : $val;
Branch: 1 branch
BranchTrueFalseTotal
$fn ? : 241 0 241
1228  
1229 # Continue pipeline
1230 241 ▶ if ($remaining =~ s/^\s*\|\s*//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remaining =~ s/^\s*\|\s*//) 49 192 241
1231 49 return $self->_eval_expr("__PIPEVAL__ | $remaining", { %$vars, '__PIPEVAL__' => $val });
1232 }
1233 192 return $val;
1234 }
1235  
1236 # Comparison operators
1237 1725 ▶ if ($expr =~ /^(.+?)\s*(==|!=|>=|<=|>|<)\s*(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^(.+?)\s*(==|!=|>=|<=|>|<)\s*(.+)$/) 107 1618 1725
1238 107 my ($lhs, $op, $rhs) = ($1, $2, $3);
1239 107 my $l = $self->_eval_expr($lhs, $vars);
1240 107 my $r = $self->_eval_expr($rhs, $vars);
1241 107 ▶ return 0 unless defined $l && defined $r;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $l and defined $r 0 107 107
Condition33%1/3
defined $l and defined $r
!ll&&!rl&&r
00107
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
1242 107 ▶ my $numeric = ($l =~ /^-?(?:\d+\.?\d*|\.\d+)$/ && $r =~ /^-?(?:\d+\.?\d*|\.\d+)$/);
Condition66%2/3
$l =~ /^-?(?:\d+\.?\d*|\.\d+)$/ && $r =~ /^-?(?:\d+\.?\d*|\.\d+)$/
!ll&&!rl&&r
17090
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
1243 107 ▶ if ($op eq '==') { return $numeric ? ($l == $r ? 1 : 0) : ($l eq $r ? 1 : 0) }
Branch: 4 branches
BranchTrueFalseTotal
if ($op eq "==") 22 85 107
$numeric ? : 14 8 22
$l == $r ? : 6 8 14
$l eq $r ? : 4 4 8
1244 85 ▶ if ($op eq '!=') { return $numeric ? ($l != $r ? 1 : 0) : ($l ne $r ? 1 : 0) }
Branch: 4 branches
BranchTrueFalseTotal
if ($op eq "!=") 15 70 85
$numeric ? : 6 9 15
$l != $r ? : 2 4 6
$l ne $r ? : 6 3 9
1245 70 ▶ if ($op eq '>') { return $l > $r ? 1 : 0 }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq ">") 42 28 70
$l > $r ? : 22 20 42
1246 28 ▶ if ($op eq '<') { return $l < $r ? 1 : 0 }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq "<") 10 18 28
$l < $r ? : 4 6 10
1247 18 ▶ if ($op eq '>=') { return $l >= $r ? 1 : 0 }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq ">=") 12 6 18
$l >= $r ? : 4 8 12
1248 6 ▶ if ($op eq '<=') { return $l <= $r ? 1 : 0 }
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq "<=") 6 0 6
$l <= $r ? : 2 4 6
1249 }
1250  
1251 # Arithmetic: match ** and // before single-char operators
1252 1618 ▶ if ($expr =~ /^(.+?)\s*(\*\*|\/\/)\s*(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ m[^(.+?)\s*(\*\*|//)\s*(.+)$]) 4 1614 1618
1253 4 my ($lhs, $op, $rhs) = ($1, $2, $3);
1254 4 my $l = $self->_eval_expr($lhs, $vars);
1255 4 my $r = $self->_eval_expr($rhs, $vars);
1256 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
1257 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
1258 1 ▶ if ($op eq '**') { return $l ** $r }
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "**") 1 0 1
1259 }
1260 0 return undef;
1261 }
1262  
1263 # Arithmetic: scan for operator at depth-0 (respects parentheses)
1264 {
1265 1614 my $depth3 = 0; my $in_sq3 = 0; my $in_dq3 = 0;
1266 1614 my $op_pos = -1; my $op_char = '';
1267 1614 for my $ci (0 .. length($expr) - 1) {
1268 6561 my $c = substr($expr, $ci, 1);
1269 6561 ▶ 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 6559 6561
Condition100%6/6
not $in_sq3 || $in_dq3 and $c eq "'"
!ll&&!rl&&r
7264872
$in_sq3 || $in_dq3
l!l&&r!l&&!r
7656489
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
1270 6559 ▶ if ($in_sq3 && $c eq "'") { $in_sq3 = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($in_sq3 and $c eq "'") 2 6557 6559
Condition100%3/3
$in_sq3 and $c eq "'"
!ll&&!rl&&r
655252
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
1271 6557 ▶ 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 6531 6557
Condition100%6/6
not $in_sq3 || $in_dq3 and $c eq "\""
!ll&&!rl&&r
70646126
$in_sq3 || $in_dq3
l!l&&r!l&&!r
5656487
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
1272 6531 ▶ if ($in_dq3 && $c eq '"') { $in_dq3 = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($in_dq3 and $c eq "\"") 26 6505 6531
Condition100%3/3
$in_dq3 and $c eq "\""
!ll&&!rl&&r
64663926
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
1273 6505 ▶ next if $in_sq3 || $in_dq3;
Branch: 1 branch
BranchTrueFalseTotal
if $in_sq3 or $in_dq3 44 6461 6505
Condition100%3/3
$in_sq3 or $in_dq3
l!l&&r!l&&!r
5396461
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
1274 6461 ▶ if ($c eq '(' || $c eq '[' || $c eq '{') { $depth3++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "(" or $c eq "[" or $c eq "{") 82 6379 6461
Condition100%6/6
$c eq "(" or $c eq "[" or $c eq "{"
l!l&&r!l&&!r
8116379
$c eq "(" or $c eq "["
l!l&&r!l&&!r
55266380
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
1275 6379 ▶ if ($c eq ')' || $c eq ']' || $c eq '}') { $depth3--; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq ")" or $c eq "]" or $c eq "}") 78 6301 6379
Condition100%6/6
$c eq ")" or $c eq "]" or $c eq "}"
l!l&&r!l&&!r
7716301
$c eq ")" or $c eq "]"
l!l&&r!l&&!r
51266302
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
1276 6301 ▶ next if $depth3 != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $depth3 != 0 499 5802 6301
1277 5802 ▶ if ($c =~ /^[+\-*\/%~]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c =~ m[^[+\-*/%~]$]) 59 5743 5802
1278 59 ▶ next if $ci == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $ci == 0 1 58 59
1279 58 my $prev = substr($expr, $ci - 1, 1);
1280 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
1281 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
1282 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
1283 58 $op_pos = $ci; $op_char = $c;
1284 58 last;
1285 }
1286 }
1287 1614 ▶ if ($op_pos > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op_pos > 0) 58 1556 1614
1288 58 my $lhs = substr($expr, 0, $op_pos);
1289 58 my $rhs = substr($expr, $op_pos + 1);
1290 58 $lhs =~ s/\s+$//; $rhs =~ s/^\s+//;
1291 58 my $l = $self->_eval_expr($lhs, $vars);
1292 58 my $r = $self->_eval_expr($rhs, $vars);
1293 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
1294 58 ▶ if ($op_char eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($op_char eq "+") 26 32 58
1295 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
1296 }
1297 32 ▶ if ($op_char eq '~') { return $l . $r }
Branch: 1 branch
BranchTrueFalseTotal
if ($op_char eq "~") 16 16 32
1298 16 ▶ if ($op_char eq '-') { return $l - $r }
Branch: 1 branch
BranchTrueFalseTotal
if ($op_char eq "-") 2 14 16
1299 14 ▶ if ($op_char eq '*') { return $l * $r }
Branch: 1 branch
BranchTrueFalseTotal
if ($op_char eq "*") 12 2 14
1300 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
1301 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
1302 }
1303 }
1304 }
1305  
1306 # Parenthesised expression -- must be before is/is not so that
1307 # (expr is test) does not mis-parse the leading "(" as part of lhs.
1308 1556 ▶ if ($expr =~ /^\((.+)\)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^\((.+)\)$/) 35 1521 1556
1309 35 return $self->_eval_expr($1, $vars);
1310 }
1311  
1312 # is / is not test
1313 1521 ▶ 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 1512 1521
1314 9 my ($lhs, $test, $arg) = ($1, $2, $3);
1315 9 my $val = $self->_eval_expr($lhs, $vars);
1316 9 ▶ my $targ = defined $arg ? $self->_eval_expr($arg, $vars) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $arg ? : 0 9 9
1317 9 my $fn = $self->{_tests}{$test};
1318 9 ▶ return $fn ? ($fn->($val, $targ) ? 0 : 1) : 1;
Branch: 2 branches
BranchTrueFalseTotal
$fn ? : 9 0 9
&$fn($val, $targ) ? : 2 7 9
1319 }
1320 1512 ▶ if ($expr =~ /^(.+?)\s+is\s+(\w+)(?:\s+(.+))?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^(.+?)\s+is\s+(\w+)(?:\s+(.+))?$/) 134 1378 1512
1321 134 my ($lhs, $test, $arg) = ($1, $2, $3);
1322 134 my $val = $self->_eval_expr($lhs, $vars);
1323 134 ▶ my $targ = defined $arg ? $self->_eval_expr($arg, $vars) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $arg ? : 42 92 134
1324 134 my $fn = $self->{_tests}{$test};
1325 134 ▶ return $fn ? ($fn->($val, $targ) ? 1 : 0) : 0;
Branch: 2 branches
BranchTrueFalseTotal
$fn ? : 134 0 134
&$fn($val, $targ) ? : 63 71 134
1326 }
1327  
1328 # Attribute access: obj.attr or obj["key"] or obj['key']
1329 1378 ▶ if ($expr =~ /^(.+?)\.(\w+)(?:\(([^)]*)\))?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^(.+?)\.(\w+)(?:\(([^)]*)\))?$/) 157 1221 1378
1330 157 my ($obj_expr, $attr, $call_args) = ($1, $2, $3);
1331 157 my $obj = $self->_eval_expr($obj_expr, $vars);
1332 157 ▶ if (defined $call_args) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $call_args) 0 157 157
1333 # Method call (filters on object)
1334 0 my $fn = $self->{_filters}{$attr};
1335 0 my @args = map { $self->_eval_expr($_, $vars) } _split_args($call_args);
1336 0 ▶ return $fn ? $fn->($obj, @args) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$fn ? : 0 0 0
1337 }
1338 157 return _get_attr($obj, $attr);
1339 }
1340  
1341 1221 ▶ if ($expr =~ /^(.+?)\[["'](\w+)["']\]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^(.+?)\[["'](\w+)["']\]$/) 5 1216 1221
1342 5 my ($obj_expr, $key) = ($1, $2);
1343 5 my $obj = $self->_eval_expr($obj_expr, $vars);
1344 5 return _get_attr($obj, $key);
1345 }
1346  
1347 1216 ▶ if ($expr =~ /^(.+?)\[(-?\d+)\]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^(.+?)\[(-?\d+)\]$/) 12 1204 1216
1348 12 my ($obj_expr, $idx) = ($1, $2);
1349 12 my $obj = $self->_eval_expr($obj_expr, $vars);
1350 12 ▶ return ref($obj) eq 'ARRAY' ? $obj->[$idx] : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $obj eq 'ARRAY' ? : 12 0 12
1351 }
1352  
1353 # Slice: list[start:end]
1354 1204 ▶ if ($expr =~ /^(.+?)\[(-?\d*):(-?\d*)\]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^(.+?)\[(-?\d*):(-?\d*)\]$/) 5 1199 1204
1355 5 my ($obj_expr, $s, $e) = ($1, $2, $3);
1356 5 my $obj = $self->_eval_expr($obj_expr, $vars);
1357 5 ▶ return undef unless ref($obj) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj eq "ARRAY" 0 5 5
1358 5 my $len = scalar @$obj;
1359 5 ▶ my $si = ($s ne '') ? int($s) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$s ne '' ? : 4 1 5
1360 5 ▶ my $ei = ($e ne '') ? int($e) : $len;
Branch: 1 branch
BranchTrueFalseTotal
$e ne '' ? : 4 1 5
1361 5 ▶ $si += $len if $si < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $si < 0 0 5 5
1362 5 ▶ $ei += $len if $ei < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $ei < 0 1 4 5
1363 5 ▶ $si = 0 if $si < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $si < 0 0 5 5
1364 5 ▶ $ei = $len if $ei > $len;
Branch: 1 branch
BranchTrueFalseTotal
if $ei > $len 0 5 5
1365 5 return [ @{$obj}[$si .. $ei - 1] ];
1366 }
1367  
1368 # Function/macro call: name(args)
1369 1199 ▶ if ($expr =~ /^(\w+)\s*\(([^)]*)\)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^(\w+)\s*\(([^)]*)\)$/) 11 1188 1199
1370 11 my ($fname, $argstr) = ($1, $2);
1371 # Macro call
1372 11 ▶ if (exists $self->{_macros}{$fname}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_macros"}{$fname}) 11 0 11
1373 11 return $self->_call_macro($fname, $argstr, $vars, '<expr>');
1374 }
1375 # Built-in functions
1376 0 ▶ if ($fname eq 'range') {
Branch: 1 branch
BranchTrueFalseTotal
if ($fname eq "range") 0 0 0
1377 0 return $self->_eval_range($argstr, $vars);
1378 }
1379 }
1380  
1381 # Variable lookup
1382 1188 ▶ if ($expr =~ /^(\w+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /^(\w+)$/) 1178 10 1188
1383 1178 ▶ return exists $vars->{$expr} ? $vars->{$expr} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $vars->{$expr} ? : 1150 28 1178
1384 }
1385  
1386 10 return undef;
1387 }
1388  
1389 ###############################################################################
1390 # _eval_dict - Parse and evaluate a dict literal { key: val, ... }
1391 ###############################################################################
1392 1 ▶sub _eval_dict {
Subroutine _eval_dict: called CC=4 SCAR=14.0
1393 1 my ($self, $inner, $vars) = @_;
1394 1 my %h;
1395 # Simple k:v split (no nested dicts)
1396 1 for my $pair (_split_args($inner)) {
1397 1 ▶ if ($pair =~ /^(.+?)\s*:\s*(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pair =~ /^(.+?)\s*:\s*(.+)$/) 1 0 1
1398 1 my $k = $self->_eval_expr($1, $vars);
1399 1 my $v = $self->_eval_expr($2, $vars);
1400 1 ▶ $h{$k} = $v if defined $k;
Branch: 1 branch
BranchTrueFalseTotal
if defined $k 1 0 1
1401 }
1402 }
1403 1 return { %h };
1404 }
1405  
1406 ###############################################################################
1407 # _eval_range - Evaluate range(stop) / range(start, stop[, step])
1408 ###############################################################################
1409 22 ▶sub _eval_range {
Subroutine _eval_range: called CC=8 SCAR=20.8
1410 22 my ($self, $args_str, $vars) = @_;
1411 22 my @args = map { $self->_eval_expr($_, $vars) } _split_args($args_str);
1412 22 my ($start, $stop, $step);
1413 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
1414 4 elsif (@args == 2) { ($start, $stop, $step) = (int($args[0]), int($args[1]), 1) }
1415 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
1416 22 ▶ $step = 1 if $step == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $step == 0 0 22 22
1417 22 my @result;
1418 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
1419 2 ▶ else { for (my $n = $start; $n > $stop; $n += $step) { push @result, $n } }
Branch: 1 branch
BranchTrueFalseTotal
if ($n > $stop) 10 2 12
1420 22 return [ @result ];
1421 }
1422  
1423 ###############################################################################
1424 # _get_attr - Get attribute from hash or array
1425 ###############################################################################
1426 189 ▶sub _get_attr {
Subroutine _get_attr: called CC=6 SCAR=24.3
1427 189 my ($obj, $attr) = @_;
1428 189 ▶ return undef unless defined $obj;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $obj 0 189 189
1429 189 ▶ if (ref($obj) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $obj eq "HASH") 189 0 189
1430 189 ▶ return exists $obj->{$attr} ? $obj->{$attr} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $obj->{$attr} ? : 188 1 189
1431 }
1432 0 ▶ if (ref($obj) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $obj eq "ARRAY") 0 0 0
1433 0 ▶ return $attr =~ /^-?\d+$/ ? $obj->[$attr] : scalar @$obj;
Branch: 1 branch
BranchTrueFalseTotal
$attr =~ /^-?\d+$/ ? : 0 0 0
1434 }
1435 0 return undef;
1436 }
1437  
1438 ###############################################################################
1439 # _split_args - Split comma-separated arguments (respects nested parens/quotes)
1440 ###############################################################################
1441 156 ▶sub _split_args {
Subroutine _split_args: called CC=25 SCAR=32.3
1442 156 my ($str) = @_;
1443 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
1444 155 my @args;
1445 155 my $cur = '';
1446 155 my $depth = 0;
1447 155 my $in_sq = 0;
1448 155 my $in_dq = 0;
1449  
1450 155 for my $ch (split //, $str) {
1451 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
1452 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
1453 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
1454 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
1455 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
1456 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
1457 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
1458 ▶ 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
1459 42 $cur =~ s/^\s+|\s+$//g;
1460 42 push @args, $cur;
1461 42 $cur = '';
1462 42 next;
1463 }
1464 }
1465 433 $cur .= $ch;
1466 }
1467 155 $cur =~ s/^\s+|\s+$//g;
1468 155 ▶ push @args, $cur if $cur ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $cur ne "" 155 0 155
1469 155 return @args;
1470 }
1471  
1472 ###############################################################################
1473 # _html_escape - Escape HTML special characters
1474 ###############################################################################
1475 26 ▶sub _html_escape {
Subroutine _html_escape: called CC=2 SCAR=7.0
1476 26 my ($s) = @_;
1477 26 ▶ return '' unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 26 26
1478 26 $s =~ s/&/&amp;/g;
1479 26 $s =~ s/</&lt;/g;
1480 26 $s =~ s/>/&gt;/g;
1481 26 $s =~ s/"/&quot;/g;
1482 26 $s =~ s/'/&#39;/g;
1483 26 return $s;
1484 }
1485  
1486 ###############################################################################
1487 # _to_json - Minimal JSON serializer (no external dependency)
1488 ###############################################################################
1489 13 ▶sub _to_json {
Subroutine _to_json: called CC=9 SCAR=22.6
1490 13 my ($val) = @_;
1491 13 ▶ return 'null' unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 1 12 13
1492 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
1493 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
1494 12 ▶ if (ref($val) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq "ARRAY") 2 10 12
1495 2 return '[' . join(',', map { _to_json($_) } @$val) . ']';
1496 }
1497 10 ▶ if (ref($val) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq "HASH") 1 9 10
1498 return '{' . join(',', map {
1499 1 _to_json($_) . ':' . _to_json($val->{$_})
1500 } sort keys %$val) . '}';
1501 }
1502 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
1503 6 return $val;
1504 }
1505 3 $val =~ s/\\/\\\\/g;
1506 3 $val =~ s/"/\\"/g;
1507 3 $val =~ s/\n/\\n/g;
1508 3 $val =~ s/\r/\\r/g;
1509 3 $val =~ s/\t/\\t/g;
1510 3 return "\"$val\"";
1511 }
1512  
1513 ###############################################################################
1514 # Back to main package -- demo when run directly
1515 ###############################################################################
1516  
1517 # Run as script: perl lib/HP/Handy.pm
1518 12 ▶unless (caller) {
Branch: 1 branch
BranchTrueFalseTotal
unless (caller) 0 12 12
1519 0 my $tmpl = HP::Handy->new(auto_escape => 1);
1520  
1521 0 my $source = <<'TMPL';
1522 <!DOCTYPE html>
1523 <html>
1524 <head><meta charset="utf-8"><title>HP::Handy Demo</title>
1525 <style>body{font-family:sans-serif;max-width:600px;margin:40px auto;padding:0 20px}
1526 h1{color:#336699}table{border-collapse:collapse;width:100%}
1527 td { padding: 6px 10px; border: 1px solid #ccc }
1528 th { padding: 6px 10px; border: 1px solid #ccc }
1529 tr:nth-child(even) { background: #f8f8f8 }
1530 </style>
1531 </head>
1532 <body>
1533 <h1>HP::Handy Demo</h1>
1534 <p>Version: {{ version }}</p>
1535  
1536 <h2>Variable and Filter</h2>
1537 <p>Hello, {{ name | upper }}!</p>
1538 <p>Escaped: {{ snippet }}</p>
1539  
1540 <h2>For Loop</h2>
1541 <table>
1542 <tr><th>#</th><th>Item</th><th>First?</th><th>Last?</th></tr>
1543 {% for item in items %}
1544 <tr>
1545 <td>{{ loop.index }}</td>
1546 <td>{{ item }}</td>
1547 <td>{{ "yes" if loop.first else "no" }}</td>
1548 <td>{{ "yes" if loop.last else "no" }}</td>
1549 </tr>
1550 {% endfor %}
1551 </table>
1552  
1553 <h2>Conditional</h2>
1554 {% if score >= 90 %}
1555 <p>Grade: A</p>
1556 {% elif score >= 70 %}
1557 <p>Grade: B</p>
1558 {% else %}
1559 <p>Grade: C</p>
1560 {% endif %}
1561  
1562 <h2>Set and Default Filter</h2>
1563 {% set greeting = "Konnichiwa" %}
1564 <p>{{ greeting | default("Hello") }}</p>
1565 <p>Missing: {{ missing | default("(none)") }}</p>
1566 </body>
1567 </html>
1568 TMPL
1569  
1570 0 my $html = $tmpl->render_string($source, {
1571 version => $HP::Handy::VERSION,
1572 name => 'World',
1573 snippet => '<script>alert("xss")</script>',
1574 items => [ 'Perl', 'Python', 'Ruby', 'JavaScript' ],
1575 score => 85,
1576 });
1577  
1578 0 print $html;
1579 }
1580  
1581  
1582 ###############################################################################
1583 # HP::Handy::SafeString - trusted HTML string (bypasses auto_escape)
1584 ###############################################################################
1585 package HP::Handy::SafeString;
1586 use overload
1587 2 ▶ q("") => sub { ${$_[0]} },
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
1588 12 fallback => 1;
1589  
1590 package HP::Handy;
1591  
1592 12 1;
1593  
1594 __END__
« blib/lib/HP/Handy.pm ↑ Summary