Sluz.pm

filter: Statementstmt 88.3% 848 / 960 Branchbran 76.2% 354 / 464 Conditioncond 74.1% 149 / 201 MC/DCmcdc 54.3% 63 / 116 Subroutinesub 90.4% 57 / 63 Podpod 41.1% 7 / 17 totaltotal 81.1% 1478 / 1821 CC 330 SCAR 35.3
CC
330
Cov
83%
_foreach_block
86.5
_variable_block
84.9
_get_blocks
41.1
CRAP 34.1
↑ Summary example.pl »
1 # Copyright (C) 2025 Scott Baker <scott@perturb.org>
2 #
3 # This program is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <https://www.gnu.org/licenses/>.
15  
16 package Template::Sluz;
17  
18 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 10 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 10 ▶use File::Basename qw(dirname basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 10 ▶use autouse 'Carp' => qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 10 ▶use constant SLUZ_INLINE => 'INLINE_TEMPLATE';
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 10 our $VERSION = 'v0.9.6';
28  
29 ################################################################################
30 # Built-in Sluz functions that can be used in templates
31 ################################################################################
32  
33 3 ▶sub count {
Subroutine count: called CC=4 SCAR=22.9
Pod undocumented
34 3 my $v = shift;
35  
36 3 ▶ if (ref $v eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v eq "ARRAY") 3 0 3
37 3 return scalar @$v;
38 }
39  
40 0 ▶ if (ref $v eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v eq "HASH") 0 0 0
41 0 return scalar(keys %$v);
42 }
43  
44 0 ▶ if (defined $v) { return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v) 0 0 0
45  
46 0 return 0;
47 }
48  
49 4 ▶sub join {
Subroutine join: called CC=2 SCAR=6.9
Pod undocumented
50 4 my $arr = shift();
51 4 ▶ my $glue = shift() // ', ';
Condition100%2/2
shift() // ", "
l!l
31
MC/DC100%1/1
shift() // ", "
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
52  
53 4 return CORE::join($glue, @$arr);
54 }
55  
56 ################################################################################
57 ################################################################################
58  
59 11 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
60 11 my $class = shift;
61 11 my %args = @_;
62 my $self = {
63 version => $VERSION,
64 tpl_file => undef,
65 inc_tpl_file => undef,
66 debug => $args{debug} // 0,
67 11 ▶ auto_escape => $args{auto_escape} // 0,
Condition75%3/4
$args{"debug"} // 0
l!l
011
$args{"auto_escape"} // 0
l!l
29
MC/DC0%0/1
$args{"debug"} // 0
$args{"debug"}
MC/DC100%1/1
$args{"auto_escape"} // 0
$args{"auto_escape"}
Truth table50%1/2
A
A= $args{"debug"}
Aresult
11
00
Truth table100%2/2
A
A= $args{"auto_escape"}
Aresult
11
00
68 tpl_vars => {},
69 parent_tpl => undef,
70 var_prefix => 'sluz_pfx',
71 perl_file => undef,
72 perl_file_dir => undef,
73 fetch_called => 0,
74 char_pos => -1,
75 open_delim => '{',
76 close_delim => '}',
77 _sub_cache => {},
78 __S => {}, # Cached prefixed var hash used by _peval
79 _convert_cache => {}, # Cached _convert_vars results (avoids re-running regex on repeated expressions)
80 _blocks_cache => {}, # Cached _get_blocks results (avoids re-tokenizing if payloads in loops)
81 _if_rules_cache => {}, # Cached parsed {if} rules (avoids re-parsing same if block in loops)
82 _verified_sub_cache => {}, # Cached subs that succeeded once — skip eval/SIG overhead
83 _mod_cache => {}, # Cached resolved modifier coderefs keyed by func name
84 };
85  
86 11 bless $self, $class;
87 11 $self->_precompute_tags();
88 11 return $self;
89 }
90  
91 244 ▶sub assign {
Subroutine assign: called CC=6 SCAR=17.9
Pod documented
92 244 my $self = shift;
93  
94 244 my $pfx = $self->{var_prefix};
95  
96 # Accept either a hashref: assign($hash_ref)
97 244 ▶ if (@_ == 1 && ref $_[0] eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] eq 'HASH') { } 9 235 244
elsif (@_ % 2 == 0) { } 234 1 235
Condition100%3/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
23419
MC/DC100%2/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table100%3/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
98 9 my $h = shift;
99 9 @{$self->{tpl_vars}}{keys %$h} = values %$h;
100 9 for my $k (keys %$h) {
101 24 $self->{__S}{"${pfx}_$k"} = $h->{$k};
102 }
103  
104 # Or a key-value list: assign(name => 'Scott', age => 42)
105 } elsif (@_ % 2 == 0) {
106 234 my %h = @_;
107 234 @{$self->{tpl_vars}}{keys %h} = values %h;
108 234 for my $k (keys %h) {
109 258 $self->{__S}{"${pfx}_$k"} = $h{$k};
110 }
111 } else {
112 1 $self->_error_out("Invalid assign. Must be a key/value or hash", 18956);
113 }
114 }
115  
116 3 ▶sub fetch {
Subroutine fetch: called CC=5 SCAR=16.1
Pod documented
117 3 my $self = shift;
118 3 ▶ my $tpl_file = shift // SLUZ_INLINE;
Condition50%1/2
shift() // "INLINE_TEMPLATE"
l!l
30
MC/DC0%0/1
shift() // "INLINE_TEMPLATE"
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
119 3 my $parent = shift;
120  
121 3 ▶ if (!$self->{perl_file}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"perl_file"}) 1 2 3
122 1 $self->{perl_file} = $self->_get_perl_file;
123 1 $self->{perl_file_dir} = dirname($self->{perl_file});
124 }
125  
126 3 my $parent_tpl;
127 3 ▶ if (defined $parent) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $parent) { } 1 2 3
128 1 $parent_tpl = $parent;
129 } else {
130 2 $parent_tpl = $self->{parent_tpl};
131 }
132  
133 3 ▶ if ($parent_tpl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent_tpl) 2 1 3
134 2 $self->assign('__CHILD_TPL', $tpl_file);
135 2 $tpl_file = $parent_tpl;
136 }
137  
138 3 my $str = $self->_get_tpl_content($tpl_file);
139 3 my @blocks = $self->_get_blocks($str);
140 3 my $html = $self->_process_blocks(\@blocks);
141  
142 3 $self->{fetch_called} = 1;
143 3 return $html;
144 }
145  
146 0 ▶sub parse {
Subroutine parse: not called CC=1 SCAR=6.9
Pod undocumented
147 0 my $self = shift;
148 0 return $self->fetch(@_);
149 }
150  
151 0 ▶sub display {
Subroutine display: not called CC=1 SCAR=6.9
Pod undocumented
152 0 my $self = shift;
153 0 print $self->fetch(@_);
154 }
155  
156 # Parse a string instead of a file
157 220 ▶sub parse_string {
Subroutine parse_string: called CC=2 SCAR=7.0
Pod documented
158 220 my $self = shift;
159 220 ▶ my $tpl_str = shift // '';
Condition50%1/2
shift() // ""
l!l
2200
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
160 220 my @blocks = $self->_get_blocks($tpl_str);
161  
162 220 return $self->_process_blocks(\@blocks);
163 }
164  
165 # Getter/setter for parent TPL
166 2 ▶sub parent_tpl {
Subroutine parent_tpl: called CC=2 SCAR=7.0
Pod undocumented
167 2 my $self = shift;
168  
169 2 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 0 2
170 2 $self->{parent_tpl} = shift;
171 }
172  
173 2 return $self->{parent_tpl};
174 }
175  
176 1 ▶sub set_delimiters {
Subroutine set_delimiters: called CC=6 SCAR=19.8
Pod documented
177 1 my $self = shift;
178 1 my $open = shift;
179 1 my $close = shift;
180  
181 1 ▶ if (!defined $open || !defined $close) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $open and defined $close) 0 1 1
Condition33%1/3
defined $open and defined $close
!ll&&!rl&&r
001
MC/DC0%0/2
defined $open and defined $close
defined $open defined $close
Truth table33%1/3
A and B
A= defined $openB= defined $close
ABresult
0X0
100
111
182 0 $self->_error_out("set_delimiters requires both open and close delimiter arguments", 51234);
183 }
184  
185 1 ▶ if (length($open) != 1 || length($close) != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $open != 1 or length $close != 1) 0 1 1
Condition33%1/3
length $open != 1 or length $close != 1
l!l&&r!l&&!r
001
MC/DC0%0/2
length $open != 1 or length $close != 1
length $open != 1 length $close != 1
Truth table33%1/3
A or B
A= length $open != 1B= length $close != 1
ABresult
1X1
011
000
186 0 $self->_error_out("Delimiters must be single characters", 51235);
187 }
188  
189 1 ▶ if ($open eq $close) {
Branch: 1 branch
BranchTrueFalseTotal
if ($open eq $close) 0 1 1
190 0 $self->_error_out("Open and close delimiters must be different characters", 51236);
191 }
192  
193 1 $self->{open_delim} = $open;
194 1 $self->{close_delim} = $close;
195  
196 1 $self->_precompute_tags();
197  
198 # Clear all caches since results are delimiter-dependent
199 1 $self->{_blocks_cache} = {};
200 1 $self->{_if_rules_cache} = {};
201 1 $self->{_convert_cache} = {};
202 1 $self->{_sub_cache} = {};
203 1 $self->{_verified_sub_cache} = {};
204 1 $self->{_mod_cache} = {};
205  
206 1 return;
207 }
208  
209 # Dive down an array or hashref using our dotted syntax
210 80 ▶sub array_dive {
Subroutine array_dive: called CC=11 SCAR=26.4
Pod undocumented
211 80 my $self = shift;
212 80 my $needle = shift;
213 80 my $haystack = shift;
214  
215 80 ▶ if (!defined $needle || !defined $haystack) { return undef }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $needle and defined $haystack) 0 80 80
Condition33%1/3
defined $needle and defined $haystack
!ll&&!rl&&r
0080
MC/DC0%0/2
defined $needle and defined $haystack
defined $needle defined $haystack
Truth table33%1/3
A and B
A= defined $needleB= defined $haystack
ABresult
0X0
100
111
216  
217 # Quick path: needle is a direct key in the hash
218 80 ▶ if (exists $haystack->{$needle}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $haystack->{$needle}) 50 30 80
219 50 return $haystack->{$needle};
220 }
221  
222 # Walk dotted path (e.g. "user.address.city") through nested structures
223 30 my @parts = split /\./, $needle;
224 30 my $arr = $haystack;
225  
226 30 for my $elem (@parts) {
227 53 ▶ if (!defined $arr) { return undef }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $arr) 0 53 53
228 53 ▶ if (ref $arr eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $arr eq 'ARRAY') { } 10 43 53
elsif (ref $arr eq 'HASH') { } 43 0 43
229 10 ▶ if (!($elem =~ /^\d+$/ && $elem < @$arr)) { return undef }
Branch: 1 branch
BranchTrueFalseTotal
unless ($elem =~ /^\d+$/u and $elem < @$arr) 0 10 10
Condition33%1/3
$elem =~ /^\d+$/u and $elem < @$arr
!ll&&!rl&&r
0010
MC/DC0%0/2
$elem =~ /^\d+$/u and $elem < @$arr
$elem =~ /^\d+$/u $elem < @$arr
Truth table33%1/3
A and B
A= $elem =~ /^\d+$/uB= $elem < @$arr
ABresult
0X0
100
111
230 10 $arr = $arr->[$elem];
231 } elsif (ref $arr eq 'HASH') {
232 43 ▶ if (!exists $arr->{$elem}) { return undef }
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $arr->{$elem}) 9 34 43
233 34 $arr = $arr->{$elem};
234 } else {
235 0 return undef;
236 }
237 }
238 21 return $arr;
239 }
240  
241 # HTML-escape a string for safe output. Encodes & < > " ' to entities.
242 # Usable as a modifier: {$var|escape} or as a callable: {escape($var)}
243 32 ▶sub escape {
Subroutine escape: called CC=4 SCAR=14.1
Pod documented
244 32 ▶ my $str = shift // '';
Condition50%1/2
shift() // ""
l!l
320
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
245  
246 32 ▶ if (ref $str eq 'ARRAY') { return 'ARRAY' }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $str eq "ARRAY") 1 31 32
247 31 ▶ if (ref $str eq 'HASH') { return 'HASH' }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $str eq "HASH") 0 31 31
248  
249 31 $str =~ s/&/&amp;/g;
250 31 $str =~ s/</&lt;/g;
251 31 $str =~ s/>/&gt;/g;
252 31 $str =~ s/"/&quot;/g;
253 31 $str =~ s/'/&#x27;/g;
254  
255 31 return $str;
256 }
257  
258 # Bypass auto-escaping when auto_escape is on: {$var|noescape}
259 7 ▶sub noescape {
Subroutine noescape: called CC=1 SCAR=0.0
Pod documented
260 7 return shift;
261 }
262  
263 # Apply auto-escaping if enabled, otherwise return value unchanged.
264 # Ref types (ARRAY/HASH) pass through unescaped.
265 0 ▶sub _esc {
Subroutine _esc: not called CC=3 SCAR=24.8
266 0 my ($self, $val) = @_;
267  
268 0 ▶ if (!$self->{auto_escape}) { return $val }
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"auto_escape"}) 0 0 0
269 0 ▶ if (ref $val) { return $val }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val) 0 0 0
270  
271 0 return escape($val);
272 }
273  
274 129 ▶sub ltrim_one {
Subroutine ltrim_one: called CC=4 SCAR=13.9
Pod undocumented
275 129 my $self = shift;
276 129 ▶ my $str = shift // '';
Condition50%1/2
shift() // ""
l!l
1290
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
277 129 my $char = shift;
278  
279 129 ▶ if (length $str && substr($str, 0, 1) eq $char) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $str and substr($str, 0, 1) eq $char) 15 114 129
Condition100%3/3
length $str and substr($str, 0, 1) eq $char
!ll&&!rl&&r
111315
MC/DC100%2/2
length $str and substr($str, 0, 1) eq $char
length $str substr($str, 0, 1) eq $char
Truth table100%3/3
A and B
A= length $strB= substr($str, 0, 1) eq $char
ABresult
0X0
100
111
280 15 return substr($str, 1);
281 }
282  
283 114 return $str;
284 }
285  
286 23 ▶sub find_ending_tag {
Subroutine find_ending_tag: called CC=7 SCAR=19.8
Pod undocumented
287 23 my $self = shift;
288 23 ▶ my $haystack = shift // '';
Condition50%1/2
shift() // ""
l!l
230
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
289 23 my $open_tag = shift;
290 23 my $close_tag = shift;
291  
292 # Find the first close tag; if there's only one open tag before it, we're done
293 23 my $pos = index($haystack, $close_tag);
294 23 ▶ if ($pos < 0) { return undef }
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < 0) 0 23 23
295  
296 23 my $substr = substr($haystack, 0, $pos);
297 23 my $open_count = () = $substr =~ /\Q$open_tag\E/g;
298 23 ▶ if ($open_count == 1) { return $pos }
Branch: 1 branch
BranchTrueFalseTotal
if ($open_count == 1) 18 5 23
299  
300 # Nested tags: scan forward through subsequent close tags until
301 # open/close counts balance (max 5 nesting levels)
302 5 my $close_len = length $close_tag;
303 5 my $offset = $pos + $close_len;
304  
305 5 for (0 .. 4) {
306 13 $pos = index($haystack, $close_tag, $offset);
307 13 ▶ if ($pos < 0) { return undef }
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < 0) 0 13 13
308  
309 13 $substr = substr($haystack, 0, $pos + 2);
310 13 $open_count = () = $substr =~ /\Q$open_tag\E/g;
311 13 my $close_count = () = $substr =~ /\Q$close_tag\E/g;
312 13 ▶ if ($open_count == $close_count) { return $pos }
Branch: 1 branch
BranchTrueFalseTotal
if ($open_count == $close_count) 5 8 13
313  
314 8 $offset = $pos + $close_len;
315 }
316  
317 0 return undef;
318 }
319  
320 24 ▶sub get_tokens {
Subroutine get_tokens: called CC=3 SCAR=11.1
Pod undocumented
321 24 my $self = shift;
322 24 ▶ my $str = shift // '';
Condition50%1/2
shift() // ""
l!l
240
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
323 24 my $o = quotemeta($self->{open_delim});
324 24 my $c = quotemeta($self->{close_delim});
325 24 my @tokens = split /($o[^$c]+$c)/, $str;
326 24 ▶ @tokens = grep { defined && length } @tokens;
Condition66%2/3
defined $_ && length $_
!ll&&!rl&&r
036160
MC/DC50%1/2
defined $_ && length $_
defined $_ length $_
Truth table66%2/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
327 24 return @tokens;
328 }
329  
330 195 ▶sub is_if_token {
Subroutine is_if_token: called CC=6 SCAR=17.9
Pod undocumented
331 195 my $self = shift;
332 195 ▶ my $str = shift // '';
Condition50%1/2
shift() // ""
l!l
1950
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
333 195 ▶ if ($str eq $self->{_tag_else}) { return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($str eq $self->{"_tag_else"}) 42 153 195
334 153 ▶ if ($str eq $self->{_tag_if_close}) { return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($str eq $self->{"_tag_if_close"}) 30 123 153
335 123 ▶ if (index($str, $self->{_tag_if}) == 0 || index($str, $self->{_tag_elseif}) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($str, $self->{"_tag_if"}) == 0 or index($str, $self->{"_tag_elseif"}) == 0) 70 53 123
Condition100%3/3
index($str, $self->{"_tag_if"}) == 0 or index($str, $self->{"_tag_elseif"}) == 0
l!l&&r!l&&!r
462453
MC/DC100%2/2
index($str, $self->{"_tag_if"}) == 0 or index($str, $self->{"_tag_elseif"}) == 0
index($str, $self->{"_tag_if"}) == 0 index($str, $self->{"_tag_elseif"}) == 0
Truth table100%3/3
A or B
A= index($str, $self->{"_tag_if"}) == 0B= index($str, $self->{"_tag_elseif"}) == 0
ABresult
1X1
011
000
336 70 my $inner = substr($str, length($self->{open_delim}));
337 70 $inner =~ s/^\S+\s+//; # strip 'if ' or 'elseif '
338 70 $inner =~ s/\Q$self->{close_delim}\E$//;
339 70 return $inner;
340 }
341 53 return '';
342 }
343  
344 # -------------------------------------------------------------------
345 # Private methods
346 # -------------------------------------------------------------------
347  
348 12 ▶sub _precompute_tags {
Subroutine _precompute_tags: called CC=1 SCAR=0.0
349 12 my $self = shift;
350 12 my $o = $self->{open_delim};
351 12 my $c = $self->{close_delim};
352  
353 # Tag strings
354 12 $self->{_tag_if} = "${o}if ";
355 12 $self->{_tag_if_close} = "${o}/if${c}";
356 12 $self->{_tag_else} = "${o}else${c}";
357 12 $self->{_tag_elseif} = "${o}elseif ";
358 12 $self->{_tag_foreach} = "${o}foreach ";
359 12 $self->{_tag_foreach_close} = "${o}/foreach${c}";
360 12 $self->{_tag_include} = "${o}include ";
361 12 $self->{_tag_literal} = "${o}literal${c}";
362 12 $self->{_tag_literal_close} = "${o}/literal${c}";
363 12 $self->{_tag_comment_open} = "${o}*";
364 12 $self->{_tag_comment_close} = "*${c}";
365  
366 # Precomputed tag lengths (avoids repeated length() calls in hot loops)
367 12 $self->{_tag_if_len} = length($self->{_tag_if});
368 12 $self->{_tag_if_close_len} = length($self->{_tag_if_close});
369 12 $self->{_tag_foreach_len} = length($self->{_tag_foreach});
370 12 $self->{_tag_include_len} = length($self->{_tag_include});
371 12 $self->{_tag_literal_len} = length($self->{_tag_literal});
372 12 $self->{_tag_literal_close_len} = length($self->{_tag_literal_close});
373  
374 # Precomputed quotemeta values (avoids per-call quotemeta in _get_blocks)
375 12 $self->{_od_qr} = quotemeta($o);
376 12 $self->{_cd_qr} = quotemeta($c);
377  
378 # Precompiled space-guard regex (avoids per-open-delimiter regex compile)
379 12 my $od_qr = $self->{_od_qr};
380 12 my $cd_qr = $self->{_cd_qr};
381 12 $self->{_space_guard_re} = qr/\s[$od_qr$cd_qr]\s/;
382  
383 # Precompiled variable regex: {$var} or {$var.dot.path}
384 12 $self->{_re_var_simple} = qr/^\Q$o\E\$(\w[\w.]*)\Q$c\E$/;
385 12 $self->{_re_var_full} = qr/^\Q$o\E\$([\w|.'";\t :,!@#%^&*?_\/\\\-]+)\Q$c\E$/;
386  
387 # Precompiled foreach regex
388 12 $self->{_re_foreach} = qr/^\Q$o\Eforeach (\$\w[\w.]*) as \$(\w+)(?: => \$(\w+))?\Q$c\E(.+)\Q$o\E\/foreach\Q$c\E$/s;
389  
390 # Precompiled literal regex
391 12 $self->{_re_literal} = qr/^\Q$o\Eliteral\Q$c\E(.+)\Q$o\E\/literal\Q$c\E$/s;
392  
393 # Precompiled expression catch-all regex
394 12 $self->{_re_expr} = qr/^\Q$o\E(.+)\Q$c\E$/s;
395  
396 # Precompiled simple if regex (no else/elseif)
397 12 $self->{_re_if_simple} = qr/\Q$o\Eif (.+?)\Q$c\E(.+)\Q$o\E\/if\Q$c\E/s;
398  
399 # Precomputed ord values for fast single-character delimiter checks
400 12 $self->{_od_ord} = ord($o);
401 12 $self->{_cd_ord} = ord($c);
402 12 $self->{_dollar_ord} = ord('$');
403  
404 # Precompiled modifier-split regexes (avoids per-modifier-call recompile)
405 12 $self->{_pipe_re} = qr/\|(?![^"]*"(?:(?:[^"]*"){2})*[^"]*$)(?![^']*'(?:(?:[^']*'){2})*[^']*$)/;
406 12 $self->{_comma_re} = qr/,(?=(?:[^"]*"[^"]*")*[^"]*$)(?=(?:[^']*'[^']*')*[^']*$)/;
407  
408 12 return;
409 }
410  
411 1 ▶sub _get_perl_file {
Subroutine _get_perl_file: called CC=3 SCAR=11.0
412 1 my $self = shift;
413 1 my $i = 0;
414 1 my $file;
415  
416 1 ▶ while (caller($i)) {
Branch: 1 branch
BranchTrueFalseTotal
if (caller $i) 3 1 4
417 3 $file = (caller($i))[1];
418 3 $i++;
419 }
420  
421 1 ▶ return $file || __FILE__;
Condition50%1/2
$file || __FILE__
l!l
10
MC/DC0%0/1
$file || __FILE__
$file
Truth table50%1/2
A
A= $file
Aresult
11
00
422 }
423  
424 4 ▶sub _get_tpl_content {
Subroutine _get_tpl_content: called CC=11 SCAR=31.6
425 4 my $self = shift;
426 4 ▶ my $tpl_file = shift // '';
Condition50%1/2
shift() // ""
l!l
40
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
427 4 $self->{tpl_file} = $tpl_file;
428 4 my $tf = $tpl_file;
429  
430 4 ▶ if ($self->{perl_file_dir}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"perl_file_dir"}) 4 0 4
431 4 $tf = $self->{perl_file_dir} . "/$tf";
432 }
433  
434 4 ▶ if (!length($tpl_file)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $tpl_file) 0 4 4
435 0 $self->_error_out("Template file name is empty", 86801);
436 }
437  
438 4 ▶ if ($tpl_file eq SLUZ_INLINE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tpl_file eq 'INLINE_TEMPLATE') { } 0 4 4
439 0 my ($c, $line_offset) = $self->_get_inline_content($self->{perl_file});
440 0 ▶ if (defined $c) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $c) 0 0 0
441 0 $self->{tpl_file_display} = $self->{perl_file};
442 0 $self->{tpl_line_offset} = $line_offset;
443 0 return $c;
444 }
445 0 delete $self->{tpl_file_display};
446 0 delete $self->{tpl_line_offset};
447 0 return '';
448 } else {
449 4 delete $self->{tpl_file_display};
450 4 delete $self->{tpl_line_offset};
451 }
452  
453 4 ▶ if ($tf && !-r $tf) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tf and not -r $tf) 0 4 4
Condition33%1/3
$tf and not -r $tf
!ll&&!rl&&r
040
MC/DC0%0/2
$tf and not -r $tf
$tf not -r $tf
Truth table33%1/3
A and not(B)
A= $tfB= not -r $tf
ABresult
0X0
100
111
454 0 $self->_error_out("Unable to load template file <code>$tf</code>", 42280);
455 }
456  
457 4 ▶ if ($tf) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tf) 4 0 4
458 4 local $/;
459 4 ▶ open my $fh, '<', $tf or $self->_error_out("Cannot open <code>$tf</code>: $!", 13983);
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $tf or $self->_error_out("Cannot open <code>$tf</code>: $!", 13983) 4 0 4
460 4 my $str = <$fh>;
461 4 close $fh;
462 4 ▶ return $str // '';
Condition50%1/2
$str // ""
l!l
40
MC/DC0%0/1
$str // ""
$str
Truth table50%1/2
A
A= $str
Aresult
11
00
463 }
464  
465 0 return '';
466 }
467  
468 0 ▶sub _get_inline_content {
Subroutine _get_inline_content: not called CC=3 SCAR=24.8
469 0 my $self = shift;
470 0 my $file = shift;
471 0 local $/;
472 0 ▶ open my $fh, '<', $file or return undef;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or return undef 0 0 0
473 0 my $str = <$fh>;
474 0 close $fh;
475 0 my $idx = index($str, '__DATA__');
476 0 ▶ if ($idx < 0) { return undef }
Branch: 1 branch
BranchTrueFalseTotal
if ($idx < 0) 0 0 0
477 0 my $before = substr($str, 0, $idx + 9);
478 0 my $line_offset = $before =~ tr/\n//;
479 0 return (substr($str, $idx + 9), $line_offset);
480 }
481  
482 # -------------------------------------------------------------------
483 # Tokenizer
484 # -------------------------------------------------------------------
485  
486 331 ▶sub _get_blocks {
Subroutine _get_blocks: called CC=41 SCAR=37.2
487 331 my $self = shift;
488 331 ▶ my $str = shift // '';
Condition50%1/2
shift() // ""
l!l
3310
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
489  
490 # Check blocks cache — avoids re-tokenizing the same payload string
491 # (e.g. an {if} payload re-parsed on every iteration of a {foreach} loop)
492 331 ▶ if (exists $self->{_blocks_cache}{$str}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_blocks_cache"}{$str}) 15 316 331
493 15 return @{$self->{_blocks_cache}{$str}};
494 }
495  
496 316 my $od = $self->{open_delim};
497 316 my $cd = $self->{close_delim};
498 316 my $tag_if = $self->{_tag_if};
499 316 my $tag_foreach = $self->{_tag_foreach};
500 316 my $tag_literal = $self->{_tag_literal};
501 316 my $slen = length $str;
502 316 my $start = 0;
503 316 my $i;
504 my @blocks;
505  
506 316 my $z = index($str, $od);
507 316 ▶ if ($z < 0) { $z = $slen }
Branch: 1 branch
BranchTrueFalseTotal
if ($z < 0) 44 272 316
508  
509 316 ▶ for ($i = $z; $i < $slen; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $slen) 851 316 1167
510 851 my $char = substr($str, $i, 1);
511 851 my $is_open = $char eq $od;
512 851 my $is_closed = $char eq $cd;
513  
514 851 ▶ if (!$is_open && !$is_closed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_open or $is_closed) 293 558 851
Condition100%3/3
$is_open or $is_closed
l!l&&r!l&&!r
288270293
MC/DC100%2/2
$is_open or $is_closed
$is_open $is_closed
Truth table100%3/3
A or B
A= $is_openB= $is_closed
ABresult
1X1
011
000
515 293 my $next_open = index($str, $od, $i);
516 293 ▶ if ($next_open < 0) { $next_open = $slen }
Branch: 1 branch
BranchTrueFalseTotal
if ($next_open < 0) 162 131 293
517 293 my $next_close = index($str, $cd, $i);
518 293 ▶ if ($next_close < 0) { $next_close = $slen }
Branch: 1 branch
BranchTrueFalseTotal
if ($next_close < 0) 17 276 293
519 293 ▶ if ($next_open < $next_close) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next_open < $next_close) { } 7 286 293
520 7 $i = $next_open - 1;
521 } else {
522 286 $i = $next_close - 1;
523 }
524 293 next;
525 }
526  
527 558 my $has_len = $start != $i;
528 558 my $is_comment = 0;
529  
530 558 ▶ if ($is_open) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_open) 288 270 558
531 288 my $prev_c;
532 288 ▶ if ($i > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0) { } 34 254 288
533 34 $prev_c = substr($str, $i - 1, 1);
534 } else {
535 254 $prev_c = ' ';
536 }
537 288 my $next_c;
538 288 ▶ if ($i + 1 < $slen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i + 1 < $slen) { } 288 0 288
539 288 $next_c = substr($str, $i + 1, 1);
540 } else {
541 0 $next_c = ' ';
542 }
543 288 my $chk = $prev_c . $char . $next_c;
544 288 ▶ if ($chk =~ $self->{_space_guard_re}) { $is_open = 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($chk =~ /$self->{"_space_guard_re"}/u) 2 286 288
545 288 ▶ if ($next_c eq '*') { $is_comment = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($next_c eq "*") 23 265 288
546 }
547  
548 558 ▶ if ($is_open && $has_len) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_open and $has_len) { } 30 528 558
elsif ($is_closed) { } 270 258 528
Condition100%3/3
$is_open and $has_len
!ll&&!rl&&r
27225630
MC/DC100%2/2
$is_open and $has_len
$is_open $has_len
Truth table100%3/3
A and B
A= $is_openB= $has_len
ABresult
0X0
100
111
549 30 push @blocks, [substr($str, $start, $i - $start), $i];
550 30 $start = $i;
551 } elsif ($is_closed) {
552 270 my $len = $i - $start + 1;
553 270 my $block = substr($str, $start, $len);
554  
555 270 my $matched_block;
556 270 ▶ if (index($block, $tag_if) == 0) { $matched_block = 'if' }
Branch: 3 branches
BranchTrueFalseTotal
if (index($block, $tag_if) == 0) { } 58 212 270
elsif (index($block, $tag_foreach) == 0) { } 39 173 212
elsif (index($block, $tag_literal) == 0) { } 7 166 173
557 39 elsif (index($block, $tag_foreach) == 0) { $matched_block = 'foreach' }
558 7 elsif (index($block, $tag_literal) == 0) { $matched_block = 'literal' }
559  
560 270 ▶ if ($matched_block) {
Branch: 1 branch
BranchTrueFalseTotal
if ($matched_block) 104 166 270
561 104 my $close_tag = "${od}/${matched_block}${cd}";
562 104 ▶ for (my $j = $i + 1; $j < length $str; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < length $str) 2077 1 2078
563 2077 ▶ if (substr($str, $j, 1) eq $cd) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($str, $j, 1) eq $cd) 230 1847 2077
564 230 my $tmp = substr($str, $start, $j - $start + 1);
565 230 my $oc = () = $tmp =~ /\Q${od}${matched_block}\E/g;
566 230 my $cc = () = $tmp =~ /\Q${close_tag}\E/g;
567 230 ▶ if ($oc == $cc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($oc == $cc) 103 127 230
568 103 $block = $tmp;
569 103 last;
570 }
571 }
572 }
573 }
574  
575 270 ▶ if (length $block) { push @blocks, [$block, $i] }
Branch: 1 branch
BranchTrueFalseTotal
if (length $block) 270 0 270
576 270 $start += length($block);
577 270 $i = $start;
578 }
579  
580 558 ▶ if ($is_comment) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_comment) 23 535 558
581 23 my $end = $self->find_ending_tag(substr($str, $start), $self->{_tag_comment_open}, $self->{_tag_comment_close});
582 23 ▶ if (!defined $end) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $end) 0 23 23
583 0 my ($line, $col, $file) = $self->_get_char_location($i, $self->{tpl_file});
584 0 $self->_error_out("Missing closing <code>$self->{_tag_comment_close}</code> for comment in <code>$file</code> on line #$line", 48724);
585 }
586 23 my $after = $start + $end + length($self->{_tag_comment_close});
587  
588 23 ▶ my $pre_nl = ($i == 0 || substr($str, $i - 1, 1) eq "\n");
Condition100%3/3
$i == 0 || substr($str, $i - 1, 1) eq "\n"
l!l&&r!l&&!r
1463
MC/DC100%2/2
$i == 0 || substr($str, $i - 1, 1) eq "\n"
$i == 0 substr($str, $i - 1, 1) eq "\n"
Truth table100%3/3
A || B
A= $i == 0B= substr($str, $i - 1, 1) eq "\n"
ABresult
1X1
011
000
589 23 ▶ my $post_nl = ($after >= $slen || substr($str, $after, 1) eq "\n");
Condition100%3/3
$after >= $slen || substr($str, $after, 1) eq "\n"
l!l&&r!l&&!r
1373
MC/DC100%2/2
$after >= $slen || substr($str, $after, 1) eq "\n"
$after >= $slen substr($str, $after, 1) eq "\n"
Truth table100%3/3
A || B
A= $after >= $slenB= substr($str, $after, 1) eq "\n"
ABresult
1X1
011
000
590 23 ▶ if ($pre_nl && $post_nl && $after < $slen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pre_nl and $post_nl and $after < $slen) 7 16 23
Condition100%6/6
$pre_nl and $post_nl and $after < $slen
!ll&&!rl&&r
4127
$pre_nl and $post_nl
!ll&&!rl&&r
3119
MC/DC100%3/3
$pre_nl and $post_nl and $after < $slen
$pre_nl $post_nl $after < $slen
Truth table100%4/4
A and B and C
A= $pre_nlB= $post_nlC= $after < $slen
ABCresult
0XX0
10X0
1100
1111
591 7 $after++;
592 }
593  
594 23 $start = $after;
595 23 $i = $start - 1;
596 }
597 }
598  
599 316 ▶ if ($start < $slen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start < $slen) 70 246 316
600 70 push @blocks, [substr($str, $start), $i];
601 }
602  
603 # Strip leading newline from text blocks that follow {if} or
604 # {foreach} blocks, to avoid double-newlines when the block
605 # payload already ends with \n. For {foreach}, only strip when
606 # the payload actually ends with \n — if the payload is inline
607 # (no trailing \n), the newline is structural content, not
608 # whitespace noise.
609 316 my $prev_is_if = 0;
610 316 my $tag_foreach_close = $self->{_tag_foreach_close};
611 316 for my $i (0 .. $#blocks) {
612 370 ▶ my $bstr = $blocks[$i][0] // '';
Condition50%1/2
$blocks[$i][0] // ""
l!l
3700
MC/DC0%0/1
$blocks[$i][0] // ""
$blocks[$i][0]
Truth table50%1/2
A
A= $blocks[$i][0]
Aresult
11
00
613 370 ▶ my $cur_is_if = (index($bstr, $tag_if) == 0 || index($bstr, $tag_foreach) == 0);
Condition100%3/3
index($bstr, $tag_if) == 0 || index($bstr, $tag_foreach) == 0
l!l&&r!l&&!r
5839273
MC/DC100%2/2
index($bstr, $tag_if) == 0 || index($bstr, $tag_foreach) == 0
index($bstr, $tag_if) == 0 index($bstr, $tag_foreach) == 0
Truth table100%3/3
A || B
A= index($bstr, $tag_if) == 0B= index($bstr, $tag_foreach) == 0
ABresult
1X1
011
000
614 370 ▶ if ($prev_is_if) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev_is_if) 4 366 370
615 4 my $should_strip = 1;
616 4 ▶ if ($blocks[$i-1][0] =~ /^\Q$tag_foreach\E.+?\}(.*)\Q$tag_foreach_close\E$/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blocks[$i - 1][0] =~ /^\Q$tag_foreach\E.+?\}(.*)\Q$tag_foreach_close\E$/su) 2 2 4
617 2 ▶ $should_strip = (substr($1, -1) eq "\n") ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
substr($1, -1) eq "\n" ? : 1 1 2
618 }
619 4 ▶ if ($should_strip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($should_strip) 3 1 4
620 3 $blocks[$i][0] = $self->ltrim_one($bstr, "\n");
621 }
622 }
623 370 $prev_is_if = $cur_is_if;
624 }
625  
626 316 $self->{_blocks_cache}{$str} = \@blocks;
627 316 return @blocks;
628 }
629  
630 289 ▶sub _process_blocks {
Subroutine _process_blocks: called CC=28 SCAR=33.6
631 289 my $self = shift;
632 289 my $blocks = shift;
633 289 my $out = shift; # Optional: ref to append output to (avoids temp string + concat)
634  
635 289 my $od_ord = $self->{_od_ord};
636 289 my $od = $self->{open_delim};
637 289 my $var_tag = "${od}\$";
638 289 my $var_re = $self->{_re_var_simple};
639  
640 289 ▶ if ($out) {
Branch: 1 branch
BranchTrueFalseTotal
if ($out) 57 232 289
641 57 for my $x (@$blocks) {
642 62 my $block = $x->[0];
643 62 ▶ next unless length $block;
Branch: 1 branch
BranchTrueFalseTotal
unless length $block 0 62 62
644 62 ▶ if (ord($block) != $od_ord) {
Branch: 1 branch
BranchTrueFalseTotal
if (ord $block != $od_ord) 42 20 62
645 42 $$out .= $block;
646 42 next;
647 }
648 # Fast path: {$var} or {$var.dot} with no modifier — inline
649 # variable resolution, skip _process_block AND _variable_block
650 20 ▶ if (substr($block, 0, 2) eq $var_tag && index($block, '|') < 0
Branch: 1 branch
BranchTrueFalseTotal
if (substr($block, 0, 2) eq $var_tag and index($block, "|") == -1 and $block =~ /$var_re/u) 13 7 20
Condition83%5/6
substr($block, 0, 2) eq $var_tag and index($block, "|") == -1 and $block =~ /$var_re/u
!ll&&!rl&&r
7013
substr($block, 0, 2) eq $var_tag and index($block, "|") == -1
!ll&&!rl&&r
6113
MC/DC66%2/3
substr($block, 0, 2) eq $var_tag and index($block, "|") == -1 and $block =~ /$var_re/u
substr($block, 0, 2) eq $var_tag index($block, "|") == -1 $block =~ /$var_re/u
Truth table75%3/4
A and B and C
A= substr($block, 0, 2) eq $var_tagB= index($block, "|") == -1C= $block =~ /$var_re/u
ABCresult
0XX0
10X0
1100
1111
651 && $block =~ $var_re) {
652 13 my $var = $1;
653 13 my $val;
654 13 ▶ if (index($var, '.') < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($var, '.') == -1) { } 9 4 13
655 9 $val = $self->{tpl_vars}{$var};
656 } else {
657 4 $val = $self->array_dive($var, $self->{tpl_vars});
658 }
659 13 ▶ if (ref $val eq 'ARRAY') { $$out .= 'ARRAY' }
Branch: 3 branches
BranchTrueFalseTotal
if (ref $val eq 'ARRAY') { } 0 13 13
elsif (ref $val eq 'HASH') { } 0 13 13
elsif (defined $val) { } 13 0 13
660 0 elsif (ref $val eq 'HASH') { $$out .= 'HASH' }
661 13 ▶ elsif (defined $val) { $$out .= ($self->{auto_escape} ? escape($val) : $val) }
Branch: 1 branch
BranchTrueFalseTotal
$self->{'auto_escape'} ? : 1 12 13
662 13 next;
663 }
664 # If block fast path — skip _process_block dispatch (mirrors
665 # the $html branch so the $out path is equally fast).
666 7 ▶ if (substr($block, 0, $self->{_tag_if_len}) eq $self->{_tag_if}
Branch: 1 branch
BranchTrueFalseTotal
if (substr($block, 0, $self->{"_tag_if_len"}) eq $self->{"_tag_if"} and substr($block, -$self->{"_tag_if_close_len"}) eq $self->{"_tag_if_close"}) 6 1 7
Condition66%2/3
substr($block, 0, $self->{"_tag_if_len"}) eq $self->{"_tag_if"} and substr($block, -$self->{"_tag_if_close_len"}) eq $self->{"_tag_if_close"}
!ll&&!rl&&r
106
MC/DC50%1/2
substr($block, 0, $self->{"_tag_if_len"}) eq $self->{"_tag_if"} and substr($block, -$self->{"_tag_if_close_len"}) eq $self->{"_tag_if_close"}
substr($block, 0, $self->{"_tag_if_len"}) eq $self->{"_tag_if"} substr($block, -$self->{"_tag_if_close_len"}) eq $self->{"_tag_if_close"}
Truth table66%2/3
A and B
A= substr($block, 0, $self->{"_tag_if_len"}) eq $self->{"_tag_if"}B= substr($block, -$self->{"_tag_if_close_len"}) eq $self->{"_tag_if_close"}
ABresult
0X0
100
111
667 && substr($block, -$self->{_tag_if_close_len}) eq $self->{_tag_if_close}) {
668 6 $self->{char_pos} = $x->[1];
669 6 $$out .= $self->_if_block($block);
670 6 next;
671 }
672 1 $$out .= $self->_process_block($block, $x->[1]);
673 }
674 57 return;
675 }
676  
677 232 my $html = '';
678 232 for my $x (@$blocks) {
679 272 my $block = $x->[0];
680 272 ▶ next unless length $block;
Branch: 1 branch
BranchTrueFalseTotal
unless length $block 0 272 272
681 272 ▶ if (ord($block) != $od_ord) {
Branch: 1 branch
BranchTrueFalseTotal
if (ord $block != $od_ord) 52 220 272
682 52 $html .= $block;
683 52 next;
684 }
685 # Fast path: {$var} or {$var.dot} with no modifier
686 220 ▶ if (substr($block, 0, 2) eq $var_tag && index($block, '|') < 0
Branch: 1 branch
BranchTrueFalseTotal
if (substr($block, 0, 2) eq $var_tag and index($block, "|") == -1 and $block =~ /$var_re/u) 49 171 220
Condition100%6/6
substr($block, 0, 2) eq $var_tag and index($block, "|") == -1 and $block =~ /$var_re/u
!ll&&!rl&&r
164749
substr($block, 0, 2) eq $var_tag and index($block, "|") == -1
!ll&&!rl&&r
1145056
MC/DC100%3/3
substr($block, 0, 2) eq $var_tag and index($block, "|") == -1 and $block =~ /$var_re/u
substr($block, 0, 2) eq $var_tag index($block, "|") == -1 $block =~ /$var_re/u
Truth table100%4/4
A and B and C
A= substr($block, 0, 2) eq $var_tagB= index($block, "|") == -1C= $block =~ /$var_re/u
ABCresult
0XX0
10X0
1100
1111
687 && $block =~ $var_re) {
688 49 my $var = $1;
689 49 my $val;
690 49 ▶ if (index($var, '.') < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($var, '.') == -1) { } 45 4 49
691 45 $val = $self->{tpl_vars}{$var};
692 } else {
693 4 $val = $self->array_dive($var, $self->{tpl_vars});
694 }
695 49 ▶ if (ref $val eq 'ARRAY') { $html .= 'ARRAY' }
Branch: 3 branches
BranchTrueFalseTotal
if (ref $val eq 'ARRAY') { } 4 45 49
elsif (ref $val eq 'HASH') { } 0 45 45
elsif (defined $val) { } 38 7 45
696 0 elsif (ref $val eq 'HASH') { $html .= 'HASH' }
697 38 ▶ elsif (defined $val) { $html .= ($self->{auto_escape} ? escape($val) : $val) }
Branch: 1 branch
BranchTrueFalseTotal
$self->{'auto_escape'} ? : 5 33 38
698 49 next;
699 }
700 # If block fast path — skip _process_block dispatch
701 171 ▶ if (substr($block, 0, $self->{_tag_if_len}) eq $self->{_tag_if}
Branch: 1 branch
BranchTrueFalseTotal
if (substr($block, 0, $self->{"_tag_if_len"}) eq $self->{"_tag_if"} and substr($block, -$self->{"_tag_if_close_len"}) eq $self->{"_tag_if_close"}) 44 127 171
Condition100%3/3
substr($block, 0, $self->{"_tag_if_len"}) eq $self->{"_tag_if"} and substr($block, -$self->{"_tag_if_close_len"}) eq $self->{"_tag_if_close"}
!ll&&!rl&&r
126144
MC/DC100%2/2
substr($block, 0, $self->{"_tag_if_len"}) eq $self->{"_tag_if"} and substr($block, -$self->{"_tag_if_close_len"}) eq $self->{"_tag_if_close"}
substr($block, 0, $self->{"_tag_if_len"}) eq $self->{"_tag_if"} substr($block, -$self->{"_tag_if_close_len"}) eq $self->{"_tag_if_close"}
Truth table100%3/3
A and B
A= substr($block, 0, $self->{"_tag_if_len"}) eq $self->{"_tag_if"}B= substr($block, -$self->{"_tag_if_close_len"}) eq $self->{"_tag_if_close"}
ABresult
0X0
100
111
702 && substr($block, -$self->{_tag_if_close_len}) eq $self->{_tag_if_close}) {
703 44 $self->{char_pos} = $x->[1];
704 44 $html .= $self->_if_block($block);
705 44 next;
706 }
707 127 $html .= $self->_process_block($block, $x->[1]);
708 }
709  
710 222 return $html;
711 }
712  
713 140 ▶sub _process_block {
Subroutine _process_block: called CC=14 SCAR=26.7
714 140 my $self = shift;
715 140 ▶ my $str = shift // '';
Condition50%1/2
shift() // ""
l!l
1400
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
716 140 ▶ my $char_pos = shift // -1;
Condition50%1/2
shift() // -1
l!l
1400
MC/DC0%0/1
shift() // -1
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
717  
718 140 $self->{char_pos} = $char_pos;
719  
720 140 my $od = $self->{open_delim};
721 140 my $cd_ord = $self->{_cd_ord};
722  
723 # 1. Variable block {$foo} or {$foo|modifier}
724 140 ▶ if (substr($str, 0, 2) eq "${od}\$" && $str =~ $self->{_re_var_full}) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($str, 0, 2) eq "$od\$" and $str =~ /$self->{"_re_var_full"}/u) 60 80 140
Condition100%3/3
substr($str, 0, 2) eq "$od\$" and $str =~ /$self->{"_re_var_full"}/u
!ll&&!rl&&r
73760
MC/DC100%2/2
substr($str, 0, 2) eq "$od\$" and $str =~ /$self->{"_re_var_full"}/u
substr($str, 0, 2) eq "$od\$" $str =~ /$self->{"_re_var_full"}/u
Truth table100%3/3
A and B
A= substr($str, 0, 2) eq "$od\$"B= $str =~ /$self->{"_re_var_full"}/u
ABresult
0X0
100
111
725 60 return $self->_variable_block($1);
726 }
727  
728 # 2. If block {if ...}{/if}
729 80 ▶ if (substr($str, 0, $self->{_tag_if_len}) eq $self->{_tag_if}
Branch: 1 branch
BranchTrueFalseTotal
if (substr($str, 0, $self->{"_tag_if_len"}) eq $self->{"_tag_if"} and substr($str, -$self->{"_tag_if_close_len"}) eq $self->{"_tag_if_close"}) 0 80 80
Condition66%2/3
substr($str, 0, $self->{"_tag_if_len"}) eq $self->{"_tag_if"} and substr($str, -$self->{"_tag_if_close_len"}) eq $self->{"_tag_if_close"}
!ll&&!rl&&r
7910
MC/DC0%0/2
substr($str, 0, $self->{"_tag_if_len"}) eq $self->{"_tag_if"} and substr($str, -$self->{"_tag_if_close_len"}) eq $self->{"_tag_if_close"}
substr($str, 0, $self->{"_tag_if_len"}) eq $self->{"_tag_if"} substr($str, -$self->{"_tag_if_close_len"}) eq $self->{"_tag_if_close"}
Truth table66%2/3
A and B
A= substr($str, 0, $self->{"_tag_if_len"}) eq $self->{"_tag_if"}B= substr($str, -$self->{"_tag_if_close_len"}) eq $self->{"_tag_if_close"}
ABresult
0X0
100
111
730 && substr($str, -$self->{_tag_if_close_len}) eq $self->{_tag_if_close}) {
731 0 return $self->_if_block($str);
732 }
733  
734 # 3. Foreach block {foreach ...}{/foreach}
735 80 ▶ if (substr($str, 0, $self->{_tag_foreach_len}) eq $self->{_tag_foreach} && $str =~ $self->{_re_foreach}) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($str, 0, $self->{"_tag_foreach_len"}) eq $self->{"_tag_foreach"} and $str =~ /$self->{"_re_foreach"}/u) 39 41 80
Condition66%2/3
substr($str, 0, $self->{"_tag_foreach_len"}) eq $self->{"_tag_foreach"} and $str =~ /$self->{"_re_foreach"}/u
!ll&&!rl&&r
41039
MC/DC50%1/2
substr($str, 0, $self->{"_tag_foreach_len"}) eq $self->{"_tag_foreach"} and $str =~ /$self->{"_re_foreach"}/u
substr($str, 0, $self->{"_tag_foreach_len"}) eq $self->{"_tag_foreach"} $str =~ /$self->{"_re_foreach"}/u
Truth table66%2/3
A and B
A= substr($str, 0, $self->{"_tag_foreach_len"}) eq $self->{"_tag_foreach"}B= $str =~ /$self->{"_re_foreach"}/u
ABresult
0X0
100
111
736 39 return $self->_foreach_block($1, $2, $3, $4);
737 }
738  
739 # 4. Include block {include ...}
740 41 ▶ if (substr($str, 0, $self->{_tag_include_len}) eq $self->{_tag_include}) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($str, 0, $self->{"_tag_include_len"}) eq $self->{"_tag_include"}) 9 32 41
741 9 return $self->_include_block($str);
742 }
743  
744 # 5. Literal block {literal}...{/literal}
745 32 ▶ if (substr($str, 0, $self->{_tag_literal_len}) eq $self->{_tag_literal} && $str =~ $self->{_re_literal}) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($str, 0, $self->{"_tag_literal_len"}) eq $self->{"_tag_literal"} and $str =~ /$self->{"_re_literal"}/u) 7 25 32
Condition66%2/3
substr($str, 0, $self->{"_tag_literal_len"}) eq $self->{"_tag_literal"} and $str =~ /$self->{"_re_literal"}/u
!ll&&!rl&&r
2507
MC/DC50%1/2
substr($str, 0, $self->{"_tag_literal_len"}) eq $self->{"_tag_literal"} and $str =~ /$self->{"_re_literal"}/u
substr($str, 0, $self->{"_tag_literal_len"}) eq $self->{"_tag_literal"} $str =~ /$self->{"_re_literal"}/u
Truth table66%2/3
A and B
A= substr($str, 0, $self->{"_tag_literal_len"}) eq $self->{"_tag_literal"}B= $str =~ /$self->{"_re_literal"}/u
ABresult
0X0
100
111
746 7 return $1;
747 }
748  
749 # 6. Expression / function block
750 25 ▶ if ($str =~ $self->{_re_expr}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /$self->{"_re_expr"}/u) 21 4 25
751 21 return $self->_expression_block($str, $1);
752 }
753  
754 # 7. Unclosed tag
755 4 ▶ if (ord(substr($str, -1)) != $cd_ord) {
Branch: 1 branch
BranchTrueFalseTotal
if (ord substr($str, -1) != $cd_ord) 3 1 4
756 3 my $tag_start = $self->{char_pos} - length($str);
757 3 my ($line, $col, $file) = $self->_get_char_location($tag_start, $self->{tpl_file});
758 3 $self->_error_out("Unclosed tag <code>$str</code> in <code>$file</code> on line #$line", 45821);
759 }
760  
761 # 8. Fallthrough
762 1 return $str;
763 }
764  
765 # -------------------------------------------------------------------
766 # Block handlers
767 # -------------------------------------------------------------------
768  
769 60 ▶sub _variable_block {
Subroutine _variable_block: called CC=38 SCAR=44.4
770 60 my $self = shift;
771 60 my $str = shift;
772  
773 # Fast path: no pipe means no modifier, just resolve the variable.
774 # Avoids running the pipe-split regex on every plain variable block.
775 60 ▶ if (index($str, '|') < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($str, "|") == -1) 0 60 60
776 0 my $ret;
777 # Inline simple key lookup (no dots) — skips array_dive method call
778 0 ▶ if (index($str, '.') < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($str, '.') == -1) { } 0 0 0
779 0 $ret = $self->{tpl_vars}{$str};
780 } else {
781 0 $ret = $self->array_dive($str, $self->{tpl_vars});
782 }
783 0 ▶ if (ref $ret eq 'ARRAY') { return 'ARRAY' }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ret eq "ARRAY") 0 0 0
784 0 ▶ if (ref $ret eq 'HASH') { return 'HASH' }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ret eq "HASH") 0 0 0
785 0 ▶ if (defined $ret) { return ($self->{auto_escape} ? escape($ret) : $ret) }
Branch: 2 branches
BranchTrueFalseTotal
if (defined $ret) 0 0 0
$self->{'auto_escape'} ? : 0 0 0
786 0 return '';
787 }
788  
789 60 ▶ if ($str =~ /(.+?)\|(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /(.+?)\|(.*)/u) 60 0 60
790 60 my $key = $1;
791 60 my $mod = $2;
792  
793 60 my $tmp = $self->array_dive($key, $self->{tpl_vars});
794 60 ▶ my $is_nothing = (!defined $tmp || (defined $tmp && ref $tmp eq '' && !length $tmp && $tmp ne '0'));
Condition83%10/12
!defined($tmp) || defined $tmp && ref $tmp eq '' && !length($tmp) && $tmp ne '0'
l!l&&r!l&&!r
11148
defined $tmp and ref $tmp eq "" and not length $tmp and $tmp ne 0
!ll&&!rl&&r
4801
defined $tmp and ref $tmp eq "" and not length $tmp
!ll&&!rl&&r
14341
defined $tmp and ref $tmp eq ""
!ll&&!rl&&r
01435
MC/DC60%3/5
!defined($tmp) || defined $tmp && ref $tmp eq '' && !length($tmp) && $tmp ne '0'
!defined($tmp) defined $tmp ref $tmp eq "" not length $tmp $tmp ne 0
Truth table66%4/6
not(A) || B and C and not(D) and E
A= !defined($tmp)B= defined $tmpC= ref $tmp eq ""D= not length $tmpE= $tmp ne 0
ABCDEresult
1XXXX1
011111
00XXX0
010XX0
0110X0
011100
795 60 my $is_default = index($mod, 'default:') >= 0;
796  
797 60 ▶ if ($is_nothing && $is_default) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_nothing and $is_default) { } 6 54 60
elsif (not $is_nothing and $is_default) { } 3 51 54
Condition100%3/3
$is_nothing and $is_default
!ll&&!rl&&r
4866
MC/DC100%2/2
$is_nothing and $is_default
$is_nothing $is_default
Truth table100%3/3
A and B
A= $is_nothingB= $is_default
ABresult
0X0
100
111
798 6 my $dval = $mod;
799 6 $dval =~ s/^.*?default://;
800 6 my ($ret) = $self->_peval($dval);
801 6 ▶ if (defined $ret) { return $ret }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ret) 6 0 6
802 0 return '';
803 ▶ } elsif (!$is_nothing && $is_default) {
Condition100%3/3
not $is_nothing and $is_default
!ll&&!rl&&r
6453
MC/DC100%2/2
not $is_nothing and $is_default
not $is_nothing $is_default
Truth table100%3/3
not(A) and B
A= not $is_nothingB= $is_default
ABresult
0X0
100
111
804 3 ▶ return $tmp // '';
Condition50%1/2
$tmp // ""
l!l
30
MC/DC0%0/1
$tmp // ""
$tmp
Truth table50%1/2
A
A= $tmp
Aresult
11
00
805 } else {
806 51 ▶ if ($is_nothing) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_nothing) 6 45 51
807 6 return '';
808 }
809 45 my $pre = $tmp;
810  
811 45 my $seen_escape = 0;
812 45 my $seen_noescape = 0;
813  
814 # Split on | not inside double or single quotes (supports chained
815 # modifiers like {$x|uc|substr:0,3}). Regex precompiled in
816 # _precompute_tags to avoid per-call recompile cost.
817 45 my $pipe_re = $self->{_pipe_re};
818 45 for my $m_part (split $pipe_re, $mod) {
819 49 my @x = split /:/, $m_part, 2;
820 49 ▶ my $func = $x[0] // '';
Condition50%1/2
$x[0] // ""
l!l
490
MC/DC0%0/1
$x[0] // ""
$x[0]
Truth table50%1/2
A
A= $x[0]
Aresult
11
00
821 49 ▶ if ($func eq 'escape') { $seen_escape = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($func eq "escape") 19 30 49
822 49 ▶ if ($func eq 'noescape') { $seen_noescape = 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($func eq "noescape") 7 42 49
823 49 ▶ my $param_str = $x[1] // '';
Condition100%2/2
$x[1] // ""
l!l
1534
MC/DC100%1/1
$x[1] // ""
$x[1]
Truth table100%2/2
A
A= $x[1]
Aresult
11
00
824 49 my @params = ($pre);
825  
826 49 ▶ if (length $param_str) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $param_str) 15 34 49
827 # Split on commas not inside double or single quotes
828 # (parameter separator in modifier calls like substr:2,2)
829 my @new = map {
830 16 my ($v) = $self->_peval($_);
831 16 $v;
832 15 } split $self->{_comma_re}, $param_str;
833 15 push @params, @new;
834 }
835  
836 # Resolve the modifier coderef once per func name and cache it.
837 # Priority: main::, current package (Template::Sluz built-ins),
838 # then CORE:: built-in operators.
839 49 my $cref = $self->{_mod_cache}{$func};
840 49 ▶ if (!defined $cref) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cref) 12 37 49
841 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
842 12 ▶ if (defined &{"main::$func"}) { $cref = \&{"main::$func"} }
Branch: 3 branches
BranchTrueFalseTotal
if (defined &{"main::$func";}) { } 2 10 12
elsif (defined &{$func;}) { } 6 4 10
elsif (defined &{"CORE::$func";}) { } 4 0 4
843 10 elsif (defined &{$func}) { $cref = \&{$func} }
844 4 elsif (defined &{"CORE::$func"}) { $cref = \&{"CORE::$func"} }
845 0 else { $cref = 0 }
846 12 $self->{_mod_cache}{$func} = $cref;
847 }
848  
849 49 ▶ if (!$cref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cref) 0 49 49
850 0 my ($line, $col, $file) = $self->_get_char_location($self->{char_pos}, $self->{tpl_file});
851 0 $self->_error_out("Unknown function call <code>$func</code> in <code>$file</code> on line #$line", 47204);
852 }
853  
854 49 $pre = eval { $cref->(@params) };
855 49 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 49 49
856 0 $self->_error_out("Exception: $@", 79134);
857 }
858 }
859  
860 45 ▶ if ($self->{auto_escape} && !$seen_noescape && !$seen_escape) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"auto_escape"} and not $seen_noescape and not $seen_escape) 1 44 45
Condition100%6/6
$self->{"auto_escape"} and not $seen_noescape and not $seen_escape
!ll&&!rl&&r
4311
$self->{"auto_escape"} and not $seen_noescape
!ll&&!rl&&r
3672
MC/DC100%3/3
$self->{"auto_escape"} and not $seen_noescape and not $seen_escape
$self->{"auto_escape"} not $seen_noescape not $seen_escape
Truth table100%4/4
A and not(B) and not(C)
A= $self->{"auto_escape"}B= not $seen_noescapeC= not $seen_escape
ABCresult
0XX0
10X0
1100
1111
861 1 return escape($pre);
862 }
863 44 return $pre;
864 }
865 }
866  
867 0 my $ret = $self->array_dive($str, $self->{tpl_vars});
868 0 ▶ if (ref $ret eq 'ARRAY') { return 'ARRAY' }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ret eq "ARRAY") 0 0 0
869 0 ▶ if (ref $ret eq 'HASH') { return 'HASH' }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ret eq "HASH") 0 0 0
870 0 ▶ if (defined $ret) { return ($self->{auto_escape} ? escape($ret) : $ret) }
Branch: 2 branches
BranchTrueFalseTotal
if (defined $ret) 0 0 0
$self->{'auto_escape'} ? : 0 0 0
871 0 return '';
872 }
873  
874 68 ▶sub _if_block {
Subroutine _if_block: called CC=10 SCAR=23.0
875 68 my $self = shift;
876 68 my $str = shift;
877  
878 68 my @rules;
879 68 ▶ if (exists $self->{_if_rules_cache}{$str}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'_if_rules_cache'}{$str}) { } 12 56 68
880 12 @rules = @{$self->{_if_rules_cache}{$str}};
881 } else {
882 56 my $od = $self->{open_delim};
883 56 my $cd = $self->{close_delim};
884 56 my $isimple_start = length($od) + 1;
885 56 my $else_check = $od . 'else';
886 56 my $is_simple = index($str, $else_check, $isimple_start) < 0;
887  
888 56 ▶ if ($is_simple) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_simple) { } 32 24 56
889 32 $str =~ $self->{_re_if_simple};
890 32 ▶ my $cond = $1 // '';
Condition100%2/2
$1 // ""
l!l
311
MC/DC100%1/1
$1 // ""
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
891 32 ▶ my $payload = $2 // '';
Condition100%2/2
$2 // ""
l!l
311
MC/DC100%1/1
$2 // ""
$2
Truth table100%2/2
A
A= $2
Aresult
11
00
892 32 $payload = $self->ltrim_one($payload, "\n");
893 32 @rules = ([$cond, $payload]);
894 } else {
895 24 my @toks = $self->get_tokens($str);
896 24 @rules = $self->_if_rules_from_tokens(\@toks);
897 }
898  
899 56 $self->{_if_rules_cache}{$str} = \@rules;
900 }
901  
902 68 my $ret = '';
903 68 for my $rule (@rules) {
904 86 my $raw = $rule->[0];
905 # Inline _convert_vars for cached expressions — saves method call per iteration
906 my $test = (index($raw, '$') < 0) ? $raw :
907 86 ▶ ($self->{_convert_cache}{$raw} // $self->_convert_vars($raw));
Branch: 1 branch
BranchTrueFalseTotal
(index($raw, '$') == -1) ? : 16 70 86
Condition66%2/3
$self->{"_convert_cache"}{$raw} // $self->_convert_vars($raw)
l!l&&r!l&&!r
38320
MC/DC0%0/2
$self->{"_convert_cache"}{$raw} // $self->_convert_vars($raw)
$self->{"_convert_cache"}{$raw} $self->_convert_vars($raw)
Truth table66%2/3
A // B
A= $self->{"_convert_cache"}{$raw}B= $self->_convert_vars($raw)
ABresult
1X1
011
000
908 86 my $payload = $rule->[1];
909 86 my ($res) = $self->_peval($test);
910 86 ▶ if ($res) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res) 57 29 86
911 # Inline _get_blocks for cached payloads. Pass \$ret to
912 # _process_blocks so it appends directly — avoids a temp
913 # string allocation + concat per if-payload render.
914 57 my $cached = $self->{_blocks_cache}{$payload};
915 57 ▶ my @in_blocks = $cached ? @$cached : $self->_get_blocks($payload);
Branch: 1 branch
BranchTrueFalseTotal
$cached ? : 13 44 57
916 57 $self->_process_blocks(\@in_blocks, \$ret);
917 57 last;
918 }
919 }
920  
921 68 return $ret;
922 }
923  
924 39 ▶sub _foreach_block {
Subroutine _foreach_block: called CC=64 SCAR=44.6
925 39 my $self = shift;
926 39 my $src_expr = shift;
927 39 my $okey = shift;
928 39 my $oval = shift;
929 39 my $payload = shift;
930  
931 39 my $conv_src = $self->_convert_vars($src_expr);
932 39 $payload = $self->ltrim_one($payload, "\n");
933 39 my @blocks = $self->_get_blocks($payload);
934  
935 # Pre-classify blocks for fast dispatch in the loop (cached in block arrays)
936 # type: -1=empty, 0=text, 1=simple_var, 2=if_block, 99=other
937 39 my $od = $self->{open_delim};
938 39 my $od_ord = $self->{_od_ord};
939 39 for my $b (@blocks) {
940 62 ▶ next if defined $b->[2];
Branch: 1 branch
BranchTrueFalseTotal
if defined $b->[2] 6 56 62
941 56 my $bs = $b->[0];
942 56 ▶ if (!length $bs) {
Branch: 4 branches
BranchTrueFalseTotal
if (not length $bs) { } 0 56 56
elsif (ord $bs != $od_ord) { } 16 40 56
elsif (substr($bs, 0, 2) eq "$od\$" and index($bs, '|') == -1 and $bs =~ /$self->{'_re_var_simple'}/u) { } 30 10 40
elsif (substr($bs, 0, $self->{'_tag_if_len'}) eq $self->{'_tag_if'} and substr($bs, -$self->{'_tag_if_close_len'}) eq $self->{'_tag_if_close'}) { } 6 4 10
943 0 $b->[2] = -1;
944 } elsif (ord($bs) != $od_ord) {
945 16 $b->[2] = 0;
946 ▶ } elsif (substr($bs, 0, 2) eq "${od}\$" && index($bs, '|') < 0
Condition83%5/6
substr($bs, 0, 2) eq "$od\$" and index($bs, "|") == -1 and $bs =~ /$self->{"_re_var_simple"}/u
!ll&&!rl&&r
10030
substr($bs, 0, 2) eq "$od\$" and index($bs, "|") == -1
!ll&&!rl&&r
7330
MC/DC66%2/3
substr($bs, 0, 2) eq "$od\$" and index($bs, "|") == -1 and $bs =~ /$self->{"_re_var_simple"}/u
substr($bs, 0, 2) eq "$od\$" index($bs, "|") == -1 $bs =~ /$self->{"_re_var_simple"}/u
Truth table75%3/4
A and B and C
A= substr($bs, 0, 2) eq "$od\$"B= index($bs, "|") == -1C= $bs =~ /$self->{"_re_var_simple"}/u
ABCresult
0XX0
10X0
1100
1111
947 && $bs =~ $self->{_re_var_simple}) {
948 30 $b->[2] = 1;
949 30 $b->[3] = $1;
950 ▶ } elsif (substr($bs, 0, $self->{_tag_if_len}) eq $self->{_tag_if}
Condition66%2/3
substr($bs, 0, $self->{"_tag_if_len"}) eq $self->{"_tag_if"} and substr($bs, -$self->{"_tag_if_close_len"}) eq $self->{"_tag_if_close"}
!ll&&!rl&&r
406
MC/DC50%1/2
substr($bs, 0, $self->{"_tag_if_len"}) eq $self->{"_tag_if"} and substr($bs, -$self->{"_tag_if_close_len"}) eq $self->{"_tag_if_close"}
substr($bs, 0, $self->{"_tag_if_len"}) eq $self->{"_tag_if"} substr($bs, -$self->{"_tag_if_close_len"}) eq $self->{"_tag_if_close"}
Truth table66%2/3
A and B
A= substr($bs, 0, $self->{"_tag_if_len"}) eq $self->{"_tag_if"}B= substr($bs, -$self->{"_tag_if_close_len"}) eq $self->{"_tag_if_close"}
ABresult
0X0
100
111
951 && substr($bs, -$self->{_tag_if_close_len}) eq $self->{_tag_if_close}) {
952 6 $b->[2] = 2;
953 } else {
954 4 $b->[2] = 99;
955 }
956 }
957  
958 39 my ($src) = $self->_peval($conv_src);
959  
960 39 ▶ if (!defined $src) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $src) { } 2 37 39
elsif (ref $src ne 'ARRAY' and ref $src ne 'HASH') { } 1 68 69
961 2 $src = [];
962 ▶ } elsif (ref $src ne 'ARRAY' && ref $src ne 'HASH') {
Condition100%3/3
ref $src ne "ARRAY" and ref $src ne "HASH"
!ll&&!rl&&r
3241
MC/DC100%2/2
ref $src ne "ARRAY" and ref $src ne "HASH"
ref $src ne "ARRAY" ref $src ne "HASH"
Truth table100%3/3
A and B
A= ref $src ne "ARRAY"B= ref $src ne "HASH"
ABresult
0X0
100
111
963 1 $src = [$src];
964 }
965  
966 39 my $pfx = $self->{var_prefix};
967  
968 # Precompute __S keys for the loop variables
969 39 my $okey_ks = "${pfx}_$okey";
970 39 ▶ my $oval_ks = defined $oval ? "${pfx}_$oval" : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $oval ? : 9 30 39
971 39 my $first_ks = "${pfx}__FOREACH_FIRST";
972 39 my $last_ks = "${pfx}__FOREACH_LAST";
973 39 my $index_ks = "${pfx}__FOREACH_INDEX";
974  
975 39 my $ret = '';
976 39 my $idx = 0;
977  
978 39 my $need_first = index($payload, '__FOREACH_FIRST') >= 0;
979 39 my $need_last = index($payload, '__FOREACH_LAST') >= 0;
980 39 my $need_index = index($payload, '__FOREACH_INDEX') >= 0;
981  
982 # Save only the keys we'll modify — O(k) where k <= 5, vs O(n) for
983 # copying the entire tpl_vars/__S hashes. Big win for nested foreach.
984 39 my @tpl_keys = ($okey);
985 39 my @ks_keys = ($okey_ks);
986 39 ▶ if (defined $oval) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $oval) 9 30 39
987 9 push @tpl_keys, $oval;
988 9 push @ks_keys, $oval_ks;
989 }
990 39 ▶ push @tpl_keys, '__FOREACH_FIRST' if $need_first;
Branch: 1 branch
BranchTrueFalseTotal
if $need_first 2 37 39
991 39 ▶ push @ks_keys, $first_ks if $need_first;
Branch: 1 branch
BranchTrueFalseTotal
if $need_first 2 37 39
992 39 ▶ push @tpl_keys, '__FOREACH_LAST' if $need_last;
Branch: 1 branch
BranchTrueFalseTotal
if $need_last 1 38 39
993 39 ▶ push @ks_keys, $last_ks if $need_last;
Branch: 1 branch
BranchTrueFalseTotal
if $need_last 1 38 39
994 39 ▶ push @tpl_keys, '__FOREACH_INDEX' if $need_index;
Branch: 1 branch
BranchTrueFalseTotal
if $need_index 1 38 39
995 39 ▶ push @ks_keys, $index_ks if $need_index;
Branch: 1 branch
BranchTrueFalseTotal
if $need_index 1 38 39
996  
997 39 my @tpl_exists = map { exists $self->{tpl_vars}{$_} } @tpl_keys;
998 39 my @tpl_vals = map { $self->{tpl_vars}{$_} } @tpl_keys;
999 39 my @ks_exists = map { exists $self->{__S}{$_} } @ks_keys;
1000 39 my @ks_vals = map { $self->{__S}{$_} } @ks_keys;
1001  
1002 39 ▶ if (ref $src eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $src eq 'ARRAY') { } 35 4 39
elsif (ref $src eq 'HASH') { } 4 0 4
1003 35 my $last = $#$src;
1004 35 for my $i (0 .. $last) {
1005 88 ▶ if ($need_first) {
Branch: 1 branch
BranchTrueFalseTotal
if ($need_first) 6 82 88
1006 6 ▶ $self->{tpl_vars}{__FOREACH_FIRST} = ($idx == 0) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$idx == 0 ? : 2 4 6
1007 6 ▶ $self->{__S}{$first_ks} = ($idx == 0) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$idx == 0 ? : 2 4 6
1008 }
1009 88 ▶ if ($need_last) {
Branch: 1 branch
BranchTrueFalseTotal
if ($need_last) 3 85 88
1010 3 ▶ $self->{tpl_vars}{__FOREACH_LAST} = ($idx == $last) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$idx == $last ? : 1 2 3
1011 3 ▶ $self->{__S}{$last_ks} = ($idx == $last) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$idx == $last ? : 1 2 3
1012 }
1013 88 ▶ if ($need_index) {
Branch: 1 branch
BranchTrueFalseTotal
if ($need_index) 3 85 88
1014 3 $self->{tpl_vars}{__FOREACH_INDEX} = $idx;
1015 3 $self->{__S}{$index_ks} = $idx;
1016 }
1017 88 ▶ if (defined $oval) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $oval) { } 16 72 88
1018 16 $self->{tpl_vars}{$okey} = $i;
1019 16 $self->{tpl_vars}{$oval} = $src->[$i];
1020 16 $self->{__S}{$okey_ks} = $i;
1021 16 $self->{__S}{$oval_ks} = $src->[$i];
1022 } else {
1023 72 $self->{tpl_vars}{$okey} = $src->[$i];
1024 72 $self->{__S}{$okey_ks} = $src->[$i];
1025 }
1026 # Inline block processing with pre-classified types — no substr/regex per iteration
1027 88 for my $b (@blocks) {
1028 132 my $type = $b->[2];
1029 132 ▶ if ($type == 0) {
Branch: 4 branches
BranchTrueFalseTotal
if ($type == 0) { } 34 98 132
elsif ($type == 1) { } 68 30 98
elsif ($type == 2) { } 18 12 30
elsif ($type == -1) { } 0 12 12
1030 34 $ret .= $b->[0];
1031 } elsif ($type == 1) {
1032 68 my $var = $b->[3];
1033 68 my $val;
1034 68 ▶ if (index($var, '.') < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($var, '.') == -1) { } 56 12 68
1035 56 $val = $self->{tpl_vars}{$var};
1036 } else {
1037 12 $val = $self->array_dive($var, $self->{tpl_vars});
1038 }
1039 68 ▶ if (ref $val eq 'ARRAY') { $ret .= 'ARRAY' }
Branch: 3 branches
BranchTrueFalseTotal
if (ref $val eq 'ARRAY') { } 0 68 68
elsif (ref $val eq 'HASH') { } 0 68 68
elsif (defined $val) { } 68 0 68
1040 0 elsif (ref $val eq 'HASH') { $ret .= 'HASH' }
1041 68 ▶ elsif (defined $val) { $ret .= ($self->{auto_escape} ? escape($val) : $val) }
Branch: 1 branch
BranchTrueFalseTotal
$self->{'auto_escape'} ? : 3 65 68
1042 } elsif ($type == 2) {
1043 18 $self->{char_pos} = $b->[1];
1044 18 $ret .= $self->_if_block($b->[0]);
1045 } elsif ($type == -1) {
1046 0 next;
1047 } else {
1048 12 $ret .= $self->_process_block($b->[0], $b->[1]);
1049 }
1050 }
1051 88 $idx++;
1052 }
1053 } elsif (ref $src eq 'HASH') {
1054 4 my @keys = sort keys %$src;
1055 4 my $last = $#keys;
1056 4 for my $i (0 .. $last) {
1057 11 my $k = $keys[$i];
1058 11 ▶ if ($need_first) {
Branch: 1 branch
BranchTrueFalseTotal
if ($need_first) 0 11 11
1059 0 ▶ $self->{tpl_vars}{__FOREACH_FIRST} = ($idx == 0) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$idx == 0 ? : 0 0 0
1060 0 ▶ $self->{__S}{$first_ks} = ($idx == 0) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$idx == 0 ? : 0 0 0
1061 }
1062 11 ▶ if ($need_last) {
Branch: 1 branch
BranchTrueFalseTotal
if ($need_last) 0 11 11
1063 0 ▶ $self->{tpl_vars}{__FOREACH_LAST} = ($idx == $last) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$idx == $last ? : 0 0 0
1064 0 ▶ $self->{__S}{$last_ks} = ($idx == $last) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$idx == $last ? : 0 0 0
1065 }
1066 11 ▶ if ($need_index) {
Branch: 1 branch
BranchTrueFalseTotal
if ($need_index) 0 11 11
1067 0 $self->{tpl_vars}{__FOREACH_INDEX} = $idx;
1068 0 $self->{__S}{$index_ks} = $idx;
1069 }
1070 11 ▶ if (defined $oval) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $oval) { } 6 5 11
1071 6 $self->{tpl_vars}{$okey} = $k;
1072 6 $self->{tpl_vars}{$oval} = $src->{$k};
1073 6 $self->{__S}{$okey_ks} = $k;
1074 6 $self->{__S}{$oval_ks} = $src->{$k};
1075 } else {
1076 5 $self->{tpl_vars}{$okey} = $src->{$k};
1077 5 $self->{__S}{$okey_ks} = $src->{$k};
1078 }
1079 # Inline block processing with pre-classified types — no substr/regex per iteration
1080 11 for my $b (@blocks) {
1081 32 my $type = $b->[2];
1082 32 ▶ if ($type == 0) {
Branch: 4 branches
BranchTrueFalseTotal
if ($type == 0) { } 15 17 32
elsif ($type == 1) { } 17 0 17
elsif ($type == 2) { } 0 0 0
elsif ($type == -1) { } 0 0 0
1083 15 $ret .= $b->[0];
1084 } elsif ($type == 1) {
1085 17 my $var = $b->[3];
1086 17 my $val;
1087 17 ▶ if (index($var, '.') < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($var, '.') == -1) { } 17 0 17
1088 17 $val = $self->{tpl_vars}{$var};
1089 } else {
1090 0 $val = $self->array_dive($var, $self->{tpl_vars});
1091 }
1092 17 ▶ if (ref $val eq 'ARRAY') { $ret .= 'ARRAY' }
Branch: 3 branches
BranchTrueFalseTotal
if (ref $val eq 'ARRAY') { } 0 17 17
elsif (ref $val eq 'HASH') { } 0 17 17
elsif (defined $val) { } 17 0 17
1093 0 elsif (ref $val eq 'HASH') { $ret .= 'HASH' }
1094 17 ▶ elsif (defined $val) { $ret .= ($self->{auto_escape} ? escape($val) : $val) }
Branch: 1 branch
BranchTrueFalseTotal
$self->{'auto_escape'} ? : 2 15 17
1095 } elsif ($type == 2) {
1096 0 $self->{char_pos} = $b->[1];
1097 0 $ret .= $self->_if_block($b->[0]);
1098 } elsif ($type == -1) {
1099 0 next;
1100 } else {
1101 0 $ret .= $self->_process_block($b->[0], $b->[1]);
1102 }
1103 }
1104 11 $idx++;
1105 }
1106 }
1107  
1108 # Restore only the keys we modified
1109 39 for my $i (0 .. $#tpl_keys) {
1110 52 ▶ if ($tpl_exists[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tpl_exists[$i]) { } 23 29 52
1111 23 $self->{tpl_vars}{$tpl_keys[$i]} = $tpl_vals[$i];
1112 } else {
1113 29 delete $self->{tpl_vars}{$tpl_keys[$i]};
1114 }
1115 52 ▶ if ($ks_exists[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ks_exists[$i]) { } 23 29 52
1116 23 $self->{__S}{$ks_keys[$i]} = $ks_vals[$i];
1117 } else {
1118 29 delete $self->{__S}{$ks_keys[$i]};
1119 }
1120 }
1121  
1122 39 return $ret;
1123 }
1124  
1125 9 ▶sub _include_block {
Subroutine _include_block: called CC=8 SCAR=21.7
1126 9 my $self = shift;
1127 9 my $str = shift;
1128  
1129 9 my $save = $self->{tpl_vars};
1130 9 my $inc_tpl = $self->_extract_include_file($str);
1131  
1132 9 ▶ if ($self->{perl_file_dir}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"perl_file_dir"}) 9 0 9
1133 9 $inc_tpl = $self->{perl_file_dir} . "/$inc_tpl";
1134 }
1135  
1136 9 ▶ while ($str =~ m/(\w+)=(['"](.+?)['"])/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /(\w+)=(['"](.+?)['"])/gu) 9 9 18
1137 9 my $key = $1;
1138 9 my $val = $2;
1139 9 ▶ if ($key eq 'file') { next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "file") 8 1 9
1140 1 $val = $self->_convert_vars($val);
1141 1 my ($res) = $self->_peval($val);
1142 1 ▶ if (defined $res) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $res) { } 1 0 1
1143 1 $self->assign($key => $res);
1144 } else {
1145 0 $self->assign($key => $val);
1146 }
1147 }
1148  
1149 9 ▶ if (!-f $inc_tpl || !-r $inc_tpl) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $inc_tpl and -r $inc_tpl) 0 9 9
Condition33%1/3
-f $inc_tpl and -r $inc_tpl
!ll&&!rl&&r
009
MC/DC0%0/2
-f $inc_tpl and -r $inc_tpl
-f $inc_tpl -r $inc_tpl
Truth table33%1/3
A and B
A= -f $inc_tplB= -r $inc_tpl
ABresult
0X0
100
111
1150 0 $self->{inc_tpl_file} = undef;
1151 0 my ($line, $col, $file) = $self->_get_char_location($self->{char_pos}, $self->{tpl_file});
1152 0 $self->_error_out("Unable to load include template <code>$inc_tpl</code> in <code>$file</code> on line #$line", 18485);
1153 }
1154  
1155 9 local $/;
1156 9 ▶ open my $fh, '<', $inc_tpl or $self->_error_out("Cannot open <code>$inc_tpl</code>: $!", 63579);
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $inc_tpl or $self->_error_out("Cannot open <code>$inc_tpl</code>: $!", 63579) 9 0 9
1157 9 my $content = <$fh>;
1158 9 close $fh;
1159  
1160 9 my @blocks = $self->_get_blocks($content);
1161 9 my $r = $self->_process_blocks(\@blocks);
1162  
1163 9 $self->{tpl_vars} = $save;
1164 9 $self->{inc_tpl_file} = undef;
1165  
1166 9 return $r;
1167 }
1168  
1169 21 ▶sub _expression_block {
Subroutine _expression_block: called CC=7 SCAR=19.5
1170 21 my $self = shift;
1171 21 my $str = shift;
1172 21 my $inner = shift;
1173  
1174 21 ▶ if ($str !~ /["\d\$\(]/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($str =~ /["\d\$\(]/u) 5 16 21
1175 5 my ($line, $col, $file) = $self->_get_char_location($self->{char_pos}, $self->{tpl_file});
1176 5 $self->_error_out("Unknown block type <code>$str</code> in <code>$file</code> on line #$line", 73467);
1177 }
1178  
1179 16 my $after = $self->_convert_vars($inner);
1180 16 my ($ret, $err) = $self->_peval($after);
1181  
1182 16 my $valid;
1183 16 ▶ if (defined $ret && (!ref $ret || ref $ret eq '')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ret and !ref($ret) || ref $ret eq '') { } 14 2 16
Condition50%3/6
defined $ret and !ref($ret) || ref $ret eq ""
!ll&&!rl&&r
2014
not ref $ret or ref $ret eq ""
l!l&&r!l&&!r
1400
MC/DC33%1/3
defined $ret and !ref($ret) || ref $ret eq ""
defined $ret not ref $ret ref $ret eq ""
Truth table50%2/4
A and not(B) or C
A= defined $retB= not ref $retC= ref $ret eq ""
ABCresult
0XX0
1000
11X1
1011
1184 14 $valid = 1;
1185 } else {
1186 2 $valid = 0;
1187 }
1188  
1189 16 ▶ if ($err || !$valid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err or not $valid) 2 14 16
Condition100%3/3
$err or not $valid
l!l&&r!l&&!r
1114
MC/DC100%2/2
$err or not $valid
$err not $valid
Truth table100%3/3
A or not(B)
A= $errB= not $valid
ABresult
1X1
011
000
1190 2 my ($line, $col, $file) = $self->_get_char_location($self->{char_pos}, $self->{tpl_file});
1191 2 $self->_error_out("Unknown tag <code>$str</code> in <code>$file</code> on line #$line", 18933);
1192 }
1193  
1194 14 return $ret;
1195 }
1196  
1197 # -------------------------------------------------------------------
1198 # Variable / eval engine
1199 # -------------------------------------------------------------------
1200  
1201 97 ▶sub _convert_vars {
Subroutine _convert_vars: called CC=5 SCAR=16.1
1202 97 my $self = shift;
1203 97 ▶ my $str = shift // '';
Condition50%1/2
shift() // ""
l!l
970
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
1204 97 ▶ if (index($str, '$') < 0) { return $str }
Branch: 1 branch
BranchTrueFalseTotal
if (index($str, "\$") == -1) 13 84 97
1205  
1206 # Check conversion cache — avoids re-running regex substitutions on
1207 # the same expression (e.g. an {if} condition inside a {foreach} loop)
1208 84 ▶ if (exists $self->{_convert_cache}{$str}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_convert_cache"}{$str}) 21 63 84
1209 21 return $self->{_convert_cache}{$str};
1210 }
1211  
1212 63 my $orig = $str;
1213  
1214 # Step 1: $var.key -> $__S->{sluz_pfx_var}->{key}
1215 63 $str =~ s/(\$\w[\w\.]*)/ $self->_dot_to_bracket_cb($1) /ge;
1216  
1217 # Step 2: $__S->{...}["key"] -> $__S->{...}->{key} (PHP bracket syntax)
1218 63 ▶ if (index($str, '[') >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($str, "[") != -1) 3 60 63
1219 3 $str =~ s/(\$__S(?:->\{[^}]+\})+)\[(["'])([^\]]+?)\2\]/$1 . '->{' . $3 . '}'/ge;
1220 }
1221  
1222 63 $self->{_convert_cache}{$orig} = $str;
1223 63 return $str;
1224 }
1225  
1226 67 ▶sub _dot_to_bracket_cb {
Subroutine _dot_to_bracket_cb: called CC=3 SCAR=11.0
1227 67 my $self = shift;
1228 67 my $match = shift;
1229 67 my @parts = split /\./, $match;
1230 67 my $first = shift @parts;
1231 67 my $var = substr($first, 1);
1232 67 my $res = "\$__S->\{$self->{var_prefix}_$var\}";
1233 67 for my $p (@parts) {
1234 6 ▶ if ($p =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p =~ /^\d+$/u) { } 1 5 6
1235 1 $res .= "->[$p]";
1236 } else {
1237 5 $res .= "->{$p}";
1238 }
1239 }
1240 67 return $res;
1241 }
1242  
1243 173 ▶sub _micro_optimize {
Subroutine _micro_optimize: called CC=17 SCAR=28.6
1244 173 my $self = shift;
1245 173 ▶ my $str = shift // '';
Condition50%1/2
shift() // ""
l!l
1730
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
1246 173 ▶ if ($str =~ /^-?\d+(?:\.\d+)?$/) { return $str }
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^-?\d+(?:\.\d+)?$/u) 19 154 173
1247  
1248 154 ▶ if (!length $str) { return undef }
Branch: 1 branch
BranchTrueFalseTotal
unless (length $str) 1 153 154
1249 153 my $first = ord($str);
1250 153 my $last = ord(substr($str, -1));
1251  
1252 153 ▶ if ($first == 39 && $last == 39) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first == 39 and $last == 39) 10 143 153
Condition66%2/3
$first == 39 and $last == 39
!ll&&!rl&&r
143010
MC/DC50%1/2
$first == 39 and $last == 39
$first == 39 $last == 39
Truth table66%2/3
A and B
A= $first == 39B= $last == 39
ABresult
0X0
100
111
1253 10 my $tmp = substr($str, 1, length($str) - 2);
1254 10 ▶ if (index($tmp, "'") < 0) { return $tmp }
Branch: 1 branch
BranchTrueFalseTotal
if (index($tmp, "'") == -1) 10 0 10
1255 }
1256  
1257 143 ▶ if ($first == 34 && $last == 34) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first == 34 and $last == 34) 16 127 143
Condition66%2/3
$first == 34 and $last == 34
!ll&&!rl&&r
127016
MC/DC50%1/2
$first == 34 and $last == 34
$first == 34 $last == 34
Truth table66%2/3
A and B
A= $first == 34B= $last == 34
ABresult
0X0
100
111
1258 16 my $tmp = substr($str, 1, length($str) - 2);
1259 16 ▶ if (index($tmp, '$') < 0 && index($tmp, '"') < 0) { return $tmp }
Branch: 1 branch
BranchTrueFalseTotal
if (index($tmp, "\$") == -1 and index($tmp, "\"") == -1) 13 3 16
Condition66%2/3
index($tmp, "\$") == -1 and index($tmp, "\"") == -1
!ll&&!rl&&r
3013
MC/DC50%1/2
index($tmp, "\$") == -1 and index($tmp, "\"") == -1
index($tmp, "\$") == -1 index($tmp, "\"") == -1
Truth table66%2/3
A and B
A= index($tmp, "\$") == -1B= index($tmp, "\"") == -1
ABresult
0X0
100
111
1260 }
1261  
1262 130 ▶ if ($str =~ /^(!?)\$__S->\{sluz_pfx_(\w+)\}$/ && exists $self->{tpl_vars}{$2}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^(!?)\$__S->\{sluz_pfx_(\w+)\}$/u and exists $self->{"tpl_vars"}{$2}) 80 50 130
Condition100%3/3
$str =~ /^(!?)\$__S->\{sluz_pfx_(\w+)\}$/u and exists $self->{"tpl_vars"}{$2}
!ll&&!rl&&r
361480
MC/DC100%2/2
$str =~ /^(!?)\$__S->\{sluz_pfx_(\w+)\}$/u and exists $self->{"tpl_vars"}{$2}
$str =~ /^(!?)\$__S->\{sluz_pfx_(\w+)\}$/u exists $self->{"tpl_vars"}{$2}
Truth table100%3/3
A and B
A= $str =~ /^(!?)\$__S->\{sluz_pfx_(\w+)\}$/uB= exists $self->{"tpl_vars"}{$2}
ABresult
0X0
100
111
1263 80 ▶ return $1 ? !$self->{tpl_vars}{$2} : $self->{tpl_vars}{$2};
Branch: 1 branch
BranchTrueFalseTotal
$1 ? : 3 77 80
1264 }
1265  
1266 50 ▶ if ($str =~ /^(!?)(\w+)$/ && exists $self->{tpl_vars}{$2}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^(!?)(\w+)$/u and exists $self->{"tpl_vars"}{$2}) 3 47 50
Condition66%2/3
$str =~ /^(!?)(\w+)$/u and exists $self->{"tpl_vars"}{$2}
!ll&&!rl&&r
4703
MC/DC50%1/2
$str =~ /^(!?)(\w+)$/u and exists $self->{"tpl_vars"}{$2}
$str =~ /^(!?)(\w+)$/u exists $self->{"tpl_vars"}{$2}
Truth table66%2/3
A and B
A= $str =~ /^(!?)(\w+)$/uB= exists $self->{"tpl_vars"}{$2}
ABresult
0X0
100
111
1267 3 ▶ return $1 ? !$self->{tpl_vars}{$2} : $self->{tpl_vars}{$2};
Branch: 1 branch
BranchTrueFalseTotal
$1 ? : 0 3 3
1268 }
1269  
1270 47 return undef;
1271 }
1272  
1273 173 ▶sub _peval {
Subroutine _peval: called CC=11 SCAR=24.0
1274 173 my $self = shift;
1275 173 ▶ my $str = shift // '';
Condition50%1/2
shift() // ""
l!l
1730
MC/DC0%0/1
shift() // ""
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
1276  
1277 173 ▶ if (index($str, '===') >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($str, "===") != -1) 0 173 173
1278 0 $str =~ s/===/==/g;
1279 }
1280  
1281 173 my $opt = $self->_micro_optimize($str);
1282 173 ▶ if (defined $opt) { return ($opt, 0) }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt) 123 50 173
1283  
1284 # Use the persistent $__S hash (maintained by assign/foreach) instead
1285 # of rebuilding it from tpl_vars on every call
1286 50 my $__S = $self->{__S};
1287  
1288 # Check verified sub cache — subs that have succeeded at least once.
1289 # Skip eval/local $SIG overhead (the biggest per-call cost in loops).
1290 # no warnings in the compiled sub suppresses uninitialized-value warnings.
1291 50 my $vsub = $self->{_verified_sub_cache}{$str};
1292 50 ▶ if ($vsub) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vsub) 13 37 50
1293 13 return ($vsub->($__S), 0);
1294 }
1295  
1296 # Check compiled sub cache — avoids re-parsing the same expression
1297 37 my $sub = $self->{_sub_cache}{$str};
1298 37 ▶ if (!defined $sub) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sub) 37 0 37
1299 # Compile in main:: first (where user functions live), then Template::Sluz
1300 6 ▶ $sub = eval "package main; no warnings; sub { my \$__S = \$_[0]; return ($str); }";
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
1301 37 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 36 37
1302 1 ▶ $sub = eval "no warnings; sub { my \$__S = \$_[0]; return ($str); }";
Subroutine BEGIN: called CC=1 SCAR=0.0
1303 }
1304 # Cache the result (even undef) so we don't recompile failures
1305 37 $self->{_sub_cache}{$str} = $sub;
1306 }
1307  
1308 37 my $ret;
1309 37 ▶ if ($sub) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sub) 36 1 37
1310 36 ▶ local $SIG{__WARN__} = sub {};
Subroutine __ANON__: not called CC=1 SCAR=0.0
1311 36 $ret = eval { $sub->($__S) };
1312 36 ▶ unless ($@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 33 3 36
1313 # Promote to verified cache — skip eval/SIG on future calls
1314 33 $self->{_verified_sub_cache}{$str} = $sub;
1315 33 delete $self->{_sub_cache}{$str};
1316 33 return ($ret, 0);
1317 }
1318 # Cached sub failed (e.g. function not in main::) — evict and fall through
1319 3 delete $self->{_sub_cache}{$str};
1320 }
1321  
1322 {
1323 4 ▶ local $SIG{__WARN__} = sub {};
Subroutine __ANON__: not called CC=1 SCAR=0.0
1324 4 ▶ $ret = eval "no warnings; return ($str);";
Subroutine BEGIN: called CC=1 SCAR=0.0
1325 4 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 3 4
1326 1 ▶ $ret = eval "package main; no warnings; return ($str);";
Subroutine BEGIN: called CC=1 SCAR=0.0
1327 }
1328 }
1329  
1330 4 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 3 4
1331 1 return (undef, -1);
1332 }
1333  
1334 3 return ($ret, 0);
1335 }
1336  
1337 # -------------------------------------------------------------------
1338 # Error handling
1339 # -------------------------------------------------------------------
1340  
1341 11 ▶sub _error_out {
Subroutine _error_out: called CC=1 SCAR=0.0
1342 11 my $self = shift;
1343 11 my $msg = shift;
1344 11 my $err_num = shift;
1345 11 croak "Template::Sluz error #$err_num: $msg";
1346 }
1347  
1348 10 ▶sub _get_char_location {
Subroutine _get_char_location: called CC=12 SCAR=29.3
1349 10 my $self = shift;
1350 10 my $pos = shift;
1351 10 ▶ my $tpl_file = shift // '';
Condition100%2/2
shift() // ""
l!l
19
MC/DC100%1/1
shift() // ""
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
1352  
1353 10 ▶ if ($self->{inc_tpl_file}) { $tpl_file = $self->{inc_tpl_file} }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inc_tpl_file"}) 0 10 10
1354  
1355 # Use display file name and line offset when available (e.g. inline DATA templates)
1356 10 ▶ my $display_file = exists $self->{tpl_file_display} ? $self->{tpl_file_display} : $tpl_file;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'tpl_file_display'} ? : 0 10 10
1357 10 ▶ my $line_offset = exists $self->{tpl_line_offset} ? $self->{tpl_line_offset} : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'tpl_line_offset'} ? : 0 10 10
1358  
1359 # Guard: no file context (e.g. parse_string) — skip _get_tpl_content
1360 # to avoid trying to open the perl_file_dir directory as a template file
1361 10 ▶ if (!length $tpl_file) { return (-1, -1, $display_file) }
Branch: 1 branch
BranchTrueFalseTotal
unless (length $tpl_file) 9 1 10
1362  
1363 1 my $str = $self->_get_tpl_content($tpl_file);
1364 1 ▶ if ($pos < 0 || !defined $str) { return (-1, -1, $display_file) }
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < 0 or not defined $str) 0 1 1
Condition33%1/3
$pos < 0 or not defined $str
l!l&&r!l&&!r
001
MC/DC0%0/2
$pos < 0 or not defined $str
$pos < 0 not defined $str
Truth table33%1/3
A or not(B)
A= $pos < 0B= not defined $str
ABresult
1X1
011
000
1365  
1366 1 my $line = 1;
1367 1 my $col = 0;
1368 1 ▶ for (my $i = 0; $i < length $str; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $str) 4 0 4
1369 4 $col++;
1370 4 ▶ if (substr($str, $i, 1) eq "\n") {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($str, $i, 1) eq "\n") 0 4 4
1371 0 $line++;
1372 0 $col = 0;
1373 }
1374 4 ▶ if ($pos == $i) { return ($line + $line_offset, $col, $display_file) }
Branch: 1 branch
BranchTrueFalseTotal
if ($pos == $i) 1 3 4
1375 }
1376  
1377 0 ▶ if ($pos == length $str) { return ($line + $line_offset, $col, $display_file) }
Branch: 1 branch
BranchTrueFalseTotal
if ($pos == length $str) 0 0 0
1378 0 return (-1, -1, $display_file);
1379 }
1380  
1381 9 ▶sub _extract_include_file {
Subroutine _extract_include_file: called CC=3 SCAR=11.1
1382 9 my $self = shift;
1383 9 my $str = shift;
1384  
1385 9 ▶ if ($str =~ /\s(file=)(['"].+?['"])/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /\s(file=)(['"].+?['"])/u) 8 1 9
1386 8 my $xstr = $self->_convert_vars($2);
1387 8 my ($ret) = $self->_peval($xstr);
1388 8 $self->{inc_tpl_file} = $ret;
1389 8 return $ret;
1390 }
1391  
1392 1 ▶ if ($str =~ /\s(['"].+?['"])/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /\s(['"].+?['"])/u) 1 0 1
1393 1 my $xstr = $self->_convert_vars($1);
1394 1 my ($ret) = $self->_peval($xstr);
1395 1 $self->{inc_tpl_file} = $ret;
1396 1 return $ret;
1397 }
1398  
1399 0 my ($line, $col, $file) = $self->_get_char_location($self->{char_pos}, $self->{tpl_file});
1400 0 $self->_error_out("Unable to find a file in include block <code>$str</code> in <code>$file</code> on line #$line", 68493);
1401 }
1402  
1403 24 ▶sub _if_rules_from_tokens {
Subroutine _if_rules_from_tokens: called CC=16 SCAR=27.7
1404 24 my $self = shift;
1405 24 my $toks = shift;
1406 24 my $num = scalar @$toks;
1407 24 my $nested = 0;
1408 24 my @tmp;
1409  
1410 24 my $tif_tag = $self->{_tag_if};
1411 24 my $tifc_tag = $self->{_tag_if_close};
1412 24 my $tif_prefix = $self->{open_delim} . 'if';
1413  
1414 24 for my $i (0 .. $num - 1) {
1415 160 my $item = $toks->[$i];
1416 160 ▶ if (index($item, $tif_prefix) == 0) { $nested++ }
Branch: 1 branch
BranchTrueFalseTotal
if (index($item, $tif_prefix) == 0) 30 130 160
1417 160 ▶ if ($item eq $tifc_tag) { $nested-- }
Branch: 1 branch
BranchTrueFalseTotal
if ($item eq $tifc_tag) 30 130 160
1418  
1419 160 my $yes = 0;
1420 160 ▶ if ($nested == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nested == 1) 116 44 160
1421 116 ▶ $yes = $self->is_if_token($item) || 0;
Condition100%2/2
$self->is_if_token($item) || 0
l!l
6155
MC/DC100%1/1
$self->is_if_token($item) || 0
$self->is_if_token($item)
Truth table100%2/2
A
A= $self->is_if_token($item)
Aresult
11
00
1422 116 ▶ $yes = 0 if $item eq $tifc_tag;
Branch: 1 branch
BranchTrueFalseTotal
if $item eq $tifc_tag 6 110 116
1423 }
1424 160 $tmp[$i] = $yes;
1425 }
1426  
1427 24 $tmp[$num - 1] = 1;
1428  
1429 24 my @conds;
1430 24 for my $i (0 .. $num - 1) {
1431 160 ▶ if ($tmp[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp[$i]) 79 81 160
1432 79 my $test = $self->is_if_token($toks->[$i]);
1433 79 ▶ if ($i != $num - 1) { push @conds, $test }
Branch: 1 branch
BranchTrueFalseTotal
if ($i != $num - 1) 55 24 79
1434 }
1435 }
1436  
1437 24 my $str = '';
1438 24 my @payloads;
1439 24 my $first = 1;
1440 24 for my $i (0 .. $num - 1) {
1441 160 ▶ if ($tmp[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp[$i]) { } 79 81 160
1442 79 ▶ if (!$first) { push @payloads, $str }
Branch: 1 branch
BranchTrueFalseTotal
unless ($first) 55 24 79
1443 79 $first = 0;
1444 79 $str = '';
1445 } else {
1446 81 $str .= $toks->[$i];
1447 }
1448 }
1449  
1450 24 ▶ if (@conds != @payloads) {
Branch: 1 branch
BranchTrueFalseTotal
if (@conds != @payloads) 0 24 24
1451 0 $self->_error_out("Error parsing if conditions in '$str'", 95320);
1452 }
1453  
1454 24 my @ret;
1455 24 push @ret, [$conds[$_], $payloads[$_]] for 0 .. $#conds;
1456 24 for my $rule (@ret) {
1457 55 $rule->[1] = $self->ltrim_one($rule->[1], "\n");
1458 }
1459 24 return @ret;
1460 }
1461  
1462 10 1;
1463  
1464 __END__
↑ Summary example.pl »