| 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
| |||||||||||||||||||||||||||||
| 25 | 660 | ▶ | next CTX_LOOP if $i == 0 && !exists $value->{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$i == 0 and not exists $value->{$key}
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}
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
not $is_last and ref $value eq "CODE"
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"
| |||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | 382 | ▶ | next if ref $value eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 379 | ▶ | if (ref $value eq 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 53 | 366 | ▶ | next CTX_LOOP if $i == 0 && !exists $value->{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$i == 0 and not exists $value->{$key}
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}
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
not $is_last and ref $value eq "CODE"
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"
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 68 | 209 | ▶ | return $value ? $value : (); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||