MustacheTemplate/Evaluator.pm

filter: Statementstmt 100% 58 / 58 Branchbran 100% 22 / 22 Conditioncond 100% 12 / 12 MC/DCmcdc 100% 8 / 8 Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 100% 113 / 113 CC 20 SCAR 19.4
CC
20
Cov
100%
retrieve_variable
11.0
evaluate_section_variable
8.0
evaluate_section
3.0
CRAP 6.9
« MustacheTemplate/Generator.pm ↑ Summary MustacheTemplate/Lexer.pm »
1 package Text::MustacheTemplate::Evaluator;
2 14 ▶use 5.022000;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 14 ▶use Exporter 5.57 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 14 our @EXPORT_OK = qw/retrieve_variable evaluate_section_variable evaluate_section/;
9  
10 14 ▶use Scalar::Util qw/looks_like_number/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 14 ▶use Text::MustacheTemplate::Lexer ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 14 our $LAMBDA_RENDERER;
15  
16 410 ▶sub retrieve_variable {
Subroutine retrieve_variable: called CC=11 SCAR=24.0
Pod documented
17 410 my ($ctx, @keys) = @_;
18  
19 CTX_LOOP:
20 410 for my $root (map $ctx->[$_], reverse 0..$#{$ctx}) {
21 669 my $value = $root;
22 669 for my $i (keys @keys) {
23 746 my $key = $keys[$i];
24 746 ▶ next CTX_LOOP if ref $value ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value ne "HASH" 86 660 746
25 660 ▶ next CTX_LOOP if $i == 0 && !exists $value->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $i == 0 and not exists $value->{$key} 201 459 660
Condition100%3/3
$i == 0 and not exists $value->{$key}
!ll&&!rl&&r
65394201
MC/DC100%2/2
$i == 0 and not exists $value->{$key}
$i == 0 not exists $value->{$key}
Truth table100%3/3
A and not(B)
A= $i == 0B= not exists $value->{$key}
ABresult
0X0
100
111
26  
27 459 my $is_last = $i == $#keys;
28 459 $value = $value->{$key};
29 459 ▶ $value = $value->() if !$is_last && ref $value eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if not $is_last and ref $value eq "CODE" 1 458 459
Condition100%3/3
not $is_last and ref $value eq "CODE"
!ll&&!rl&&r
382761
MC/DC100%2/2
not $is_last and ref $value eq "CODE"
not $is_last ref $value eq "CODE"
Truth table100%3/3
not(A) and B
A= not $is_lastB= ref $value eq "CODE"
ABresult
0X0
100
111
30 }
31 382 ▶ next if ref $value eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "HASH" 3 379 382
32  
33 379 ▶ if (ref $value eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "CODE") 18 361 379
34 18 $value = $value->();
35 14 ▶ if (do { no warnings qw/once/; $Text::MustacheTemplate::LAMBDA_TEMPLATE_RENDERING }) {
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (do { BEGIN {${^WARNING_BITS} = "\x55\x55\x55\x45\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55"} $Text::MustacheTemplate::LAMBDA_TEMPLATE_RENDERING }) 17 1 18
36 17 return $LAMBDA_RENDERER->($value);
37 }
38 }
39 362 return $value;
40 }
41 31 return '';
42 }
43  
44 274 ▶sub evaluate_section_variable {
Subroutine evaluate_section_variable: called CC=8 SCAR=20.8
Pod documented
45 274 my ($ctx, @keys) = @_;
46  
47 CTX_LOOP:
48 274 for my $root (map $ctx->[$_], reverse 0..$#{$ctx}) {
49 336 my $value = $root;
50 336 for my $i (keys @keys) {
51 375 my $key = $keys[$i];
52 375 ▶ next CTX_LOOP if ref $value ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value ne "HASH" 9 366 375
53 366 ▶ next CTX_LOOP if $i == 0 && !exists $value->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $i == 0 and not exists $value->{$key} 68 298 366
Condition100%3/3
$i == 0 and not exists $value->{$key}
!ll&&!rl&&r
3226668
MC/DC100%2/2
$i == 0 and not exists $value->{$key}
$i == 0 not exists $value->{$key}
Truth table100%3/3
A and not(B)
A= $i == 0B= not exists $value->{$key}
ABresult
0X0
100
111
54  
55 298 my $is_last = $i == $#keys;
56 298 $value = $value->{$key};
57 298 ▶ $value = $value->() if !$is_last && ref $value eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if not $is_last and ref $value eq "CODE" 4 294 298
Condition100%3/3
not $is_last and ref $value eq "CODE"
!ll&&!rl&&r
259354
MC/DC100%2/2
not $is_last and ref $value eq "CODE"
not $is_last ref $value eq "CODE"
Truth table100%3/3
not(A) and B
A= not $is_lastB= ref $value eq "CODE"
ABresult
0X0
100
111
58 }
59  
60 259 return evaluate_section($value);
61 }
62 15 return;
63 }
64  
65 277 ▶sub evaluate_section {
Subroutine evaluate_section: called CC=3 SCAR=11.0
Pod documented
66 277 my $value = shift;
67 277 ▶ return @$value if ref $value eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "ARRAY" 68 209 277
68 209 ▶ return $value ? $value : ();
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 140 69 209
69 }
70  
71 14 1;
72  
73 =head1 NAME
74  
75 Text::MustacheTemplate::Evaluator - [INTERNAL] Context evaluation for Mustache templates
76  
77 =head1 SYNOPSIS
78  
79 use Text::MustacheTemplate::Evaluator qw/
80 retrieve_variable
81 evaluate_section
82 evaluate_section_variable
83 /;
84
85 my @context = ({ name => 'World', items => [1, 2, 3] });
86
87 # Get a variable value
88 my $value = retrieve_variable(\@context, 'name');
89
90 # Evaluate if a section should be rendered
91 my @section_ctx = evaluate_section_variable(\@context, 'items');
92
93 # Evaluate a direct value
94 my @items_ctx = evaluate_section($context[-1]->{items});
95  
96 =head1 DESCRIPTION
97  
98 Text::MustacheTemplate::Evaluator provides functions for evaluating Mustache template contexts
99 and retrieving values from nested context structures according to the Mustache specification.
100  
101 This is internal interface for Text::MustacheTemplate.
102 The APIs may change without notice.
103  
104 =head1 FUNCTIONS
105  
106 =over 4
107  
108 =item retrieve_variable(\@context, @path)
109  
110 Retrieves a value from the context stack by following the given path.
111 Returns the value if found, otherwise undef.
112  
113 Parameters:
114  
115 =over 8
116  
117 =item \@context - An array reference to the context stack
118  
119 =item @path - The dot-separated path components to the variable
120  
121 =back
122  
123 =item evaluate_section($value)
124  
125 Evaluates whether a section should be rendered and how it should be processed.
126 Returns an array of context objects for iteration.
127  
128 Parameters:
129  
130 =over 8
131  
132 =item $value - The value to evaluate
133  
134 =back
135  
136 For different value types:
137  
138 =over 8
139  
140 =item * Undefined or falsy values: Returns an empty array (section not rendered)
141  
142 =item * Array references: Returns the array elements for iteration
143  
144 =item * Hash references: Returns the hash reference itself for context
145  
146 =item * Code references: Returns the code reference for lambda processing
147  
148 =item * True scalar values: Returns the value as a single-element array
149  
150 =back
151  
152 =item evaluate_section_variable(\@context, @path)
153  
154 Retrieves a variable by path and evaluates it as a section.
155 Combines retrieve_variable and evaluate_section.
156  
157 Parameters:
158  
159 =over 8
160  
161 =item \@context - An array reference to the context stack
162  
163 =item @path - The dot-separated path components to the variable
164  
165 =back
166  
167 =back
168  
169 =head1 LAMBDA SUPPORT
170  
171 When a section value is a code reference (lambda), the Evaluator provides special handling:
172  
173 =over 4
174  
175 =item * The lambda receives the raw section content as its first argument
176  
177 =item * If $LAMBDA_RENDERER is defined, the lambda's result will be processed by this renderer
178  
179 =item * This enables dynamic template generation within templates
180  
181 =back
182  
183 =head1 LICENSE
184  
185 =cut
« MustacheTemplate/Generator.pm ↑ Summary MustacheTemplate/Lexer.pm »