| 1 | package HTML::Template::Expr; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 7 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 7 | $VERSION = '0.07'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 7 | ▶ | use HTML::Template 2.4; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 7 | ▶ | use Carp qw(croak confess carp); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 7 | ▶ | use Parse::RecDescent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 7 | ▶ | use base 'HTML::Template'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 7 | ▶ | use vars qw($GRAMMAR); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 7 | $GRAMMAR = <<'END'; | |||||||||||||||||||||||||||
| 16 | expression : paren /^$/ { $return = $item[1] } | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | paren : '(' binary_op ')' { $item[2] } | ||||||||||||||||||||||||||||
| 19 | | '(' subexpression ')' { $item[2] } | ||||||||||||||||||||||||||||
| 20 | | subexpression { $item[1] } | ||||||||||||||||||||||||||||
| 21 | | '(' paren ')' { $item[2] } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | subexpression : function_call | ||||||||||||||||||||||||||||
| 24 | | var | ||||||||||||||||||||||||||||
| 25 | | literal | ||||||||||||||||||||||||||||
| 26 | | <error> | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | binary_op : paren (op paren { [ $item[2], $item[1] ] })(s) | ||||||||||||||||||||||||||||
| 29 | { $return = [ 'SUB_EXPR', $item[1], map { @$_ } @{$item[2]} ] } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | op : />=?|<=?|!=|==/ { [ 'BIN_OP', $item[1] ] } | ||||||||||||||||||||||||||||
| 32 | | /le|ge|eq|ne|lt|gt/ { [ 'BIN_OP', $item[1] ] } | ||||||||||||||||||||||||||||
| 33 | | /\|\||or|&&|and/ { [ 'BIN_OP', $item[1] ] } | ||||||||||||||||||||||||||||
| 34 | | /[-+*\/%]/ { [ 'BIN_OP', $item[1] ] } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | function_call : function_name '(' args ')' | ||||||||||||||||||||||||||||
| 37 | { [ 'FUNCTION_CALL', $item[1], $item[3] ] } | ||||||||||||||||||||||||||||
| 38 | | function_name ...'(' paren | ||||||||||||||||||||||||||||
| 39 | { [ 'FUNCTION_CALL', $item[1], [ $item[3] ] ] } | ||||||||||||||||||||||||||||
| 40 | | function_name '(' ')' | ||||||||||||||||||||||||||||
| 41 | { [ 'FUNCTION_CALL', $item[1] ] } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | function_name : /[A-Za-z_][A-Za-z0-9_]*/ | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | args : <leftop: paren ',' paren> | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | var : /[A-Za-z_][A-Za-z0-9_]*/ { [ 'VAR', $item[1] ] } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | literal : /-?\d*\.\d+/ { [ 'LITERAL', $item[1] ] } | ||||||||||||||||||||||||||||
| 50 | | /-?\d+/ { [ 'LITERAL', $item[1] ] } | ||||||||||||||||||||||||||||
| 51 | | <perl_quotelike> { [ 'LITERAL', $item[1][2] ] } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | END | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | # create global parser | ||||||||||||||||||||||||||||
| 57 | 7 | ▶ | use vars qw($PARSER); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 58 | 7 | $PARSER = Parse::RecDescent->new($GRAMMAR); | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | # initialize preset function table | ||||||||||||||||||||||||||||
| 61 | 7 | ▶ | use vars qw(%FUNC); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 62 | %FUNC = | ||||||||||||||||||||||||||||
| 63 | ( | ||||||||||||||||||||||||||||
| 64 | 4 | ▶ | 'sprintf' => sub { sprintf(shift, @_); }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 65 | 5 | ▶ | 'substr' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=9.2
| |||||||||||||||||||||||||||||
| 66 | 5 | ▶ | return substr($_[0], $_[1]) if @_ == 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 0 | return substr($_[0], $_[1], $_[2]); | |||||||||||||||||||||||||||
| 68 | }, | ||||||||||||||||||||||||||||
| 69 | 1 | ▶ | 'lc' => sub { lc($_[0]); }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 70 | 1 | ▶ | 'lcfirst' => sub { lcfirst($_[0]); }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 71 | 2 | ▶ | 'uc' => sub { uc($_[0]); }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 72 | 12 | ▶ | 'ucfirst' => sub { ucfirst($_[0]); }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 73 | 3 | ▶ | 'length' => sub { length($_[0]); }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 74 | 1 | ▶ | 'defined' => sub { defined($_[0]); }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | 'abs' => sub { abs($_[0]); }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 76 | 0 | ▶ | 'atan2' => sub { atan2($_[0], $_[1]); }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | 'cos' => sub { cos($_[0]); }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | 'exp' => sub { exp($_[0]); }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 79 | 0 | ▶ | 'hex' => sub { hex($_[0]); }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 80 | 8 | ▶ | 'int' => sub { int($_[0]); }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 81 | 0 | ▶ | 'log' => sub { log($_[0]); }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 82 | 0 | ▶ | 'oct' => sub { oct($_[0]); }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | 'rand' => sub { rand($_[0]); }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 84 | 0 | ▶ | 'sin' => sub { sin($_[0]); }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 85 | 8 | ▶ | 'sqrt' => sub { sqrt($_[0]); }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 86 | 0 | ▶ | 'srand' => sub { srand($_[0]); }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 87 | 7 | ); | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 11 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=10 SCAR=30.3
Pod
documented
| |||||||||||||||||||||||||||||
| 90 | 11 | my $pkg = shift; | |||||||||||||||||||||||||||
| 91 | 11 | my $self; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | # check hashworthyness | ||||||||||||||||||||||||||||
| 94 | 11 | ▶ | croak("HTML::Template::Expr->new() called with odd number of option parameters - should be of the form option => value") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | if (@_ % 2); | ||||||||||||||||||||||||||||
| 96 | 11 | my %options = @_; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # check for unsupported options file_cache and shared_cache | ||||||||||||||||||||||||||||
| 99 | croak("HTML::Template::Expr->new() : sorry, this module won't work with file_cache or shared_cache modes. This will hopefully be fixed in an upcoming version.") | ||||||||||||||||||||||||||||
| 100 | 11 | ▶ | if ($options{file_cache} or $options{shared_cache}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$options{"file_cache"} or $options{"shared_cache"}
MC/DC0%0/2
$options{"file_cache"} or $options{"shared_cache"}
$options{"file_cache"}
$options{"shared_cache"}
Truth table33%1/3
A or B
A= $options{"file_cache"}B= $options{"shared_cache"}
| |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | # push on our filter, one way or another. Why did I allow so many | ||||||||||||||||||||||||||||
| 103 | # different ways to say the same thing? Was I smoking crack? | ||||||||||||||||||||||||||||
| 104 | 11 | my @expr; | |||||||||||||||||||||||||||
| 105 | 11 | ▶ | if (exists $options{filter}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | # CODE => ARRAY | ||||||||||||||||||||||||||||
| 107 | $options{filter} = [ { 'sub' => $options{filter}, | ||||||||||||||||||||||||||||
| 108 | 'format' => 'scalar' } ] | ||||||||||||||||||||||||||||
| 109 | 0 | ▶ | if ref($options{filter}) eq 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | # HASH => ARRAY | ||||||||||||||||||||||||||||
| 112 | $options{filter} = [ $options{filter} ] | ||||||||||||||||||||||||||||
| 113 | 0 | ▶ | if ref($options{filter}) eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | # push onto ARRAY | ||||||||||||||||||||||||||||
| 116 | 0 | ▶ | if (ref($options{filter}) eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 0 | ▶ | push(@{$options{filter}}, { 'sub' => sub { _expr_filter(\@expr, @_); }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 118 | 0 | 'format' => 'scalar' }); | |||||||||||||||||||||||||||
| 119 | } else { | ||||||||||||||||||||||||||||
| 120 | # unrecognized | ||||||||||||||||||||||||||||
| 121 | 0 | croak("HTML::Template::Expr->new() : bad format for filter argument. Please check the HTML::Template docs for the allowed forms."); | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | } else { | ||||||||||||||||||||||||||||
| 124 | # new filter | ||||||||||||||||||||||||||||
| 125 | 9 | ▶ | $options{filter} = [ { 'sub' => sub { _expr_filter(\@expr, @_) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 126 | 11 | 'format' => 'scalar' | |||||||||||||||||||||||||||
| 127 | } ]; | ||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | # force global_vars on | ||||||||||||||||||||||||||||
| 131 | 11 | $options{global_vars} = 1; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | # create an HTML::Template object, catch the results to keep error | ||||||||||||||||||||||||||||
| 134 | # message line-numbers helpful. | ||||||||||||||||||||||||||||
| 135 | 11 | eval { | |||||||||||||||||||||||||||
| 136 | $self = $pkg->SUPER::new(%options, | ||||||||||||||||||||||||||||
| 137 | expr => \@expr, | ||||||||||||||||||||||||||||
| 138 | 11 | ▶ | expr_func => $options{functions} || {}); | ||||||||||||||||||||||||||
|
Condition100%2/2
$options{'functions'} || {}
MC/DC100%1/1
$options{'functions'} || {}
$options{'functions'}
Truth table100%2/2
A
A= $options{'functions'}
| |||||||||||||||||||||||||||||
| 139 | }; | ||||||||||||||||||||||||||||
| 140 | 11 | ▶ | croak("HTML::Template::Expr->new() : Error creating HTML::Template object : $@") if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 11 | return $self; | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 9 | ▶ | sub _expr_filter { | ||||||||||||||||||||||||||
|
Subroutine
_expr_filter: called CC=7 SCAR=19.6
| |||||||||||||||||||||||||||||
| 146 | 9 | my $expr = shift; | |||||||||||||||||||||||||||
| 147 | 9 | my $text = shift; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | # find expressions and create parse trees | ||||||||||||||||||||||||||||
| 150 | 9 | my ($ref, $tree, $before_expr, $expr_text, $after_expr, $vars, $which, $out); | |||||||||||||||||||||||||||
| 151 | 9 | $$text =~ s/ | |||||||||||||||||||||||||||
| 152 | <(?:!--\s*)? | ||||||||||||||||||||||||||||
| 153 | [Tt][Mm][Pp][Ll]_ | ||||||||||||||||||||||||||||
| 154 | ([Ii][Ff]|[Uu][Nn][Ll][Ee][Ss][Ss]|[Vv][Aa][Rr]) # $1 => which tag | ||||||||||||||||||||||||||||
| 155 | (\s+[^<]+)? # $2 => before expr | ||||||||||||||||||||||||||||
| 156 | \s+[Ee][Xx][Pp][Rr]= | ||||||||||||||||||||||||||||
| 157 | "([^"]*)" # $3 => the actual expr | ||||||||||||||||||||||||||||
| 158 | (\s+[^>-]+)? # $4 => after expr | ||||||||||||||||||||||||||||
| 159 | \s*(?:--)?> | ||||||||||||||||||||||||||||
| 160 | / | ||||||||||||||||||||||||||||
| 161 | 49 | $which = $1; | |||||||||||||||||||||||||||
| 162 | 49 | ▶ | $before_expr = $2 || ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$2 || ''
MC/DC100%1/1
$2 || ''
$2
Truth table100%2/2
A
A= $2
| |||||||||||||||||||||||||||||
| 163 | 49 | $expr_text = $3; | |||||||||||||||||||||||||||
| 164 | 49 | ▶ | $after_expr = $4 || ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$4 || ''
MC/DC100%1/1
$4 || ''
$4
Truth table100%2/2
A
A= $4
| |||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | # add enclosing parens to keep grammar simple | ||||||||||||||||||||||||||||
| 167 | 49 | $expr_text = "($expr_text)"; | |||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | # parse the expression | ||||||||||||||||||||||||||||
| 170 | 49 | eval { | |||||||||||||||||||||||||||
| 171 | 49 | $tree = $PARSER->expression($expr_text); | |||||||||||||||||||||||||||
| 172 | }; | ||||||||||||||||||||||||||||
| 173 | 49 | ▶ | croak("HTML::Template::Expr : Unable to parse expression: $expr_text") | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$@ or not $tree
MC/DC0%0/2
$@ or not $tree
$@
not $tree
Truth table33%1/3
A or not(B)
A= $@B= not $tree
| |||||||||||||||||||||||||||||
| 174 | if $@ or not $tree; | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | # stub out variables needed by the expression | ||||||||||||||||||||||||||||
| 177 | 49 | $out = "<tmpl_if __expr_unused__>"; | |||||||||||||||||||||||||||
| 178 | 49 | foreach my $var (_expr_vars($tree)) { | |||||||||||||||||||||||||||
| 179 | 52 | ▶ | next unless defined $var; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | 52 | $out .= "<tmpl_var name=\"$var\">"; | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | # save parse tree for later | ||||||||||||||||||||||||||||
| 184 | 49 | push(@$expr, $tree); | |||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | # add the expression placeholder and replace | ||||||||||||||||||||||||||||
| 187 | 49 | $out . "<\/tmpl_if><tmpl_$which ${before_expr}__expr_" . $#{$expr} . "__$after_expr>"; | |||||||||||||||||||||||||||
| 188 | /xeg; | ||||||||||||||||||||||||||||
| 189 | # stupid emacs - / | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 9 | return; | |||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | # find all variables in a parse tree | ||||||||||||||||||||||||||||
| 195 | 49 | ▶ | sub _expr_vars { | ||||||||||||||||||||||||||
|
Subroutine
_expr_vars: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 196 | 49 | my $tree = shift; | |||||||||||||||||||||||||||
| 197 | 49 | my %vars; | |||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | # hunt for VAR nodes in the tree | ||||||||||||||||||||||||||||
| 200 | 49 | my @stack = @$tree; | |||||||||||||||||||||||||||
| 201 | 49 | ▶ | while (@stack) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 202 | 509 | my $node = shift @stack; | |||||||||||||||||||||||||||
| 203 | 509 | ▶ | if (ref $node and ref $node eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $node and ref $node eq "ARRAY"
MC/DC50%1/2
ref $node and ref $node eq "ARRAY"
ref $node
ref $node eq "ARRAY"
Truth table66%2/3
A and B
A= ref $nodeB= ref $node eq "ARRAY"
| |||||||||||||||||||||||||||||
| 204 | 219 | ▶ | if ($node->[0] eq 'VAR') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 205 | 65 | $vars{$node->[1]} = 1; | |||||||||||||||||||||||||||
| 206 | } else { | ||||||||||||||||||||||||||||
| 207 | 154 | push @stack, @$node; | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||
| 211 | 49 | return keys %vars; | |||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | # allow loops to stay as HTML::Template objects, we don't need to | ||||||||||||||||||||||||||||
| 215 | # override output for them | ||||||||||||||||||||||||||||
| 216 | 3 | ▶ | sub _new_from_loop { | ||||||||||||||||||||||||||
|
Subroutine
_new_from_loop: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 217 | 3 | my ($pkg, @args) = @_; | |||||||||||||||||||||||||||
| 218 | 3 | return HTML::Template->_new_from_loop(@args); | |||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | 12 | ▶ | sub output { | ||||||||||||||||||||||||||
|
Subroutine
output: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 222 | 12 | my $self = shift; | |||||||||||||||||||||||||||
| 223 | 12 | my $parse_stack = $self->{parse_stack}; | |||||||||||||||||||||||||||
| 224 | 12 | my $options = $self->{options}; | |||||||||||||||||||||||||||
| 225 | 12 | my ($expr, $expr_func); | |||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | # pull expr and expr_func out of the parse_stack for cache mode. | ||||||||||||||||||||||||||||
| 228 | 12 | ▶ | if ($options->{cache}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 229 | 3 | $expr = pop @$parse_stack; | |||||||||||||||||||||||||||
| 230 | 3 | $expr_func = pop @$parse_stack; | |||||||||||||||||||||||||||
| 231 | } else { | ||||||||||||||||||||||||||||
| 232 | 9 | $expr = $options->{expr}; | |||||||||||||||||||||||||||
| 233 | 9 | $expr_func = $options->{expr_func}; | |||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | # setup expression evaluators | ||||||||||||||||||||||||||||
| 237 | 12 | my %param; | |||||||||||||||||||||||||||
| 238 | 12 | ▶ | for (my $x = 0; $x < @$expr; $x++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 239 | 60 | my $node = $expr->[$x]; | |||||||||||||||||||||||||||
| 240 | 60 | ▶ | $param{"__expr_" . $x . "__"} = sub { _expr_evaluate($node, @_) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||
| 242 | 12 | $self->param(\%param); | |||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | # setup %FUNC | ||||||||||||||||||||||||||||
| 245 | 12 | local %FUNC = (%FUNC, %$expr_func); | |||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | 12 | my $result = $self->SUPER::output(@_); | |||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | # restore cached values to their hideout in the parse_stack | ||||||||||||||||||||||||||||
| 250 | 12 | ▶ | if ($options->{cache}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 251 | 3 | push @$parse_stack, $expr_func; | |||||||||||||||||||||||||||
| 252 | 3 | push @$parse_stack, $expr; | |||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | 12 | return $result; | |||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | 136 | ▶ | sub _expr_evaluate { | ||||||||||||||||||||||||||
|
Subroutine
_expr_evaluate: called CC=35 SCAR=41.3
| |||||||||||||||||||||||||||||
| 259 | 136 | my ($tree, $template) = @_; | |||||||||||||||||||||||||||
| 260 | 136 | my ($op, $lhs, $rhs, $node, $type, @stack); | |||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | 136 | my @nodes = $tree; | |||||||||||||||||||||||||||
| 263 | 136 | ▶ | while (@nodes) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 264 | 335 | my $node = shift @nodes; | |||||||||||||||||||||||||||
| 265 | 335 | my $type = $node->[0]; | |||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | 335 | ▶ | if ($type eq 'LITERAL') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 268 | 62 | push @stack, $node->[1]; | |||||||||||||||||||||||||||
| 269 | 62 | next; | |||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | 273 | ▶ | if ($type eq 'VAR') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 273 | 92 | push @stack, $template->param($node->[1]); | |||||||||||||||||||||||||||
| 274 | 92 | next; | |||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | 181 | ▶ | if ($type eq 'SUB_EXPR') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 278 | 63 | unshift @nodes, @{$node}[1..$#{$node}]; | |||||||||||||||||||||||||||
| 279 | 63 | next; | |||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | 118 | ▶ | if ($type eq 'BIN_OP') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 283 | 68 | $op = $node->[1]; | |||||||||||||||||||||||||||
| 284 | 68 | $rhs = pop(@stack); | |||||||||||||||||||||||||||
| 285 | 68 | $lhs = pop(@stack); | |||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | # do the op | ||||||||||||||||||||||||||||
| 288 | 0 | ▶ | if ($op eq '==') {push @stack, $lhs == $rhs; next; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 289 | 68 | ▶ | if ($op eq 'eq') {push @stack, $lhs eq $rhs; next; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 290 | 60 | ▶ | if ($op eq '>') {push @stack, $lhs > $rhs; next; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 291 | 41 | ▶ | if ($op eq '<') {push @stack, $lhs < $rhs; next; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | 0 | ▶ | if ($op eq '!=') {push @stack, $lhs != $rhs; next; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 294 | 0 | ▶ | if ($op eq 'ne') {push @stack, $lhs ne $rhs; next; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 295 | 0 | ▶ | if ($op eq '>=') {push @stack, $lhs >= $rhs; next; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 296 | 0 | ▶ | if ($op eq '<=') {push @stack, $lhs <= $rhs; next; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | 36 | ▶ | if ($op eq '+') {push @stack, $lhs + $rhs; next; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 299 | 17 | ▶ | if ($op eq '-') {push @stack, $lhs - $rhs; next; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 300 | 0 | ▶ | if ($op eq '/') {push @stack, $lhs / $rhs; next; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 301 | 15 | ▶ | if ($op eq '*') {push @stack, $lhs * $rhs; next; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 302 | 0 | ▶ | if ($op eq '%') {push @stack, $lhs % $rhs; next; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | 0 | ▶ | if ($op eq 'le') {push @stack, $lhs le $rhs; next; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 305 | 0 | ▶ | if ($op eq 'ge') {push @stack, $lhs ge $rhs; next; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 306 | 0 | ▶ | if ($op eq 'lt') {push @stack, $lhs lt $rhs; next; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 307 | 0 | ▶ | if ($op eq 'gt') {push @stack, $lhs gt $rhs; next; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | # short circuit or | ||||||||||||||||||||||||||||
| 310 | 10 | ▶ | if ($op eq 'or' or $op eq '||') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$op eq "or" or $op eq "||"
MC/DC50%1/2
$op eq "or" or $op eq "||"
$op eq "or"
$op eq "||"
Truth table66%2/3
A or B
A= $op eq "or"B= $op eq "||"
| |||||||||||||||||||||||||||||
| 311 | 8 | ▶ | if ($lhs) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 312 | 4 | push @stack, 1; | |||||||||||||||||||||||||||
| 313 | 4 | next; | |||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||
| 315 | 4 | ▶ | if ($rhs) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 316 | 4 | push @stack, 1; | |||||||||||||||||||||||||||
| 317 | 4 | next; | |||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||
| 319 | 0 | push @stack, 0; | |||||||||||||||||||||||||||
| 320 | 0 | next; | |||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | # short circuit and | ||||||||||||||||||||||||||||
| 324 | 2 | ▶ | if ($op eq '&&' or $op eq 'and') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$op eq "&&" or $op eq "and"
MC/DC0%0/2
$op eq "&&" or $op eq "and"
$op eq "&&"
$op eq "and"
Truth table33%1/3
A or B
A= $op eq "&&"B= $op eq "and"
| |||||||||||||||||||||||||||||
| 325 | 2 | ▶ | unless ($lhs) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 326 | 1 | push @stack, 0; | |||||||||||||||||||||||||||
| 327 | 1 | next; | |||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||
| 329 | 1 | ▶ | unless ($rhs) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 330 | 1 | push @stack, 0; | |||||||||||||||||||||||||||
| 331 | 1 | next; | |||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||
| 333 | 0 | push @stack, 1; | |||||||||||||||||||||||||||
| 334 | 0 | next; | |||||||||||||||||||||||||||
| 335 | } | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | 0 | confess("HTML::Template::Expr : unknown op: $op"); | |||||||||||||||||||||||||||
| 338 | } | ||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | 50 | ▶ | if ($type eq 'FUNCTION_CALL') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 341 | 50 | my $name = $node->[1]; | |||||||||||||||||||||||||||
| 342 | 50 | my $args = $node->[2]; | |||||||||||||||||||||||||||
| 343 | croak("HTML::Template::Expr : found unknown subroutine call ". | ||||||||||||||||||||||||||||
| 344 | ": $name.\n") | ||||||||||||||||||||||||||||
| 345 | 50 | ▶ | unless exists($FUNC{$name}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 346 | 50 | ▶ | if (defined $args) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 347 | push @stack, | ||||||||||||||||||||||||||||
| 348 | scalar | ||||||||||||||||||||||||||||
| 349 | 49 | $FUNC{$name}->(map { _expr_evaluate($_, $template) } @$args); | |||||||||||||||||||||||||||
| 350 | } else { | ||||||||||||||||||||||||||||
| 351 | 1 | push @stack, scalar $FUNC{$name}->(); | |||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||
| 353 | 50 | next; | |||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | 0 | confess("HTML::Template::Expr : unrecognized node in tree: $node"); | |||||||||||||||||||||||||||
| 357 | } | ||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | 136 | ▶ | unless (@stack == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 360 | 0 | confess("HTML::Template::Expr : stack overflow! ". | |||||||||||||||||||||||||||
| 361 | "Please report this bug to the maintainer."); | ||||||||||||||||||||||||||||
| 362 | } | ||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | 136 | return $stack[0]; | |||||||||||||||||||||||||||
| 365 | } | ||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | 4 | ▶ | sub register_function { | ||||||||||||||||||||||||||
|
Subroutine
register_function: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 368 | 4 | my($class, $name, $sub) = @_; | |||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | 4 | ▶ | croak("HTML::Template::Expr : args 3 of register_function must be subroutine reference\n") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 371 | unless ref($sub) eq 'CODE'; | ||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | 3 | $FUNC{$name} = $sub; | |||||||||||||||||||||||||||
| 374 | } | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | # Make caching work right by hiding our vars in the parse_stack | ||||||||||||||||||||||||||||
| 378 | # between cache store and load. This is such a hack. | ||||||||||||||||||||||||||||
| 379 | 1 | ▶ | sub _commit_to_cache { | ||||||||||||||||||||||||||
|
Subroutine
_commit_to_cache: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 380 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 381 | 1 | my $parse_stack = $self->{parse_stack}; | |||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | 1 | push @$parse_stack, $self->{options}{expr_func}; | |||||||||||||||||||||||||||
| 384 | 1 | push @$parse_stack, $self->{options}{expr}; | |||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | 1 | my $result = HTML::Template::_commit_to_cache($self, @_); | |||||||||||||||||||||||||||
| 387 | } | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | 7 | 1; | |||||||||||||||||||||||||||
| 390 | __END__ | ||||||||||||||||||||||||||||