Sluz.pm

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