| 1 | package HP::Handy; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | ###################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | # HP::Handy - A tiny Jinja2-compatible template engine for Perl 5.5.3+ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | # https://metacpan.org/dist/HP-Handy | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | # Copyright (c) 2026 INABA Hitoshi <ina@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | ###################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | # Compatible : Perl 5.005_03 and later | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | # Platform : Windows and UNIX/Linux | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | ###################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 13 | ▶ | use 5.00503; # Universal Consensus 1998 for primetools | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | # Perl 5.005_03 compatibility for historical toolchains | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | # use 5.008001; # Lancaster Consensus 2013 for toolchains | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 13 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 0 | ▶ | BEGIN { if ($] < 5.006 && !defined(&warnings::import)) { $INC{'warnings.pm'} = 'stub'; eval 'package warnings; sub import {}' } } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=3 SCAR=16.5
Branch: 1 branch
Condition33%1/3
$] < "5.006" and not defined &warnings::import
MC/DC0%0/2
$] < "5.006" and not defined &warnings::import
$] < "5.006"
not defined &warnings::import
Truth table33%1/3
A and not(B)
A= $] < "5.006"B= not defined &warnings::import
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 13 | ▶ | use warnings; local $^W = 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 13 | ▶ | BEGIN { pop @INC if $INC[-1] eq '.' } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 13 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 13 | ▶ | use File::Spec; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 13 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 13 | $VERSION = '0.02'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 13 | $VERSION = $VERSION; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | # $VERSION self-assignment suppresses "used only once" warning under strict. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | # Built-in filters | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | my %FILTERS = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 10 | ▶ | 'upper' => sub { my $s = defined $_[0] ? $_[0] : ''; $s =~ tr/a-z/A-Z/; $s }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 2 | ▶ | 'lower' => sub { my $s = defined $_[0] ? $_[0] : ''; $s =~ tr/A-Z/a-z/; $s }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 7 | ▶ | 'trim' => sub { my $s = defined $_[0] ? $_[0] : ''; $s =~ s/^\s+|\s+$//g; $s }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 7 | ▶ | 'length' => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 7 | ▶ | return 0 unless defined $_[0]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 3 | ▶ | ref($_[0]) eq 'ARRAY' ? scalar @{$_[0]} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 6 | ▶ | : ref($_[0]) eq 'HASH' ? scalar keys %{$_[0]} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | : length($_[0]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 5 | ▶ | 'reverse' => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 5 | ▶ | return '' unless defined $_[0]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 5 | ▶ | ref($_[0]) eq 'ARRAY' ? [ reverse @{$_[0]} ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | : scalar reverse("$_[0]") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 'escape' => \&_html_escape, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 'e' => \&_html_escape, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 21 | ▶ | 'safe' => sub { bless \(my $s = defined $_[0] ? $_[0] : ''), 'HP::Handy::SafeString' }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 6 | ▶ | 'default' => sub { (defined $_[0] && $_[0] ne '') ? $_[0] : $_[1] }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
Condition100%3/3
defined $_[0] and $_[0] ne ""
MC/DC100%2/2
defined $_[0] and $_[0] ne ""
defined $_[0]
$_[0] ne ""
Truth table100%3/3
A and B
A= defined $_[0]B= $_[0] ne ""
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 1 | ▶ | 'd' => sub { (defined $_[0] && $_[0] ne '') ? $_[0] : $_[1] }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=14.2
Branch: 1 branch
Condition33%1/3
defined $_[0] and $_[0] ne ""
MC/DC0%0/2
defined $_[0] and $_[0] ne ""
defined $_[0]
$_[0] ne ""
Truth table33%1/3
A and B
A= defined $_[0]B= $_[0] ne ""
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 5 | ▶ | 'replace' => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 5 | my ($s, $from, $to) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 5 | ▶ | $s = defined $s ? $s : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 5 | ▶ | $from = defined $from ? $from : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 5 | ▶ | $to = defined $to ? $to : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 5 | $s =~ s/\Q$from\E/$to/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 5 | $s | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 5 | ▶ | 'truncate' => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.3
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 5 | my ($s, $len, $end) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 5 | ▶ | $s = defined $s ? $s : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 5 | ▶ | $len = defined $len ? int($len) : 255; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 5 | ▶ | $end = defined $end ? $end : '...'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 5 | ▶ | (length($s) > $len) ? substr($s, 0, $len) . $end : $s | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 47 | ▶ | 'join' => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=16.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 47 | my ($list, $sep) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 47 | ▶ | $sep = defined $sep ? $sep : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 47 | ▶ | ref($list) eq 'ARRAY' ? join($sep, @$list) : (defined $list ? $list : '') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 7 | ▶ | 'first' => sub { ref($_[0]) eq 'ARRAY' && @{$_[0]} ? $_[0][0] : undef }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=13.1
Branch: 1 branch
Condition33%1/3
ref $_[0] eq "ARRAY" and @{$_[0];}
MC/DC0%0/2
ref $_[0] eq "ARRAY" and @{$_[0];}
ref $_[0] eq "ARRAY"
@{$_[0];}
Truth table33%1/3
A and B
A= ref $_[0] eq "ARRAY"B= @{$_[0];}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 4 | ▶ | 'last' => sub { ref($_[0]) eq 'ARRAY' && @{$_[0]} ? $_[0][-1] : undef }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=13.1
Branch: 1 branch
Condition33%1/3
ref $_[0] eq "ARRAY" and @{$_[0];}
MC/DC0%0/2
ref $_[0] eq "ARRAY" and @{$_[0];}
ref $_[0] eq "ARRAY"
@{$_[0];}
Truth table33%1/3
A and B
A= ref $_[0] eq "ARRAY"B= @{$_[0];}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 3 | ▶ | 'list' => sub { ref($_[0]) eq 'ARRAY' ? $_[0] : [defined $_[0] ? $_[0] : ()] }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.2
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 3 | ▶ | 'abs' => sub { abs(defined $_[0] ? $_[0] : 0) }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 3 | ▶ | 'int' => sub { int(defined $_[0] ? $_[0] : 0) }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 1 | ▶ | 'float' => sub { defined $_[0] ? ($_[0] + 0.0) : 0.0 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 1 | ▶ | 'string' => sub { defined $_[0] ? "$_[0]" : '' }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 2 | ▶ | 'title' => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 2 | ▶ | my $s = defined $_[0] ? $_[0] : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 2 | $s =~ s/\b([a-z])/uc($1)/ge; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 2 | $s | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 2 | ▶ | 'capitalize' => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 2 | ▶ | my $s = defined $_[0] ? lc($_[0]) : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 2 | $s =~ s/^([a-z])/uc($1)/e; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 2 | $s | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 4 | ▶ | 'urlencode' => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 4 | ▶ | my $s = defined $_[0] ? $_[0] : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 4 | $s =~ s/([^A-Za-z0-9\-_.~])/sprintf('%%%02X', ord($1))/ge; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 4 | $s | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 4 | ▶ | 'wordcount' => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 4 | ▶ | my $s = defined $_[0] ? $_[0] : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 4 | my @w = split /\s+/, $s; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 4 | @w = grep { $_ ne '' } @w; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | scalar @w | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 4 | }, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 5 | ▶ | 'batch' => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 5 | my ($list, $size, $fill) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 5 | ▶ | return [] unless ref($list) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 5 | ▶ | $size = int($size || 1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$size || 1
MC/DC0%0/1
$size || 1
$size
Truth table50%1/2
A
A= $size
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 5 | ▶ | $size = 1 if $size < 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 5 | my @result; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 5 | my @flat = @$list; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 5 | ▶ | while (@flat) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 11 | my @chunk = splice(@flat, 0, $size); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 11 | ▶ | if (defined $fill) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 2 | ▶ | push @chunk, $fill while @chunk < $size; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 11 | push @result, \@chunk; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | \@result | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 5 | }, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 4 | ▶ | 'slice' => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 4 | my ($list, $slices) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 4 | ▶ | return [] unless ref($list) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 4 | ▶ | $slices = int($slices || 1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$slices || 1
MC/DC0%0/1
$slices || 1
$slices
Truth table50%1/2
A
A= $slices
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 4 | ▶ | $slices = 1 if $slices < 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 4 | my $total = scalar @$list; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 4 | my $per = int($total / $slices); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 4 | my $extra = $total % $slices; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 4 | my @result; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 4 | my $idx = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 4 | for my $i (0 .. $slices - 1) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 9 | ▶ | my $n = $per + ($i < $extra ? 1 : 0); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 9 | push @result, [ @{$list}[$idx .. $idx + $n - 1] ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 9 | $idx += $n; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | \@result | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 4 | }, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 8 | ▶ | 'sort' => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 8 | my ($list, $attr) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 8 | ▶ | return [] unless ref($list) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 8 | ▶ | if (defined $attr) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 1 | return [ sort { _get_attr($a, $attr) cmp _get_attr($b, $attr) } @$list ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 7 | [ sort @$list ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 3 | ▶ | 'unique' => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 3 | ▶ | return [] unless ref($_[0]) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 3 | my %seen; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 3 | [ grep { !$seen{$_}++ } @{$_[0]} ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 2 | ▶ | 'min' => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 2 | ▶ | return undef unless ref($_[0]) eq 'ARRAY' && @{$_[0]}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $_[0] eq "ARRAY" and @{$_[0];}
MC/DC0%0/2
ref $_[0] eq "ARRAY" and @{$_[0];}
ref $_[0] eq "ARRAY"
@{$_[0];}
Truth table33%1/3
A and B
A= ref $_[0] eq "ARRAY"B= @{$_[0];}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 2 | my $m = $_[0][0]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 2 | ▶ | for (@{$_[0]}) { $m = $_ if $_ < $m } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | $m | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 2 | }, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 2 | ▶ | 'max' => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 2 | ▶ | return undef unless ref($_[0]) eq 'ARRAY' && @{$_[0]}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $_[0] eq "ARRAY" and @{$_[0];}
MC/DC0%0/2
ref $_[0] eq "ARRAY" and @{$_[0];}
ref $_[0] eq "ARRAY"
@{$_[0];}
Truth table33%1/3
A and B
A= ref $_[0] eq "ARRAY"B= @{$_[0];}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 2 | my $m = $_[0][0]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 2 | ▶ | for (@{$_[0]}) { $m = $_ if $_ > $m } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | $m | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 2 | }, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 3 | ▶ | 'sum' => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 3 | ▶ | return 0 unless ref($_[0]) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 3 | my $s = 0; $s += $_ for @{$_[0]}; $s | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | 6 | ▶ | 'map' => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 6 | my ($list, $attr) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 6 | ▶ | return [] unless ref($list) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 6 | [ map { _get_attr($_, $attr) } @$list ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 2 | ▶ | 'select' => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 2 | my ($list, $attr) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 2 | ▶ | return [] unless ref($list) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 2 | [ grep { _get_attr($_, $attr) } @$list ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 1 | ▶ | 'reject' => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 1 | my ($list, $attr) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 1 | ▶ | return [] unless ref($list) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 1 | [ grep { !_get_attr($_, $attr) } @$list ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 4 | ▶ | 'count' => sub { ref($_[0]) eq 'ARRAY' ? scalar @{$_[0]} : 0 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 0 | ▶ | 'pprint' => sub { _to_json($_[0]) }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 3 | ▶ | 'nl2br' => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 3 | ▶ | my $s = defined $_[0] ? $_[0] : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 3 | $s =~ s/\n/<br>\n/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 3 | $s | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 4 | ▶ | 'striptags' => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 4 | ▶ | my $s = defined $_[0] ? $_[0] : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 4 | $s =~ s/<[^>]*>//g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 4 | $s | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 11 | ▶ | 'format' => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 11 | my ($s, $fmt) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 11 | ▶ | defined $s ? sprintf($fmt, $s) : '' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | 3 | ▶ | 'center' => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 3 | my ($s, $width) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 3 | ▶ | $s = defined $s ? $s : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 3 | ▶ | $width = defined $width ? int($width) : 80; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 3 | my $pad = $width - length($s); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 3 | ▶ | return $s if $pad <= 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 2 | my $left = int($pad / 2); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | 2 | my $right = $pad - $left; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 2 | (' ' x $left) . $s . (' ' x $right) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 2 | ▶ | 'indent' => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | 2 | my ($s, $width, $first) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 2 | ▶ | $s = defined $s ? $s : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 2 | ▶ | $width = defined $width ? int($width) : 4; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 2 | ▶ | $first = defined $first ? $first : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 2 | my $pad = ' ' x $width; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 2 | my @lines = split /\n/, $s, -1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 2 | for my $i (0 .. $#lines) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 5 | ▶ | $lines[$i] = $pad . $lines[$i] if $i > 0 || $first; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$i > 0 or $first
MC/DC100%2/2
$i > 0 or $first
$i > 0
$first
Truth table100%3/3
A or B
A= $i > 0B= $first
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 2 | join("\n", @lines) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 3 | ▶ | 'xmlattr' => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 3 | my $h = $_[0]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 3 | ▶ | return '' unless ref($h) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | join(' ', map { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 3 | ▶ | my $v = defined $h->{$_} ? $h->{$_} : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 3 | $v =~ s/&/&/g; $v =~ s/"/"/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 3 | "$_=\"$v\"" | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | } sort keys %$h) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 7 | ▶ | 'tojson' => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 7 | my $v = $_[0]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 7 | _to_json($v) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 13 | 'forceescape' => \&_html_escape, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | # Built-in tests (is_xxx) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | my %TESTS = ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 12 | ▶ | 'defined' => sub { defined $_[0] }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 7 | ▶ | 'none' => sub { !defined $_[0] }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 4 | ▶ | 'string' => sub { defined $_[0] && !ref($_[0]) }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 11 | ▶ | 'number' => sub { defined $_[0] && !ref($_[0]) && $_[0] =~ /^-?(?:\d+\.?\d*|\.\d+)$/ }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=14.2
Branch: 1 branch
Condition33%1/3
defined $_[0] and not ref $_[0]
MC/DC0%0/2
defined $_[0] and not ref $_[0]
defined $_[0]
not ref $_[0]
Truth table33%1/3
A and not(B)
A= defined $_[0]B= not ref $_[0]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 4 | ▶ | 'sequence' => sub { ref($_[0]) eq 'ARRAY' }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 2 | ▶ | 'mapping' => sub { ref($_[0]) eq 'HASH' }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 3 | ▶ | 'iterable' => sub { ref($_[0]) eq 'ARRAY' || ref($_[0]) eq 'HASH' }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | 3 | ▶ | 'callable' => sub { ref($_[0]) eq 'CODE' }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 22 | ▶ | 'odd' => sub { defined $_[0] && int($_[0]) % 2 != 0 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 13 | ▶ | 'even' => sub { defined $_[0] && int($_[0]) % 2 == 0 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 22 | ▶ | 'divisibleby' => sub { defined $_[0] && defined $_[1] && $_[1] != 0 && int($_[0]) % int($_[1]) == 0 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=19.1
Branch: 1 branch
Condition33%2/6
defined $_[0] and defined $_[1] and $_[1] != 0
defined $_[0] and defined $_[1]
MC/DC0%0/3
defined $_[0] and defined $_[1] and $_[1] != 0
defined $_[0]
defined $_[1]
$_[1] != 0
Truth table25%1/4
A and B and C
A= defined $_[0]B= defined $_[1]C= $_[1] != 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | 8 | ▶ | 'upper' => sub { defined $_[0] && $_[0] eq uc($_[0]) && $_[0] =~ /[A-Z]/ }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
Branch: 1 branch
Condition66%2/3
defined $_[0] and $_[0] eq uc $_[0]
MC/DC50%1/2
defined $_[0] and $_[0] eq uc $_[0]
defined $_[0]
$_[0] eq uc $_[0]
Truth table66%2/3
A and B
A= defined $_[0]B= $_[0] eq uc $_[0]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | 3 | ▶ | 'lower' => sub { defined $_[0] && $_[0] eq lc($_[0]) && $_[0] =~ /[a-z]/ }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
Branch: 1 branch
Condition66%2/3
defined $_[0] and $_[0] eq lc $_[0]
MC/DC50%1/2
defined $_[0] and $_[0] eq lc $_[0]
defined $_[0]
$_[0] eq lc $_[0]
Truth table66%2/3
A and B
A= defined $_[0]B= $_[0] eq lc $_[0]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 6 | ▶ | 'equalto' => sub { defined $_[0] && defined $_[1] && $_[0] eq $_[1] }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=14.2
Branch: 1 branch
Condition33%1/3
defined $_[0] and defined $_[1]
MC/DC0%0/2
defined $_[0] and defined $_[1]
defined $_[0]
defined $_[1]
Truth table33%1/3
A and B
A= defined $_[0]B= defined $_[1]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | 4 | ▶ | 'ne' => sub { !(defined $_[0] && defined $_[1] && $_[0] eq $_[1]) }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=13.1
Condition50%3/6
defined $_[0] && defined $_[1] && $_[0] eq $_[1]
defined $_[0] and defined $_[1]
MC/DC33%1/3
defined $_[0] && defined $_[1] && $_[0] eq $_[1]
defined $_[0]
defined $_[1]
$_[0] eq $_[1]
Truth table50%2/4
A and B && C
A= defined $_[0]B= defined $_[1]C= $_[0] eq $_[1]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 3 | ▶ | 'lt' => sub { defined $_[0] && defined $_[1] && $_[0] < $_[1] }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=14.2
Branch: 1 branch
Condition33%1/3
defined $_[0] and defined $_[1]
MC/DC0%0/2
defined $_[0] and defined $_[1]
defined $_[0]
defined $_[1]
Truth table33%1/3
A and B
A= defined $_[0]B= defined $_[1]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 3 | ▶ | 'le' => sub { defined $_[0] && defined $_[1] && $_[0] <= $_[1] }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=14.2
Branch: 1 branch
Condition33%1/3
defined $_[0] and defined $_[1]
MC/DC0%0/2
defined $_[0] and defined $_[1]
defined $_[0]
defined $_[1]
Truth table33%1/3
A and B
A= defined $_[0]B= defined $_[1]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | 2 | ▶ | 'gt' => sub { defined $_[0] && defined $_[1] && $_[0] > $_[1] }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=14.2
Branch: 1 branch
Condition33%1/3
defined $_[0] and defined $_[1]
MC/DC0%0/2
defined $_[0] and defined $_[1]
defined $_[0]
defined $_[1]
Truth table33%1/3
A and B
A= defined $_[0]B= defined $_[1]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | 2 | ▶ | 'ge' => sub { defined $_[0] && defined $_[1] && $_[0] >= $_[1] }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=14.2
Branch: 1 branch
Condition33%1/3
defined $_[0] and defined $_[1]
MC/DC0%0/2
defined $_[0] and defined $_[1]
defined $_[0]
defined $_[1]
Truth table33%1/3
A and B
A= defined $_[0]B= defined $_[1]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 23 | ▶ | 'in' => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 23 | my ($val, $container) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | 23 | ▶ | return 0 unless defined $val && defined $container; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $val and defined $container
MC/DC50%1/2
defined $val and defined $container
defined $val
defined $container
Truth table66%2/3
A and B
A= defined $valB= defined $container
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 19 | ▶ | if (ref($container) eq 'ARRAY') { return (grep { defined $_ && $_ eq $val } @$container) ? 1 : 0 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
defined $_ && $_ eq $val
MC/DC50%1/2
defined $_ && $_ eq $val
defined $_
$_ eq $val
Truth table66%2/3
A && B
A= defined $_B= $_ eq $val
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 10 | ▶ | if (ref($container) eq 'HASH') { return exists $container->{$val} ? 1 : 0 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 6 | ▶ | return index($container, $val) >= 0 ? 1 : 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | 13 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | # Constructor | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | 711 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=13 SCAR=25.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 711 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | # new(key => value, ...) or new({ key => value, ... }) (0.02) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | # Jinja2's Environment and many Perl modules take a hash reference; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | # accept both so that either style works. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 711 | ▶ | my %args = (@_ == 1 && ref($_[0]) eq 'HASH') ? %{$_[0]} : @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@_ == 1 and ref $_[0] eq "HASH"
MC/DC50%1/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1
ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | my $self = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | template_dir => defined $args{template_dir} ? $args{template_dir} : '.', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | auto_escape => defined $args{auto_escape} ? $args{auto_escape} : 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | trim_blocks => defined $args{trim_blocks} ? $args{trim_blocks} : 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | lstrip_blocks => defined $args{lstrip_blocks} ? $args{lstrip_blocks} : 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | block_start => defined $args{block_start} ? $args{block_start} : '{%', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | block_end => defined $args{block_end} ? $args{block_end} : '%}', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | var_start => defined $args{var_start} ? $args{var_start} : '{{', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | var_end => defined $args{var_end} ? $args{var_end} : '}}', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | comment_start => defined $args{comment_start} ? $args{comment_start} : '{#', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | 711 | ▶ | comment_end => defined $args{comment_end} ? $args{comment_end} : '#}', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 10 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | _filters => { %FILTERS }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | _tests => { %TESTS }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | _blocks => {}, # block name => template string (for inheritance) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | _macros => {}, # macro name => { args=>[], defaults=>[], body=>'' } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | _extends => '', # parent template name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | 711 | bless $self, $class; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 711 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | # add_filter - Register a custom filter | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | 7 | ▶ | sub add_filter { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_filter: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | 7 | my ($self, $name, $code) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | 7 | ▶ | croak "add_filter: name required" unless defined $name; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | 7 | ▶ | croak "add_filter: code must be coderef" unless ref($code) eq 'CODE'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | 6 | $self->{_filters}{$name} = $code; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | 6 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | # add_test - Register a custom test | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | 4 | ▶ | sub add_test { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_test: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | 4 | my ($self, $name, $code) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | 4 | ▶ | croak "add_test: name required" unless defined $name; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | 4 | ▶ | croak "add_test: code must be coderef" unless ref($code) eq 'CODE'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | 4 | $self->{_tests}{$name} = $code; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | 4 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | # render_file - Render a template file with variables | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | 12 | ▶ | sub render_file { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
render_file: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | 12 | my ($self, $filename, $vars) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | 12 | ▶ | $vars = {} unless defined $vars; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | 12 | my $source = $self->_load_file($filename); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | 10 | return $self->_render($source, $vars, $filename); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | # render_string - Render a template string with variables | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | 694 | ▶ | sub render_string { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
render_string: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | 694 | my ($self, $source, $vars) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | 694 | ▶ | $vars = {} unless defined $vars; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | 694 | return $self->_render($source, $vars, '<string>'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | # _load_file - Read template from disk | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | 27 | ▶ | sub _load_file { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_load_file: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | 27 | my ($self, $filename) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | # Prevent path traversal | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | 27 | ▶ | if ($filename =~ /\.\./) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | 1 | croak "HP::Handy: path traversal not allowed in '$filename'"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | 26 | my $path; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | 26 | ▶ | if (File::Spec->file_name_is_absolute($filename)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | 0 | $path = $filename; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | 26 | $path = File::Spec->catfile($self->{template_dir}, $filename); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | 26 | local *HPHANDY_FH; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | 26 | ▶ | open(HPHANDY_FH, "< $path") or croak "HP::Handy: cannot open '$path': $!"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | 24 | local $/; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | 24 | my $source = <HPHANDY_FH>; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | 24 | close(HPHANDY_FH); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | 24 | ▶ | return defined $source ? $source : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | # _render - Core rendering engine | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | 704 | ▶ | sub _render { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_render: called CC=10 SCAR=23.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | 704 | my ($self, $source, $vars, $filename) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | 704 | ▶ | $filename = defined $filename ? $filename : '<string>'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | # Reset per-render state | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | 704 | $self->{_blocks} = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | 704 | $self->{_macros} = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | 704 | $self->{_extends} = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | # First pass: collect blocks and macros, detect extends | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | 704 | $source = $self->_preprocess($source, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | # If extends was found, do inheritance (iteratively for multi-level) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | 704 | ▶ | while ($self->{_extends} ne '') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | 7 | my $parent_src = $self->_load_file($self->{_extends}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | 7 | $self->{_extends} = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | # Collect parent blocks/macros without overwriting child definitions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | 7 | $parent_src = $self->_preprocess_parent($parent_src, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | # Apply child blocks to parent template | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | 7 | $source = $self->_apply_inheritance($parent_src, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | # Now resolve all known blocks so deeper levels see merged content. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | # For each block in _blocks, expand any other known blocks inside it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | 7 | my $merged = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | 7 | my $passes = 10; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | 7 | ▶ | while ($merged && $passes-- > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$merged and $passes-- > 0
MC/DC50%1/2
$merged and $passes-- > 0
$merged
$passes-- > 0
Truth table66%2/3
A and B
A= $mergedB= $passes-- > 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | 7 | $merged = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | 7 | for my $bname (keys %{$self->{_blocks}}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | 10 | my $prev = $self->{_blocks}{$bname}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | 10 | my $val = $prev; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | 10 | my $lim2 = 10; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | 10 | ▶ | while ($lim2-- > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | 10 | my $prev2 = $val; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | 10 | $val =~ s/\{%-?\s*block\s+(\w+)\s*-?%\}((?:(?!\{%-?\s*block\b)[\s\S])*?)\{%-?\s*endblock(?:\s+\1)?\s*-?%\}/ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | 0 | ▶ | exists $self->{_blocks}{$1} ? $self->{_blocks}{$1} : "\x00BLK\x01$1\x01$2\x00EBLK\x00" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | /gse; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | 10 | ▶ | last if $val eq $prev2; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | 10 | $val =~ s/\x00BLK\x01(\w+)\x01(.*?)\x00EBLK\x00/$2/gs; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | 10 | $val =~ s/\{%-?\s*endblock(?:\s+\w+)?\s*-?%\}//g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | 10 | ▶ | if ($val ne $prev) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | 0 | $self->{_blocks}{$bname} = $val; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | 0 | $merged = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | 704 | return $self->_eval_template($source, $vars, $filename); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | # _preprocess - Scan for extends/block/macro; strip them for non-inherited use | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | 704 | ▶ | sub _preprocess { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_preprocess: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | 704 | my ($self, $source, $vars) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | # Detect {% extends "..." %} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | 704 | ▶ | if ($source =~ s/\{%-?\s*extends\s+["']([^"']+)["']\s*-?%\}[ \t]*\n?//) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | 6 | $self->{_extends} = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | # Collect {% macro name(args) %}...{% endmacro %} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | 704 | ▶ | while ($source =~ s/\{%-?\s*macro\s+(\w+)\s*\(([^)]*)\)\s*-?%\}(.*?)\{%-?\s*endmacro\s*-?%\}//s) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | 10 | my ($mname, $argstr, $body) = ($1, $2, $3); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | 10 | my (@args, @defaults); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | 10 | for my $a (split /\s*,\s*/, $argstr) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | 16 | $a =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | 16 | ▶ | next if $a eq ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | 16 | ▶ | if ($a =~ /^(\w+)\s*=\s*(.+)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | 2 | push @args, $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | 2 | push @defaults, $2; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | 14 | push @args, $a; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | 14 | push @defaults, undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | 10 | $self->{_macros}{$mname} = { args => \@args, defaults => \@defaults, body => $body }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | # Collect {% block name %}...{% endblock %} from child | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | 704 | ▶ | while ($source =~ s/\{%-?\s*block\s+(\w+)\s*-?%\}(.*?)\{%-?\s*endblock(?:\s+\1)?\s*-?%\}/$self->_store_block($1, $2)/se) {} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | 704 | return $source; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | 10 | ▶ | sub _store_block { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_store_block: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | 10 | my ($self, $name, $content) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | 10 | ▶ | $self->{_blocks}{$name} = $content unless exists $self->{_blocks}{$name}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | 10 | return ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | # _preprocess_parent - Scan parent for extends/macro; collect NEW blocks only | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | # (does NOT strip block tags -- those must survive for _apply_inheritance) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | 7 | ▶ | sub _preprocess_parent { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_preprocess_parent: called CC=8 SCAR=30.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | 7 | my ($self, $source, $vars) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 465 | # Detect {% extends "..." %} in parent (for multi-level) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 466 | 7 | ▶ | if ($source =~ s/\{%-?\s*extends\s+["']([^"']+)["']\s*-?%\}[ \t]*\n?//) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | 1 | $self->{_extends} = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | # Collect macros from parent (without overwriting child macros) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | 7 | ▶ | while ($source =~ s/\{%-?\s*macro\s+(\w+)\s*\(([^)]*)\)\s*-?%\}(.*?)\{%-?\s*endmacro\s*-?%\}//s) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | 0 | my ($mname, $argstr, $body) = ($1, $2, $3); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 473 | 0 | ▶ | next if exists $self->{_macros}{$mname}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 474 | 0 | my (@args, @defaults); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 475 | 0 | for my $a (split /\s*,\s*/, $argstr) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 476 | 0 | $a =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 477 | 0 | ▶ | next if $a eq ""; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 478 | 0 | ▶ | if ($a =~ /^(\w+)\s*=\s*(.+)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 479 | 0 | push @args, $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 480 | 0 | push @defaults, $2; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 481 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 482 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 483 | 0 | push @args, $a; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 484 | 0 | push @defaults, undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 485 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 486 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 487 | 0 | $self->{_macros}{$mname} = { args => \@args, defaults => \@defaults, body => $body }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 488 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 490 | # Collect NEW blocks (without overwriting child blocks). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 491 | # Expand already-known child blocks within the body before storing. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 492 | 7 | my $tmp = $source; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 493 | 7 | ▶ | while ($tmp =~ s/\{%-?\s*block\s+(\w+)\s*-?%\}(.*)\{%-?\s*endblock(?:\s+\1)?\s*-?%\}/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 494 | 7 | $self->_store_block_expanded($1, $2) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 495 | /se) {} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 497 | 7 | return $source; # keep block tags intact for _apply_inheritance | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 498 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 500 | 7 | ▶ | sub _store_block_expanded { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_store_block_expanded: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 501 | 7 | my ($self, $name, $body) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 502 | 7 | ▶ | unless (exists $self->{_blocks}{$name}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 503 | # Expand already-resolved child blocks inside this body. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 504 | # Use _apply_inheritance logic on the snippet. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 505 | 1 | my $expanded = $body; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 506 | 1 | my $lim = 10; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 507 | 1 | ▶ | while ($lim-- > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 508 | 2 | my $prev = $expanded; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 509 | # Replace innermost (non-nested) blocks that are known | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 510 | 2 | $expanded =~ s/\{%-?\s*block\s+(\w+)\s*-?%\}((?:(?!\{%-?\s*block\b)[\s\S])*?)\{%-?\s*endblock(?:\s+\1)?\s*-?%\}/ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 511 | 1 | ▶ | exists $self->{_blocks}{$1} ? $self->{_blocks}{$1} : "__BLK__" . $1 . "__SEP__" . $2 . "__EBLK__" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 512 | /gse; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 513 | 2 | ▶ | last if $expanded eq $prev; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 514 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 515 | # Clean up any un-expanded block tags | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 516 | 1 | $expanded =~ s/__BLK__(\w+)__SEP__(.*?)__EBLK__/$2/gs; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 517 | 1 | $expanded =~ s/\{%-?\s*endblock(?:\s+\w+)?\s*-?%\}//g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 518 | 1 | $self->{_blocks}{$name} = $expanded; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 519 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 520 | 7 | return ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 521 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 524 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 525 | # _apply_inheritance - Render parent with child blocks | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 526 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 527 | 7 | ▶ | sub _apply_inheritance { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_apply_inheritance: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 528 | 7 | my ($self, $parent_src, $vars) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 530 | # Iteratively substitute innermost (non-nested) blocks first. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 531 | # A block body that contains no other {% block %} tags is "innermost". | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 532 | 7 | my $limit = 20; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 533 | 7 | ▶ | while ($limit-- > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 534 | 15 | my $changed = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 535 | 15 | $parent_src =~ s/\{%-?\s*block\s+(\w+)\s*-?%\}((?:(?!\{%-?\s*block\b)[\s\S])*?)\{%-?\s*endblock(?:\s+\1)?\s*-?%\}/ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 536 | 10 | ▶ | do { $changed = 1; exists $self->{_blocks}{$1} ? $self->{_blocks}{$1} : $2 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 537 | /gse; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 538 | 15 | ▶ | last unless $changed; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 539 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 541 | # Remove any orphaned {% endblock %} left from outer block substitutions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 542 | 7 | $parent_src =~ s/\{%-?\s*endblock(?:\s+\w+)?\s*-?%\}//g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 544 | 7 | return $parent_src; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 545 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 546 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 547 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 548 | # _eval_template - Evaluate template directives recursively | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 549 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 550 | 723 | ▶ | sub _eval_template { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_eval_template: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 551 | 723 | my ($self, $source, $vars, $filename) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 552 | 723 | ▶ | $filename = defined $filename ? $filename : '<string>'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 553 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 554 | # Apply trim_blocks / lstrip_blocks | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 555 | 723 | ▶ | if ($self->{trim_blocks}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 556 | 8 | $source =~ s/(%\}|#\})[ \t]*\n/$1\n/g; # already minimal; remove newline after tag | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 557 | 8 | $source =~ s/(%\}|#\})\n/$1/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 558 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 559 | 723 | ▶ | if ($self->{lstrip_blocks}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 560 | 8 | $source =~ s/^[ \t]*(\{[%#])/$1/mg; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 561 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 562 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 563 | 723 | my $bs = quotemeta($self->{block_start}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 564 | 723 | my $be = quotemeta($self->{block_end}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 565 | 723 | my $vs = quotemeta($self->{var_start}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 566 | 723 | my $ve = quotemeta($self->{var_end}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 567 | 723 | my $cs = quotemeta($self->{comment_start}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 568 | 723 | my $ce = quotemeta($self->{comment_end}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 570 | # Protect {% raw %}...{% endraw %} blocks BEFORE comment stripping, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 571 | # so that {# ... #} inside raw blocks is preserved as-is. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 572 | 723 | my @raw_chunks; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 573 | 723 | my $raw_ph = "\x00RAW\x00"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 574 | 723 | my $bs_re = quotemeta($self->{block_start}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 575 | 723 | my $be_re = quotemeta($self->{block_end}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 576 | 723 | $source =~ s/${bs_re}-?\s*raw\s*-?${be_re}(.*?)${bs_re}-?\s*endraw\s*-?${be_re}/ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 577 | 9 | my $idx = scalar @raw_chunks; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 578 | 9 | push @raw_chunks, $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 579 | 9 | "${raw_ph}${idx}\x00" | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 580 | /gse; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 582 | # Remove comments (after raw protection so {# #} inside raw is safe) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 583 | 723 | $source =~ s/$cs.*?$ce//gs; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 585 | # Tokenise into chunks | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 586 | # Tokens: [ 'text', $str ] or [ 'var', $expr ] or [ 'tag', $stmt ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 587 | 723 | my @tokens = $self->_tokenize($source); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 589 | # Restore raw chunks in text tokens | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 590 | 723 | for my $tok (@tokens) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 591 | 1763 | ▶ | if ($tok->[0] eq 'text' && $tok->[1] =~ /\Q$raw_ph\E/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$tok->[0] eq "text" and $tok->[1] =~ /\Q$raw_ph\E/
MC/DC100%2/2
$tok->[0] eq "text" and $tok->[1] =~ /\Q$raw_ph\E/
$tok->[0] eq "text"
$tok->[1] =~ /\Q$raw_ph\E/
Truth table100%3/3
A and B
A= $tok->[0] eq "text"B= $tok->[1] =~ /\Q$raw_ph\E/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 592 | 9 | $tok->[1] =~ s/\Q${raw_ph}\E(\d+)\x00/$raw_chunks[$1]/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 593 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 594 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 596 | 723 | return $self->_eval_tokens(\@tokens, $vars, 0, $filename); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 597 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 598 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 599 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 600 | # _tokenize - Split source into text/var/tag tokens | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 601 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 602 | 723 | ▶ | sub _tokenize { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_tokenize: called CC=28 SCAR=33.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 603 | 723 | my ($self, $source) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 604 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 605 | 723 | my $bs = $self->{block_start}; # {% | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 606 | 723 | my $be = $self->{block_end}; # %} | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 607 | 723 | my $vs = $self->{var_start}; # {{ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 608 | 723 | my $ve = $self->{var_end}; # }} | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 609 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 610 | # Build split pattern | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 611 | 723 | my $qbs = quotemeta($bs); my $qbe = quotemeta($be); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 612 | 723 | my $qvs = quotemeta($vs); my $qve = quotemeta($ve); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 613 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 614 | 723 | my @tokens; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 615 | 723 | my $pos = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 616 | 723 | my $len = length($source); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 617 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 618 | 723 | ▶ | while ($pos < $len) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 619 | # Find next tag start | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 620 | 1404 | my $var_pos = index($source, $vs, $pos); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 621 | 1404 | my $block_pos = index($source, $bs, $pos); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 622 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 623 | # No more tags | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 624 | 1404 | ▶ | if ($var_pos < 0 && $block_pos < 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$var_pos < 0 and $block_pos < 0
MC/DC100%2/2
$var_pos < 0 and $block_pos < 0
$var_pos < 0
$block_pos < 0
Truth table100%3/3
A and B
A= $var_pos < 0B= $block_pos < 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 625 | 52 | push @tokens, [ 'text', substr($source, $pos) ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 626 | 52 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 627 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 628 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 629 | # Determine which comes first | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 630 | 1352 | my $next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 631 | 1352 | ▶ | if ($var_pos >= 0 && ($block_pos < 0 || $var_pos <= $block_pos)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$var_pos >= 0 and $block_pos < 0 || $var_pos <= $block_pos
$block_pos < 0 or $var_pos <= $block_pos
MC/DC100%3/3
$var_pos >= 0 and $block_pos < 0 || $var_pos <= $block_pos
$var_pos >= 0
$block_pos < 0
$var_pos <= $block_pos
Truth table100%4/4
A and B or C
A= $var_pos >= 0B= $block_pos < 0C= $var_pos <= $block_pos
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 632 | 494 | $next = $var_pos; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 633 | 494 | my $end = index($source, $ve, $next + length($vs)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 634 | 494 | ▶ | if ($end < 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 635 | 0 | push @tokens, [ 'text', substr($source, $pos) ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 636 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 637 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 638 | 494 | ▶ | if ($next > $pos) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 639 | 38 | push @tokens, [ 'text', substr($source, $pos, $next - $pos) ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 640 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 641 | 494 | my $raw_expr = substr($source, $next + length($vs), $end - $next - length($vs)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 642 | # Whitespace control dash: "{{- expr -}}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 643 | # The dash must be the very first/last non-whitespace character AND | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 644 | # must be followed/preceded by whitespace (not part of the expression). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 645 | # Rule: if raw_expr starts with "-" followed by space/end, it's WS control. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 646 | # "-5" or "-x" is a negative expression, NOT whitespace control. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 647 | 494 | my $strip_left = ($raw_expr =~ /^-(?:\s|$)/); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 648 | 494 | my $strip_right = ($raw_expr =~ /(?:^|\s)-\s*$/); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 649 | 494 | my $expr = $raw_expr; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 650 | 494 | $expr =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 651 | 494 | ▶ | $expr =~ s/^-\s+// if $strip_left; # remove leading "- " | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 652 | 494 | ▶ | $expr =~ s/\s+-$// if $strip_right; # remove trailing " -" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 653 | # Strip whitespace from preceding text token if {{- }} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 654 | 494 | ▶ | if ($strip_left && @tokens && $tokens[-1][0] eq 'text') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$strip_left and @tokens and $tokens[-1][0] eq "text"
$strip_left and @tokens
MC/DC66%2/3
$strip_left and @tokens and $tokens[-1][0] eq "text"
$strip_left
@tokens
$tokens[-1][0] eq "text"
Truth table75%3/4
A and B and C
A= $strip_leftB= @tokensC= $tokens[-1][0] eq "text"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 655 | 6 | $tokens[-1][1] =~ s/\s+$//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 656 | 6 | ▶ | splice @tokens, -1 if $tokens[-1][1] eq ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 657 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 658 | 494 | push @tokens, [ 'var', $expr, $strip_right ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 659 | 494 | $pos = $end + length($ve); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 660 | # Strip leading whitespace from next text if {{ -}} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 661 | 494 | ▶ | if ($strip_right && $pos < $len) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$strip_right and $pos < $len
MC/DC100%2/2
$strip_right and $pos < $len
$strip_right
$pos < $len
Truth table100%3/3
A and B
A= $strip_rightB= $pos < $len
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 662 | 6 | my $rest = substr($source, $pos); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 663 | 6 | my $stripped = $rest; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 664 | 6 | $stripped =~ s/^\s+//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 665 | 6 | $pos += length($rest) - length($stripped); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 666 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 667 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 668 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 669 | 858 | $next = $block_pos; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 670 | 858 | my $end = index($source, $be, $next + length($bs)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 671 | 858 | ▶ | if ($end < 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 672 | 0 | push @tokens, [ 'text', substr($source, $pos) ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 673 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 674 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 675 | 858 | ▶ | if ($next > $pos) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 676 | # Whitespace control: {% - strips preceding whitespace | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 677 | 325 | my $text_before = substr($source, $pos, $next - $pos); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 678 | 325 | my $stmt_raw = substr($source, $next + length($bs), $end - $next - length($bs)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 679 | 325 | ▶ | if ($stmt_raw =~ /^-/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 680 | 2 | $text_before =~ s/\s+$//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 681 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 682 | 325 | ▶ | push @tokens, [ 'text', $text_before ] if $text_before ne ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 683 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 684 | 858 | my $stmt_full = substr($source, $next + length($bs), $end - $next - length($bs)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 685 | 858 | my $rstrip_tag = ($stmt_full =~ /\s*-\s*$/); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 686 | 858 | my $stmt = $stmt_full; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 687 | 858 | $stmt =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 688 | 858 | $stmt =~ s/^-\s*|\s*-$//g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 689 | 858 | push @tokens, [ 'tag', $stmt ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 690 | 858 | $pos = $end + length($be); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 691 | # Whitespace control: -%} strips following whitespace | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 692 | 858 | ▶ | if ($rstrip_tag && $pos < $len) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$rstrip_tag and $pos < $len
MC/DC50%1/2
$rstrip_tag and $pos < $len
$rstrip_tag
$pos < $len
Truth table66%2/3
A and B
A= $rstrip_tagB= $pos < $len
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 693 | 2 | substr($source, $pos) =~ s/^\s+//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 694 | 2 | ▶ | $pos = $len - length($source) + $pos if $source =~ s/^(\s+)//; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 695 | # Simpler: just advance pos past leading whitespace | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 696 | 2 | my $rest = substr($source, $pos); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 697 | 2 | my $stripped = $rest; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 698 | 2 | $stripped =~ s/^\s+//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 699 | 2 | $pos += length($rest) - length($stripped); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 700 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 701 | # trim_blocks: eat newline after %} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 702 | 858 | ▶ | if (!$rstrip_tag && $self->{trim_blocks} && $pos < $len && substr($source, $pos, 1) eq "\n") { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition88%8/9
not $rstrip_tag and $self->{"trim_blocks"} and $pos < $len and substr($source, $pos, 1) eq "\n"
not $rstrip_tag and $self->{"trim_blocks"} and $pos < $len
not $rstrip_tag and $self->{"trim_blocks"}
MC/DC0%0/4
not $rstrip_tag and $self->{"trim_blocks"} and $pos < $len and substr($source, $pos, 1) eq "\n"
not $rstrip_tag
$self->{"trim_blocks"}
$pos < $len
substr($source, $pos, 1) eq "\n"
Truth table80%4/5
not(A) and B and C and D
A= not $rstrip_tagB= $self->{"trim_blocks"}C= $pos < $lenD= substr($source, $pos, 1) eq "\n"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 703 | 0 | $pos++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 704 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 705 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 706 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 707 | 723 | return @tokens; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 708 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 709 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 710 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 711 | # _eval_tokens - Execute a token list and return rendered string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 712 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 713 | 1295 | ▶ | sub _eval_tokens { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_eval_tokens: called CC=31 SCAR=43.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 714 | 1295 | my ($self, $tokens, $vars, $start, $filename) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 715 | 1295 | ▶ | $start = 0 unless defined $start; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 716 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 717 | 1295 | my $out = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 718 | 1295 | my $i = $start; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 719 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 720 | 1295 | ▶ | while ($i <= $#$tokens) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 721 | 1568 | my ($type, $content) = @{$tokens->[$i]}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 722 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 723 | 1568 | ▶ | if ($type eq 'text') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 724 | 390 | $out .= $content; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 725 | 390 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 726 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 727 | elsif ($type eq 'var') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 728 | 679 | my $val = $self->_eval_expr($content, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 729 | 679 | ▶ | if (ref($val) eq 'HP::Handy::SafeString') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 730 | 20 | $val = $$val; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 731 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 732 | ▶ | elsif ($self->{auto_escape} && defined $val && !ref($val)) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%4/6
$self->{"auto_escape"} and defined $val and not ref $val
$self->{"auto_escape"} and defined $val
MC/DC33%1/3
$self->{"auto_escape"} and defined $val and not ref $val
$self->{"auto_escape"}
defined $val
not ref $val
Truth table50%2/4
A and B and not(C)
A= $self->{"auto_escape"}B= defined $valC= not ref $val
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 733 | 26 | $val = _html_escape($val); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 734 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 735 | 679 | ▶ | $out .= defined $val ? $val : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 736 | 679 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 737 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 738 | elsif ($type eq 'tag') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 739 | 499 | my $stmt = $content; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 740 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 741 | # --- set --- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 742 | 499 | ▶ | if ($stmt =~ /^set\s+(\w+)\s*=\s*(.+)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 10 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 743 | 54 | my ($vname, $expr) = ($1, $2); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 744 | 54 | $vars->{$vname} = $self->_eval_expr($expr, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 745 | 54 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 746 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 747 | # --- set block (multi-line) --- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 748 | elsif ($stmt =~ /^set\s+(\w+)\s*$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 749 | 0 | my $vname = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 750 | 0 | my ($body, $ni) = $self->_collect_until($tokens, $i + 1, 'endset'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 751 | 0 | $vars->{$vname} = $self->_eval_tokens($tokens, $vars, $i + 1, $filename); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 752 | # re-render just the body tokens | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 753 | 0 | my @body_tokens = @{$tokens}[$i+1 .. $ni-1]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 754 | 0 | $vars->{$vname} = $self->_eval_tokens(\@body_tokens, $vars, 0, $filename); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 755 | 0 | $i = $ni + 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 756 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 757 | # --- if / elif / else / endif --- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 758 | elsif ($stmt =~ /^if\s+(.+)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 759 | 298 | my $cond_expr = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 760 | 298 | my ($result, $ni) = $self->_eval_if($tokens, $i, $vars, $filename); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 761 | 298 | $out .= $result; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 762 | 298 | $i = $ni; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 763 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 764 | # --- for / endfor --- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 765 | elsif ($stmt =~ /^for\s+(.+?)\s+in\s+(.+?)(?:\s+if\s+(.+))?$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 766 | 126 | my ($loop_var, $iter_expr, $cond_expr) = ($1, $2, $3); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 767 | 126 | my ($result, $ni) = $self->_eval_for($tokens, $i, $vars, $loop_var, $iter_expr, $cond_expr, $filename); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 768 | 126 | $out .= $result; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 769 | 126 | $i = $ni; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 770 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 771 | # --- include --- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 772 | elsif ($stmt =~ /^include\s+["']([^"']+)["'](?:\s+ignore\s+missing)?$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 773 | 8 | my $inc_file = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 774 | 8 | my $inc_src; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 775 | 8 | eval { $inc_src = $self->_load_file($inc_file) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 776 | 8 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 777 | 1 | ▶ | $inc_src = '' if $stmt =~ /ignore\s+missing/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 778 | 1 | ▶ | croak $@ unless $stmt =~ /ignore\s+missing/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 779 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 780 | 8 | ▶ | $out .= $self->_eval_template($inc_src, { %$vars }, $inc_file) if defined $inc_src; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 781 | 8 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 782 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 783 | # --- call macro --- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 784 | ▶ | elsif ($stmt =~ /^(\w+)\s*\(([^)]*)\)$/ && exists $self->{_macros}{$1}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$stmt =~ /^(\w+)\s*\(([^)]*)\)$/ and exists $self->{"_macros"}{$1}
MC/DC0%0/2
$stmt =~ /^(\w+)\s*\(([^)]*)\)$/ and exists $self->{"_macros"}{$1}
$stmt =~ /^(\w+)\s*\(([^)]*)\)$/
exists $self->{"_macros"}{$1}
Truth table33%1/3
A and B
A= $stmt =~ /^(\w+)\s*\(([^)]*)\)$/B= exists $self->{"_macros"}{$1}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 785 | 0 | my ($mname, $argstr) = ($1, $2); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 786 | 0 | $out .= $self->_call_macro($mname, $argstr, $vars, $filename); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 787 | 0 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 788 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 789 | # --- raw / endraw --- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 790 | elsif ($stmt eq 'raw') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 791 | 0 | my ($raw, $ni) = $self->_collect_raw($tokens, $i + 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 792 | 0 | $out .= $raw; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 793 | 0 | $i = $ni + 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 794 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 795 | # --- with / endwith --- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 796 | elsif ($stmt =~ /^with(?:\s+(.+))?$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 797 | 13 | my $assigns = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 798 | 13 | my %local_vars = %$vars; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 799 | 13 | ▶ | if (defined $assigns) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 800 | 13 | for my $pair (_split_args($assigns)) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 801 | 15 | ▶ | if ($pair =~ /^(\w+)\s*=\s*(.+)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 802 | 15 | $local_vars{$1} = $self->_eval_expr($2, { %local_vars }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 803 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 804 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 805 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 806 | 13 | my ($body_tokens_ref, $ni) = $self->_collect_until($tokens, $i + 1, 'endwith'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 807 | 13 | $out .= $self->_eval_tokens($body_tokens_ref, { %local_vars }, 0, $filename); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 808 | 13 | $i = $ni + 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 809 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 810 | # --- block (standalone, no inheritance) --- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 811 | elsif ($stmt =~ /^block\s+(\w+)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 812 | 0 | my $bname = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 813 | 0 | my ($body_tokens_ref, $ni) = $self->_collect_until($tokens, $i + 1, "endblock $bname", 'endblock'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 814 | 0 | ▶ | if (exists $self->{_blocks}{$bname}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 815 | 0 | $out .= $self->_eval_template($self->{_blocks}{$bname}, $vars, $filename); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 816 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 817 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 818 | 0 | $out .= $self->_eval_tokens($body_tokens_ref, $vars, 0, $filename); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 819 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 820 | 0 | $i = $ni + 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 821 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 822 | # skip endxxx tags (consumed by their openers above) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 823 | ▶ | elsif ($stmt =~ /^end(if|for|block|macro|raw|with|set)/ || $stmt =~ /^(else|elif)/) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$stmt =~ /^end(if|for|block|macro|raw|with|set)/ or $stmt =~ /^(else|elif)/
MC/DC0%0/2
$stmt =~ /^end(if|for|block|macro|raw|with|set)/ or $stmt =~ /^(else|elif)/
$stmt =~ /^end(if|for|block|macro|raw|with|set)/
$stmt =~ /^(else|elif)/
Truth table0%0/3
A or B
A= $stmt =~ /^end(if|for|block|macro|raw|with|set)/B= $stmt =~ /^(else|elif)/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 824 | 0 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 825 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 826 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 827 | # Unknown tag: pass through as-is for forward compatibility | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 828 | 0 | $out .= $self->{block_start} . ' ' . $stmt . ' ' . $self->{block_end}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 829 | 0 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 830 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 831 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 832 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 833 | 0 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 834 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 835 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 836 | 1295 | return $out; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 837 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 838 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 839 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 840 | # _eval_if - Evaluate if/elif/else/endif block | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 841 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 842 | 298 | ▶ | sub _eval_if { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_eval_if: called CC=15 SCAR=27.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 843 | 298 | my ($self, $tokens, $start, $vars, $filename) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 844 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 845 | # Collect all branches: [ [cond_expr, [tokens...]], ..., [undef, [tokens...]] ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 846 | 298 | my @branches; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 847 | 298 | my $cur_cond = $tokens->[$start][1]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 848 | 298 | $cur_cond =~ s/^if\s+//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 849 | 298 | my @cur_body; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 850 | 298 | my $depth = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 851 | 298 | my $i = $start + 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 852 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 853 | 298 | ▶ | while ($i <= $#$tokens) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 854 | 703 | my ($type, $content) = @{$tokens->[$i]}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 855 | 703 | ▶ | if ($type eq 'tag') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 856 | 364 | ▶ | if ($content =~ /^if\b/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 857 | 8 | $depth++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 858 | 8 | push @cur_body, $tokens->[$i]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 859 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 860 | elsif ($content eq 'endif') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 861 | 306 | $depth--; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 862 | 306 | ▶ | if ($depth == 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 863 | 298 | push @branches, [ $cur_cond, [@cur_body] ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 864 | 298 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 865 | 298 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 866 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 867 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 868 | 8 | push @cur_body, $tokens->[$i]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 869 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 870 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 871 | ▶ | elsif ($depth == 1 && $content =~ /^elif\s+(.+)$/) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$depth == 1 and $content =~ /^elif\s+(.+)$/
MC/DC100%2/2
$depth == 1 and $content =~ /^elif\s+(.+)$/
$depth == 1
$content =~ /^elif\s+(.+)$/
Truth table100%3/3
A and B
A= $depth == 1B= $content =~ /^elif\s+(.+)$/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 872 | 13 | push @branches, [ $cur_cond, [@cur_body] ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 873 | 13 | $cur_cond = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 874 | 13 | @cur_body = (); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 875 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 876 | ▶ | elsif ($depth == 1 && $content eq 'else') { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$depth == 1 and $content eq "else"
MC/DC100%2/2
$depth == 1 and $content eq "else"
$depth == 1
$content eq "else"
Truth table100%3/3
A and B
A= $depth == 1B= $content eq "else"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 877 | 26 | push @branches, [ $cur_cond, [@cur_body] ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 878 | 26 | $cur_cond = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 879 | 26 | @cur_body = (); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 880 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 881 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 882 | 11 | push @cur_body, $tokens->[$i]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 883 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 884 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 885 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 886 | 339 | push @cur_body, $tokens->[$i]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 887 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 888 | 405 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 889 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 890 | 298 | ▶ | push @branches, [ $cur_cond, [@cur_body] ] if @cur_body || !defined $cur_cond; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@cur_body or not defined $cur_cond
MC/DC0%0/2
@cur_body or not defined $cur_cond
@cur_body
not defined $cur_cond
Truth table33%1/3
A or not(B)
A= @cur_bodyB= not defined $cur_cond
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 891 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 892 | 298 | my $result = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 893 | 298 | for my $branch (@branches) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 894 | 394 | my ($cond, $body) = @$branch; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 895 | 394 | ▶ | if (!defined $cond || _truthy($self->_eval_expr($cond, $vars))) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $cond or _truthy($self->_eval_expr($cond, $vars))
MC/DC100%2/2
not defined $cond or _truthy($self->_eval_expr($cond, $vars))
not defined $cond
_truthy($self->_eval_expr($cond, $vars))
Truth table100%3/3
not(A) or B
A= not defined $condB= _truthy($self->_eval_expr($cond, $vars))
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 896 | 225 | $result = $self->_eval_tokens($body, $vars, 0, $filename); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 897 | 225 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 898 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 899 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 900 | 298 | return ($result, $i); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 901 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 902 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 903 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 904 | # _truthy - Truth value of a template value (0.02) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 905 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 906 | # Jinja2 treats an empty list and an empty dict as false. In Perl a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 907 | # reference is always true, so 0.01 treated [] and {} as true. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 908 | # Scalars keep Perl's rule (undef, '' and '0' are false). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 909 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 910 | 552 | ▶ | sub _truthy { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_truthy: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 911 | 552 | my ($v) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 912 | 552 | ▶ | return 0 unless defined $v; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 913 | 543 | ▶ | if (ref($v) eq 'ARRAY') { return scalar(@$v) ? 1 : 0 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 914 | 531 | ▶ | if (ref($v) eq 'HASH') { return scalar(keys %$v) ? 1 : 0 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 915 | 525 | ▶ | return $v ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 916 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 917 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 918 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 919 | # _eval_for - Evaluate for loop | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 920 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 921 | 126 | ▶ | sub _eval_for { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_eval_for: called CC=22 SCAR=31.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 922 | 126 | my ($self, $tokens, $start, $vars, $loop_var, $iter_expr, $cond_expr, $filename) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 923 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 924 | # Collect loop body tokens (until endfor at depth 1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 925 | 126 | my (@body_tokens, @else_tokens); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 926 | 126 | my $in_else = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 927 | 126 | my $depth = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 928 | 126 | my $i = $start + 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 929 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 930 | 126 | ▶ | while ($i <= $#$tokens) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 931 | 390 | my ($type, $content) = @{$tokens->[$i]}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 932 | 390 | ▶ | if ($type eq 'tag') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 933 | 194 | ▶ | if ($content =~ /^(?:for|if|block|macro|with)\b/) { $depth++ } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 934 | elsif ($content =~ /^end(?:for|if|block|macro|with)/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 935 | 151 | $depth--; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 936 | 151 | ▶ | if ($depth == 0 && $content eq 'endfor') { $i++; last } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$depth == 0 and $content eq "endfor"
MC/DC50%1/2
$depth == 0 and $content eq "endfor"
$depth == 0
$content eq "endfor"
Truth table66%2/3
A and B
A= $depth == 0B= $content eq "endfor"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 937 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 938 | ▶ | elsif ($depth == 1 && $content eq 'else') { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$depth == 1 and $content eq "else"
MC/DC100%2/2
$depth == 1 and $content eq "else"
$depth == 1
$content eq "else"
Truth table100%3/3
A and B
A= $depth == 1B= $content eq "else"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 939 | 10 | $in_else = 1; $i++; next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 940 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 941 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 942 | 254 | ▶ | if ($in_else) { push @else_tokens, $tokens->[$i] } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 943 | 244 | else { push @body_tokens, $tokens->[$i] } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 944 | 254 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 945 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 946 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 947 | # Evaluate iterable | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 948 | 126 | my $iter = $self->_eval_expr($iter_expr, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 949 | 126 | my @items; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 950 | 126 | ▶ | if (ref($iter) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 951 | 115 | @items = @$iter; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 952 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 953 | elsif (ref($iter) eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 954 | 5 | @items = map { [ $_, $iter->{$_} ] } sort keys %$iter; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 955 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 956 | elsif (defined $iter) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 957 | 5 | @items = ($iter); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 958 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 959 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 960 | # Apply loop filter | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 961 | 126 | ▶ | if (defined $cond_expr) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 962 | 12 | my @filtered; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 963 | 12 | for my $item (@items) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 964 | 39 | my %loop_vars = %$vars; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 965 | 39 | $self->_assign_loop_var(\%loop_vars, $loop_var, $item); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 966 | 39 | ▶ | push @filtered, $item if _truthy($self->_eval_expr($cond_expr, \%loop_vars)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 967 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 968 | 12 | @items = @filtered; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 969 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 970 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 971 | 126 | ▶ | if (!@items) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 972 | 9 | return ($self->_eval_tokens(\@else_tokens, $vars, 0, $filename), $i); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 973 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 974 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 975 | 117 | my $total = scalar @items; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 976 | 117 | my $result = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 977 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 978 | 117 | for my $idx (0 .. $#items) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 979 | 325 | my %loop_vars = %$vars; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 980 | 325 | $self->_assign_loop_var(\%loop_vars, $loop_var, $items[$idx]); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 981 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 982 | # loop special variable | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 983 | $loop_vars{loop} = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 984 | index => $idx + 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 985 | index0 => $idx, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 986 | revindex => $total - $idx, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 987 | revindex0 => $total - $idx - 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 988 | first => ($idx == 0) ? 1 : 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 989 | last => ($idx == $#items) ? 1 : 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 990 | length => $total, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 991 | depth => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 992 | depth0 => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 993 | odd => ($idx % 2 == 0) ? 0 : 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 994 | even => ($idx % 2 == 0) ? 1 : 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 995 | 0 | ▶ | changed => sub { my $attr = shift; _loop_changed($attr, $items[$idx], $idx > 0 ? $items[$idx-1] : undef) }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 996 | 325 | ▶ | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 997 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 998 | 325 | $result .= $self->_eval_tokens(\@body_tokens, \%loop_vars, 0, $filename); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 999 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1000 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1001 | 117 | return ($result, $i); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1002 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1003 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1004 | 364 | ▶ | sub _assign_loop_var { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_assign_loop_var: called CC=3 SCAR=11.3
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1005 | 364 | my ($self, $vars, $loop_var, $item) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1006 | # Tuple unpacking: "k, v" in "for k, v in ..." | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1007 | 364 | ▶ | if ($loop_var =~ /^(\w+)\s*,\s*(\w+)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1008 | 10 | my ($k, $v) = ($1, $2); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1009 | 10 | ▶ | if (ref($item) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1010 | 10 | $vars->{$k} = $item->[0]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1011 | 10 | $vars->{$v} = $item->[1]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1012 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1013 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1014 | 0 | $vars->{$k} = $item; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1015 | 0 | $vars->{$v} = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1016 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1017 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1018 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1019 | 354 | $vars->{$loop_var} = $item; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1020 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1021 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1022 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1023 | 0 | ▶ | sub _loop_changed { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_loop_changed: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1024 | 0 | my ($attr, $cur, $prev) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1025 | 0 | ▶ | return 1 unless defined $prev; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1026 | 0 | my $cv = _get_attr($cur, $attr); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1027 | 0 | my $pv = _get_attr($prev, $attr); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1028 | 0 | ▶ | return (!defined $cv && !defined $pv) ? 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition0%0/6
defined $cv || defined $pv
defined $cv && defined $pv
MC/DC0%0/2
defined $cv || defined $pv
defined $cv
defined $pv
MC/DC0%0/2
defined $cv && defined $pv
defined $cv
defined $pv
Truth table0%0/3
A || B
A= defined $cvB= defined $pv
Truth table0%0/3
A && B
A= defined $cvB= defined $pv
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1029 | : (!defined $cv || !defined $pv) ? 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1030 | : $cv ne $pv ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1031 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1032 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1033 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1034 | # _collect_until - Collect tokens until end tag (depth-aware) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1035 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1036 | 13 | ▶ | sub _collect_until { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_collect_until: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1037 | 13 | my ($self, $tokens, $start, @end_tags) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1038 | 13 | my %ends = map { $_ => 1 } @end_tags; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1039 | 13 | my @body; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1040 | 13 | my $depth = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1041 | 13 | my $i = $start; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1042 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1043 | # Map each opener tag to its closer. We track depth only for | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1044 | # openers whose closer matches one of the @end_tags we are looking for, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1045 | # plus any nested use of the SAME opener. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1046 | 13 | my %opener_to_closer = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1047 | 'if' => 'endif', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1048 | 'for' => 'endfor', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1049 | 'block' => 'endblock', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1050 | 'macro' => 'endmacro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1051 | 'with' => 'endwith', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1052 | 'raw' => 'endraw', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1053 | 'set' => 'endset', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1054 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1055 | 13 | my %closer_to_opener = reverse %opener_to_closer; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1056 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1057 | 13 | ▶ | while ($i <= $#$tokens) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1058 | 42 | my ($type, $content) = @{$tokens->[$i]}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1059 | 42 | ▶ | if ($type eq 'tag') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1060 | 24 | my $bare = $content; $bare =~ s/\s.*//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1061 | 24 | ▶ | if (exists $ends{$content}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1062 | 14 | $depth--; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1063 | 14 | ▶ | last if $depth == 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1064 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1065 | elsif (exists $opener_to_closer{$bare}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1066 | # Push only when the closer of THIS opener is in @end_tags | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1067 | # (i.e. same kind of nesting as what we're looking for) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1068 | 6 | my $closer = $opener_to_closer{$bare}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1069 | 6 | ▶ | if (exists $ends{$closer}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1070 | 1 | $depth++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1071 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1072 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1073 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1074 | 29 | push @body, $tokens->[$i]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1075 | 29 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1076 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1077 | 13 | return (\@body, $i); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1078 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1079 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1080 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1081 | # _collect_raw - Collect raw text tokens until endraw | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1082 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1083 | 0 | ▶ | sub _collect_raw { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_collect_raw: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1084 | 0 | my ($self, $tokens, $start) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1085 | 0 | my $raw = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1086 | 0 | my $i = $start; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1087 | 0 | ▶ | while ($i <= $#$tokens) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1088 | 0 | my ($type, $content) = @{$tokens->[$i]}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1089 | 0 | ▶ | last if $type eq 'tag' && $content eq 'endraw'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$type eq "tag" and $content eq "endraw"
MC/DC0%0/2
$type eq "tag" and $content eq "endraw"
$type eq "tag"
$content eq "endraw"
Truth table0%0/3
A and B
A= $type eq "tag"B= $content eq "endraw"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1090 | 0 | $raw .= $content; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1091 | 0 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1092 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1093 | 0 | return ($raw, $i); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1094 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1095 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1096 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1097 | # _call_macro - Call a defined macro | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1098 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1099 | 11 | ▶ | sub _call_macro { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_call_macro: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1100 | 11 | my ($self, $mname, $argstr, $vars, $filename) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1101 | 11 | my $macro = $self->{_macros}{$mname}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1102 | 11 | ▶ | return '' unless defined $macro; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1103 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1104 | # Parse positional args | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1105 | 11 | my @arg_vals = _split_args($argstr); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1106 | 11 | my %macro_vars = %$vars; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1107 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1108 | 11 | my @margs = @{ $macro->{args} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1109 | 11 | my @mdefaults = @{ $macro->{defaults} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1110 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1111 | 11 | for my $idx (0 .. $#margs) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1112 | 18 | ▶ | if ($idx < @arg_vals) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1113 | 17 | $macro_vars{$margs[$idx]} = $self->_eval_expr($arg_vals[$idx], $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1114 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1115 | elsif (defined $mdefaults[$idx]) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1116 | 1 | $macro_vars{$margs[$idx]} = $self->_eval_expr($mdefaults[$idx], $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1117 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1118 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1119 | 0 | $macro_vars{$margs[$idx]} = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1120 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1121 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1122 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1123 | # varargs / kwargs accessible as special vars | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1124 | 11 | $macro_vars{varargs} = []; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1125 | 11 | $macro_vars{kwargs} = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1126 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1127 | 11 | return $self->_eval_template($macro->{body}, { %macro_vars }, $filename); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1128 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1129 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1130 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1131 | # _eval_expr - Evaluate a Jinja2 expression | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1132 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1133 | 2755 | ▶ | sub _eval_expr { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_eval_expr: called CC=154 SCAR=52.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1134 | 2755 | my ($self, $expr, $vars) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1135 | 2755 | $expr =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1136 | 2755 | ▶ | return undef unless defined $expr && $expr ne ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $expr and $expr ne ""
MC/DC0%0/2
defined $expr and $expr ne ""
defined $expr
$expr ne ""
Truth table33%1/3
A and B
A= defined $exprB= $expr ne ""
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1137 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1138 | # Literal: None / True / False | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1139 | 2755 | ▶ | return undef if $expr eq 'none' || $expr eq 'None' || $expr eq 'undefined'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$expr eq "none" or $expr eq "None" or $expr eq "undefined"
$expr eq "none" or $expr eq "None"
MC/DC66%2/3
$expr eq "none" or $expr eq "None" or $expr eq "undefined"
$expr eq "none"
$expr eq "None"
$expr eq "undefined"
Truth table75%3/4
A or B or C
A= $expr eq "none"B= $expr eq "None"C= $expr eq "undefined"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1140 | 2752 | ▶ | return 1 if $expr eq 'true' || $expr eq 'True'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$expr eq "true" or $expr eq "True"
MC/DC50%1/2
$expr eq "true" or $expr eq "True"
$expr eq "true"
$expr eq "True"
Truth table66%2/3
A or B
A= $expr eq "true"B= $expr eq "True"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1141 | 2749 | ▶ | return 0 if $expr eq 'false' || $expr eq 'False'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$expr eq "false" or $expr eq "False"
MC/DC50%1/2
$expr eq "false" or $expr eq "False"
$expr eq "false"
$expr eq "False"
Truth table66%2/3
A or B
A= $expr eq "false"B= $expr eq "False"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1142 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1143 | # Literal: integer / float | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1144 | 2745 | ▶ | return $expr + 0 if $expr =~ /^-?\d+(\.\d+)?$/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1145 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1146 | # Unary minus: -expr (where expr is not a literal number) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1147 | 2418 | ▶ | if ($expr =~ /^-(.+)$/ && $1 !~ /^\d/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$expr =~ /^-(.+)$/ and not $1 =~ /^\d/
MC/DC100%2/2
$expr =~ /^-(.+)$/ and not $1 =~ /^\d/
$expr =~ /^-(.+)$/
not $1 =~ /^\d/
Truth table100%3/3
A and not(B)
A= $expr =~ /^-(.+)$/B= not $1 =~ /^\d/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1148 | 1 | my $val = $self->_eval_expr($1, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1149 | 1 | ▶ | return defined $val ? -$val : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1150 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1151 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1152 | # Literal: string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1153 | 2417 | ▶ | if ($expr =~ /^"((?:[^"\\]|\\.)*)"$/ || $expr =~ /^'((?:[^'\\]|\\.)*)'$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$expr =~ /^"((?:[^"\\]|\\.)*)"$/ or $expr =~ /^'((?:[^'\\]|\\.)*)'$/
MC/DC100%2/2
$expr =~ /^"((?:[^"\\]|\\.)*)"$/ or $expr =~ /^'((?:[^'\\]|\\.)*)'$/
$expr =~ /^"((?:[^"\\]|\\.)*)"$/
$expr =~ /^'((?:[^'\\]|\\.)*)'$/
Truth table100%3/3
A or B
A= $expr =~ /^"((?:[^"\\]|\\.)*)"$/B= $expr =~ /^'((?:[^'\\]|\\.)*)'$/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1154 | 213 | my $s = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1155 | 213 | $s =~ s/\\n/\n/g; $s =~ s/\\t/\t/g; $s =~ s/\\r/\r/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1156 | 213 | $s =~ s/\\(.)/$1/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1157 | 213 | return $s; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1158 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1159 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1160 | # Literal: [] list | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1161 | 2204 | ▶ | if ($expr =~ /^\[(.+)\]$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1162 | 7 | my @elems = _split_args($1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1163 | 7 | return [ map { $self->_eval_expr($_, $vars) } @elems ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1164 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1165 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1166 | # Literal: {} dict | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1167 | 2197 | ▶ | if ($expr =~ /^\{(.+)\}$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1168 | 1 | return $self->_eval_dict($1, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1169 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1170 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1171 | # Literal: range() -- only when no pipe follows (e.g. range(3) but not range(3)|join) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1172 | 2196 | ▶ | if ($expr =~ /^range\(([^|]*)\)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1173 | 22 | return $self->_eval_range($1, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1174 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1175 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1176 | # Conditional expression: a if cond else b | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1177 | 2174 | ▶ | if ($expr =~ /^(.+?)\s+if\s+(.+?)\s+else\s+(.+)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1178 | 34 | my ($a, $cond, $b) = ($1, $2, $3); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1179 | 34 | ▶ | return _truthy($self->_eval_expr($cond, $vars)) ? $self->_eval_expr($a, $vars) : $self->_eval_expr($b, $vars); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1180 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1181 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1182 | # 'not' operator | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1183 | 2140 | ▶ | if ($expr =~ /^not\s+(.+)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1184 | 69 | ▶ | return _truthy($self->_eval_expr($1, $vars)) ? 0 : 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1185 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1186 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1187 | # 'in' / 'not in' operator | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1188 | 2071 | ▶ | if ($expr =~ /^(.+?)\s+not\s+in\s+(.+)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1189 | 4 | ▶ | return $TESTS{in}->($self->_eval_expr($1, $vars), $self->_eval_expr($2, $vars)) ? 0 : 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1190 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1191 | 2067 | ▶ | if ($expr =~ /^(.+?)\s+in\s+(.+)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1192 | 19 | return $TESTS{in}->($self->_eval_expr($1, $vars), $self->_eval_expr($2, $vars)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1193 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1194 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1195 | # Boolean operators: and / or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1196 | 2048 | ▶ | if ($expr =~ /^(.+?)\s+or\s+(.+)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1197 | 14 | my $a = $self->_eval_expr($1, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1198 | 14 | ▶ | return $a if _truthy($a); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1199 | 10 | return $self->_eval_expr($2, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1200 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1201 | 2034 | ▶ | if ($expr =~ /^(.+?)\s+and\s+(.+)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1202 | 18 | my $a = $self->_eval_expr($1, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1203 | 18 | ▶ | return _truthy($a) ? $self->_eval_expr($2, $vars) : $a; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1204 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1205 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1206 | # Filter pipeline: expr | filter1 | filter2(arg) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1207 | # Must be checked before comparison operators so that ">" inside | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1208 | # filter arg strings (e.g. replace("-->","x")) is not misread. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1209 | 2016 | ▶ | if ($expr =~ /^(.+?)\s*\|\s*(\w+)(.*)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1210 | 241 | my ($lhs, $fname, $rest) = ($1, $2, $3); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1211 | 241 | my $val = $self->_eval_expr($lhs, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1212 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1213 | 241 | my $fargs_str = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1214 | 241 | my $remaining = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1215 | 241 | ▶ | if ($rest =~ /^\s*\(/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1216 | # Find matching closing paren, respecting quotes and nesting | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1217 | 102 | my $depth2 = 0; my $in_sq2 = 0; my $in_dq2 = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1218 | 102 | my $found2 = -1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1219 | 102 | my $rs = $rest; $rs =~ s/^\s*//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1220 | 102 | for my $ci (0 .. length($rs) - 1) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1221 | 577 | my $c = substr($rs, $ci, 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1222 | 577 | ▶ | if (!$in_sq2 && !$in_dq2 && $c eq "'") { $in_sq2 = 1; next } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
not $in_sq2 || $in_dq2 and $c eq "'"
$in_sq2 || $in_dq2
MC/DC100%3/3
not $in_sq2 || $in_dq2 and $c eq "'"
$in_sq2
$in_dq2
$c eq "'"
Truth table100%4/4
not(A || B) and C
A= $in_sq2B= $in_dq2C= $c eq "'"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1223 | 573 | ▶ | if ($in_sq2 && $c eq "'") { $in_sq2 = 0; next } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$in_sq2 and $c eq "'"
MC/DC100%2/2
$in_sq2 and $c eq "'"
$in_sq2
$c eq "'"
Truth table100%3/3
A and B
A= $in_sq2B= $c eq "'"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1224 | 569 | ▶ | if (!$in_sq2 && !$in_dq2 && $c eq '"') { $in_dq2 = 1; next } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
not $in_sq2 || $in_dq2 and $c eq "\""
$in_sq2 || $in_dq2
MC/DC100%3/3
not $in_sq2 || $in_dq2 and $c eq "\""
$in_sq2
$in_dq2
$c eq "\""
Truth table100%4/4
not(A || B) and C
A= $in_sq2B= $in_dq2C= $c eq "\""
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1225 | 487 | ▶ | if ($in_dq2 && $c eq '"') { $in_dq2 = 0; next } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$in_dq2 and $c eq "\""
MC/DC100%2/2
$in_dq2 and $c eq "\""
$in_dq2
$c eq "\""
Truth table100%3/3
A and B
A= $in_dq2B= $c eq "\""
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1226 | 405 | ▶ | if (!$in_sq2 && !$in_dq2) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$in_sq2 or $in_dq2
MC/DC100%2/2
$in_sq2 or $in_dq2
$in_sq2
$in_dq2
Truth table100%3/3
A or B
A= $in_sq2B= $in_dq2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1227 | 240 | ▶ | if ($c eq '(') { $depth2++ } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1228 | 102 | ▶ | elsif ($c eq ')') { $depth2--; if ($depth2 == 0) { $found2 = $ci; last } } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1229 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1230 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1231 | 102 | ▶ | if ($found2 >= 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1232 | 102 | $fargs_str = substr($rs, 1, $found2 - 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1233 | 102 | $remaining = substr($rs, $found2 + 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1234 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1235 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1236 | elsif ($rest =~ /^\s*(\|.+)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1237 | 35 | $remaining = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1238 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1239 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1240 | 241 | my @fargs; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1241 | 241 | ▶ | if ($fargs_str ne '') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1242 | 102 | @fargs = map { $self->_eval_expr($_, $vars) } _split_args($fargs_str); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1243 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1244 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1245 | 241 | my $fn = $self->{_filters}{$fname}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1246 | 241 | ▶ | $val = $fn ? $fn->($val, @fargs) : $val; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1247 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1248 | # Continue pipeline | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1249 | 241 | ▶ | if ($remaining =~ s/^\s*\|\s*//) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1250 | 49 | return $self->_eval_expr("__PIPEVAL__ | $remaining", { %$vars, '__PIPEVAL__' => $val }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1251 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1252 | 192 | return $val; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1253 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1254 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1255 | # Comparison operators | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1256 | 1775 | ▶ | if ($expr =~ /^(.+?)\s*(==|!=|>=|<=|>|<)\s*(.+)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1257 | 114 | my ($lhs, $op, $rhs) = ($1, $2, $3); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1258 | 114 | my $l = $self->_eval_expr($lhs, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1259 | 114 | my $r = $self->_eval_expr($rhs, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1260 | 114 | ▶ | return 0 unless defined $l && defined $r; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $l and defined $r
MC/DC0%0/2
defined $l and defined $r
defined $l
defined $r
Truth table33%1/3
A and B
A= defined $lB= defined $r
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1261 | 114 | ▶ | my $numeric = ($l =~ /^-?(?:\d+\.?\d*|\.\d+)$/ && $r =~ /^-?(?:\d+\.?\d*|\.\d+)$/); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$l =~ /^-?(?:\d+\.?\d*|\.\d+)$/ && $r =~ /^-?(?:\d+\.?\d*|\.\d+)$/
MC/DC50%1/2
$l =~ /^-?(?:\d+\.?\d*|\.\d+)$/ && $r =~ /^-?(?:\d+\.?\d*|\.\d+)$/
$l =~ /^-?(?:\d+\.?\d*|\.\d+)$/
$r =~ /^-?(?:\d+\.?\d*|\.\d+)$/
Truth table66%2/3
A && B
A= $l =~ /^-?(?:\d+\.?\d*|\.\d+)$/B= $r =~ /^-?(?:\d+\.?\d*|\.\d+)$/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1262 | 114 | ▶ | if ($op eq '==') { return $numeric ? ($l == $r ? 1 : 0) : ($l eq $r ? 1 : 0) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1263 | 91 | ▶ | if ($op eq '!=') { return $numeric ? ($l != $r ? 1 : 0) : ($l ne $r ? 1 : 0) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1264 | # 0.02: ordering compares numbers as numbers and anything else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1265 | # as strings (Jinja2). 0.01 always compared numerically, so | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1266 | # '2026-09-30' < '2026-10-01' was false (both became 2026) and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1267 | # warned "isn't numeric". | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1268 | 76 | ▶ | if ($op eq '>') { return ($numeric ? $l > $r : $l gt $r) ? 1 : 0 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1269 | 33 | ▶ | if ($op eq '<') { return ($numeric ? $l < $r : $l lt $r) ? 1 : 0 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1270 | 21 | ▶ | if ($op eq '>=') { return ($numeric ? $l >= $r : $l ge $r) ? 1 : 0 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1271 | 8 | ▶ | if ($op eq '<=') { return ($numeric ? $l <= $r : $l le $r) ? 1 : 0 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1272 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1273 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1274 | # Arithmetic: match ** and // before single-char operators | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1275 | 1661 | ▶ | if ($expr =~ /^(.+?)\s*(\*\*|\/\/)\s*(.+)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1276 | 4 | my ($lhs, $op, $rhs) = ($1, $2, $3); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1277 | 4 | my $l = $self->_eval_expr($lhs, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1278 | 4 | my $r = $self->_eval_expr($rhs, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1279 | 4 | ▶ | if (defined $l && defined $r) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $l and defined $r
MC/DC0%0/2
defined $l and defined $r
defined $l
defined $r
Truth table33%1/3
A and B
A= defined $lB= defined $r
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1280 | 4 | ▶ | if ($op eq '//') { return $r != 0 ? int($l / $r) : undef } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1281 | 1 | ▶ | if ($op eq '**') { return $l ** $r } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1282 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1283 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1284 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1285 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1286 | # Arithmetic: scan for operator at depth-0 (respects parentheses) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1287 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1288 | 1657 | my $depth3 = 0; my $in_sq3 = 0; my $in_dq3 = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1289 | 1657 | my $op_pos = -1; my $op_char = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1290 | 1657 | for my $ci (0 .. length($expr) - 1) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1291 | 6639 | my $c = substr($expr, $ci, 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1292 | 6639 | ▶ | if (!$in_sq3 && !$in_dq3 && $c eq "'") { $in_sq3 = 1; next } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
not $in_sq3 || $in_dq3 and $c eq "'"
$in_sq3 || $in_dq3
MC/DC100%3/3
not $in_sq3 || $in_dq3 and $c eq "'"
$in_sq3
$in_dq3
$c eq "'"
Truth table100%4/4
not(A || B) and C
A= $in_sq3B= $in_dq3C= $c eq "'"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1293 | 6637 | ▶ | if ($in_sq3 && $c eq "'") { $in_sq3 = 0; next } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$in_sq3 and $c eq "'"
MC/DC100%2/2
$in_sq3 and $c eq "'"
$in_sq3
$c eq "'"
Truth table100%3/3
A and B
A= $in_sq3B= $c eq "'"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1294 | 6635 | ▶ | if (!$in_sq3 && !$in_dq3 && $c eq '"') { $in_dq3 = 1; next } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
not $in_sq3 || $in_dq3 and $c eq "\""
$in_sq3 || $in_dq3
MC/DC100%3/3
not $in_sq3 || $in_dq3 and $c eq "\""
$in_sq3
$in_dq3
$c eq "\""
Truth table100%4/4
not(A || B) and C
A= $in_sq3B= $in_dq3C= $c eq "\""
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1295 | 6609 | ▶ | if ($in_dq3 && $c eq '"') { $in_dq3 = 0; next } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$in_dq3 and $c eq "\""
MC/DC100%2/2
$in_dq3 and $c eq "\""
$in_dq3
$c eq "\""
Truth table100%3/3
A and B
A= $in_dq3B= $c eq "\""
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1296 | 6583 | ▶ | next if $in_sq3 || $in_dq3; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$in_sq3 or $in_dq3
MC/DC100%2/2
$in_sq3 or $in_dq3
$in_sq3
$in_dq3
Truth table100%3/3
A or B
A= $in_sq3B= $in_dq3
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1297 | 6539 | ▶ | if ($c eq '(' || $c eq '[' || $c eq '{') { $depth3++; next } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$c eq "(" or $c eq "[" or $c eq "{"
$c eq "(" or $c eq "["
MC/DC100%3/3
$c eq "(" or $c eq "[" or $c eq "{"
$c eq "("
$c eq "["
$c eq "{"
Truth table100%4/4
A or B or C
A= $c eq "("B= $c eq "["C= $c eq "{"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1298 | 6457 | ▶ | if ($c eq ')' || $c eq ']' || $c eq '}') { $depth3--; next } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$c eq ")" or $c eq "]" or $c eq "}"
$c eq ")" or $c eq "]"
MC/DC100%3/3
$c eq ")" or $c eq "]" or $c eq "}"
$c eq ")"
$c eq "]"
$c eq "}"
Truth table100%4/4
A or B or C
A= $c eq ")"B= $c eq "]"C= $c eq "}"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1299 | 6379 | ▶ | next if $depth3 != 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1300 | 5880 | ▶ | if ($c =~ /^[+\-*\/%~]$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1301 | 59 | ▶ | next if $ci == 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1302 | 58 | my $prev = substr($expr, $ci - 1, 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1303 | 58 | ▶ | next if $prev =~ /^[+\-*\/%~(]$/ || $prev eq ""; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$prev =~ m[^[+\-*/%~(]$] or $prev eq ""
MC/DC0%0/2
$prev =~ m[^[+\-*/%~(]$] or $prev eq ""
$prev =~ m[^[+\-*/%~(]$]
$prev eq ""
Truth table33%1/3
A or B
A= $prev =~ m[^[+\-*/%~(]$]B= $prev eq ""
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1304 | 58 | ▶ | next if $c eq '*' && $ci + 1 < length($expr) && substr($expr, $ci + 1, 1) eq '*'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$c eq "*" and $ci + 1 < length $expr and substr($expr, $ci + 1, 1) eq "*"
$c eq "*" and $ci + 1 < length $expr
MC/DC0%0/3
$c eq "*" and $ci + 1 < length $expr and substr($expr, $ci + 1, 1) eq "*"
$c eq "*"
$ci + 1 < length $expr
substr($expr, $ci + 1, 1) eq "*"
Truth table50%2/4
A and B and C
A= $c eq "*"B= $ci + 1 < length $exprC= substr($expr, $ci + 1, 1) eq "*"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1305 | 58 | ▶ | next if $c eq '/' && $ci + 1 < length($expr) && substr($expr, $ci + 1, 1) eq '/'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$c eq "/" and $ci + 1 < length $expr and substr($expr, $ci + 1, 1) eq "/"
$c eq "/" and $ci + 1 < length $expr
MC/DC0%0/3
$c eq "/" and $ci + 1 < length $expr and substr($expr, $ci + 1, 1) eq "/"
$c eq "/"
$ci + 1 < length $expr
substr($expr, $ci + 1, 1) eq "/"
Truth table50%2/4
A and B and C
A= $c eq "/"B= $ci + 1 < length $exprC= substr($expr, $ci + 1, 1) eq "/"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1306 | 58 | $op_pos = $ci; $op_char = $c; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1307 | 58 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1308 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1309 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1310 | 1657 | ▶ | if ($op_pos > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1311 | 58 | my $lhs = substr($expr, 0, $op_pos); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1312 | 58 | my $rhs = substr($expr, $op_pos + 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1313 | 58 | $lhs =~ s/\s+$//; $rhs =~ s/^\s+//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1314 | 58 | my $l = $self->_eval_expr($lhs, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1315 | 58 | my $r = $self->_eval_expr($rhs, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1316 | 58 | ▶ | if (defined $l && defined $r) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $l and defined $r
MC/DC0%0/2
defined $l and defined $r
defined $l
defined $r
Truth table33%1/3
A and B
A= defined $lB= defined $r
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1317 | 58 | ▶ | if ($op_char eq '+') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1318 | 26 | ▶ | return ($l =~ /^-?\d/ && $r =~ /^-?\d/) ? $l + $r : $l . $r; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$l =~ /^-?\d/ and $r =~ /^-?\d/
MC/DC0%0/2
$l =~ /^-?\d/ and $r =~ /^-?\d/
$l =~ /^-?\d/
$r =~ /^-?\d/
Truth table33%1/3
A and B
A= $l =~ /^-?\d/B= $r =~ /^-?\d/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1319 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1320 | 32 | ▶ | if ($op_char eq '~') { return $l . $r } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1321 | 16 | ▶ | if ($op_char eq '-') { return $l - $r } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1322 | 14 | ▶ | if ($op_char eq '*') { return $l * $r } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1323 | 2 | ▶ | if ($op_char eq '/') { return $r != 0 ? $l / $r : undef } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1324 | 1 | ▶ | if ($op_char eq '%') { return $r != 0 ? $l % $r : undef } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1325 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1326 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1327 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1328 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1329 | # Parenthesised expression -- must be before is/is not so that | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1330 | # (expr is test) does not mis-parse the leading "(" as part of lhs. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1331 | 1599 | ▶ | if ($expr =~ /^\((.+)\)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1332 | 35 | return $self->_eval_expr($1, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1333 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1334 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1335 | # is / is not test | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1336 | 1564 | ▶ | if ($expr =~ /^(.+?)\s+is\s+not\b\s+(\w+)(?:\s+(.+))?$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1337 | 9 | my ($lhs, $test, $arg) = ($1, $2, $3); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1338 | 9 | my $val = $self->_eval_expr($lhs, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1339 | 9 | ▶ | my $targ = defined $arg ? $self->_eval_expr($arg, $vars) : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1340 | 9 | my $fn = $self->{_tests}{$test}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1341 | 9 | ▶ | return $fn ? ($fn->($val, $targ) ? 0 : 1) : 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1342 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1343 | 1555 | ▶ | if ($expr =~ /^(.+?)\s+is\s+(\w+)(?:\s+(.+))?$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1344 | 134 | my ($lhs, $test, $arg) = ($1, $2, $3); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1345 | 134 | my $val = $self->_eval_expr($lhs, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1346 | 134 | ▶ | my $targ = defined $arg ? $self->_eval_expr($arg, $vars) : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1347 | 134 | my $fn = $self->{_tests}{$test}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1348 | 134 | ▶ | return $fn ? ($fn->($val, $targ) ? 1 : 0) : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1349 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1350 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1351 | # Attribute access: obj.attr or obj["key"] or obj['key'] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1352 | 1421 | ▶ | if ($expr =~ /^(.+?)\.(\w+)(?:\(([^)]*)\))?$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1353 | 159 | my ($obj_expr, $attr, $call_args) = ($1, $2, $3); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1354 | 159 | my $obj = $self->_eval_expr($obj_expr, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1355 | 159 | ▶ | if (defined $call_args) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1356 | # Method call (filters on object) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1357 | 0 | my $fn = $self->{_filters}{$attr}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1358 | 0 | my @args = map { $self->_eval_expr($_, $vars) } _split_args($call_args); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1359 | 0 | ▶ | return $fn ? $fn->($obj, @args) : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1360 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1361 | 159 | return _get_attr($obj, $attr); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1362 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1363 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1364 | 1262 | ▶ | if ($expr =~ /^(.+?)\[["'](\w+)["']\]$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1365 | 5 | my ($obj_expr, $key) = ($1, $2); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1366 | 5 | my $obj = $self->_eval_expr($obj_expr, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1367 | 5 | return _get_attr($obj, $key); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1368 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1369 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1370 | 1257 | ▶ | if ($expr =~ /^(.+?)\[(-?\d+)\]$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1371 | 12 | my ($obj_expr, $idx) = ($1, $2); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1372 | 12 | my $obj = $self->_eval_expr($obj_expr, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1373 | 12 | ▶ | return ref($obj) eq 'ARRAY' ? $obj->[$idx] : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1374 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1375 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1376 | # Slice: list[start:end] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1377 | 1245 | ▶ | if ($expr =~ /^(.+?)\[(-?\d*):(-?\d*)\]$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1378 | 5 | my ($obj_expr, $s, $e) = ($1, $2, $3); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1379 | 5 | my $obj = $self->_eval_expr($obj_expr, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1380 | 5 | ▶ | return undef unless ref($obj) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1381 | 5 | my $len = scalar @$obj; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1382 | 5 | ▶ | my $si = ($s ne '') ? int($s) : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1383 | 5 | ▶ | my $ei = ($e ne '') ? int($e) : $len; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1384 | 5 | ▶ | $si += $len if $si < 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1385 | 5 | ▶ | $ei += $len if $ei < 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1386 | 5 | ▶ | $si = 0 if $si < 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1387 | 5 | ▶ | $ei = $len if $ei > $len; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1388 | 5 | return [ @{$obj}[$si .. $ei - 1] ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1389 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1390 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1391 | # Function/macro call: name(args) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1392 | 1240 | ▶ | if ($expr =~ /^(\w+)\s*\(([^)]*)\)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1393 | 11 | my ($fname, $argstr) = ($1, $2); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1394 | # Macro call | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1395 | 11 | ▶ | if (exists $self->{_macros}{$fname}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1396 | 11 | return $self->_call_macro($fname, $argstr, $vars, '<expr>'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1397 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1398 | # Built-in functions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1399 | 0 | ▶ | if ($fname eq 'range') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1400 | 0 | return $self->_eval_range($argstr, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1401 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1402 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1403 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1404 | # Variable lookup | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1405 | 1229 | ▶ | if ($expr =~ /^(\w+)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1406 | 1219 | ▶ | return exists $vars->{$expr} ? $vars->{$expr} : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1407 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1408 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1409 | 10 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1410 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1411 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1412 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1413 | # _eval_dict - Parse and evaluate a dict literal { key: val, ... } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1414 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1415 | 1 | ▶ | sub _eval_dict { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_eval_dict: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1416 | 1 | my ($self, $inner, $vars) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1417 | 1 | my %h; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1418 | # Simple k:v split (no nested dicts) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1419 | 1 | for my $pair (_split_args($inner)) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1420 | 1 | ▶ | if ($pair =~ /^(.+?)\s*:\s*(.+)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1421 | 1 | my $k = $self->_eval_expr($1, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1422 | 1 | my $v = $self->_eval_expr($2, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1423 | 1 | ▶ | $h{$k} = $v if defined $k; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1424 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1425 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1426 | 1 | return { %h }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1427 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1428 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1429 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1430 | # _eval_range - Evaluate range(stop) / range(start, stop[, step]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1431 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1432 | 22 | ▶ | sub _eval_range { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_eval_range: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1433 | 22 | my ($self, $args_str, $vars) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1434 | 22 | my @args = map { $self->_eval_expr($_, $vars) } _split_args($args_str); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1435 | 22 | my ($start, $stop, $step); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1436 | 22 | ▶ | if (@args == 1) { ($start, $stop, $step) = (0, int($args[0]), 1) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1437 | 4 | elsif (@args == 2) { ($start, $stop, $step) = (int($args[0]), int($args[1]), 1) } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1438 | 4 | ▶ | else { ($start, $stop, $step) = (int($args[0]), int($args[1]), int($args[2] || 1)) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args[2] || 1
MC/DC0%0/1
$args[2] || 1
$args[2]
Truth table50%1/2
A
A= $args[2]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1439 | 22 | ▶ | $step = 1 if $step == 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1440 | 22 | my @result; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1441 | 22 | ▶ | if ($step > 0) { for (my $n = $start; $n < $stop; $n += $step) { push @result, $n } } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1442 | 2 | ▶ | else { for (my $n = $start; $n > $stop; $n += $step) { push @result, $n } } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1443 | 22 | return [ @result ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1444 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1445 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1446 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1447 | # _get_attr - Get attribute from hash or array | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1448 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1449 | 191 | ▶ | sub _get_attr { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_attr: called CC=6 SCAR=24.3
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1450 | 191 | my ($obj, $attr) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1451 | 191 | ▶ | return undef unless defined $obj; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1452 | 191 | ▶ | if (ref($obj) eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1453 | 191 | ▶ | return exists $obj->{$attr} ? $obj->{$attr} : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1454 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1455 | 0 | ▶ | if (ref($obj) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1456 | 0 | ▶ | return $attr =~ /^-?\d+$/ ? $obj->[$attr] : scalar @$obj; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1457 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1458 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1459 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1460 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1461 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1462 | # _split_args - Split comma-separated arguments (respects nested parens/quotes) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1463 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1464 | 156 | ▶ | sub _split_args { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_split_args: called CC=25 SCAR=32.3
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1465 | 156 | my ($str) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1466 | 156 | ▶ | return () unless defined $str && $str =~ /\S/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $str and $str =~ /\S/
MC/DC50%1/2
defined $str and $str =~ /\S/
defined $str
$str =~ /\S/
Truth table66%2/3
A and B
A= defined $strB= $str =~ /\S/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1467 | 155 | my @args; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1468 | 155 | my $cur = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1469 | 155 | my $depth = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1470 | 155 | my $in_sq = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1471 | 155 | my $in_dq = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1472 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1473 | 155 | for my $ch (split //, $str) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1474 | 681 | ▶ | if (!$in_sq && !$in_dq && $ch eq "'") { $in_sq = 1; $cur .= $ch; next } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
not $in_sq || $in_dq and $ch eq "'"
$in_sq || $in_dq
MC/DC100%3/3
not $in_sq || $in_dq and $ch eq "'"
$in_sq
$in_dq
$ch eq "'"
Truth table100%4/4
not(A || B) and C
A= $in_sqB= $in_dqC= $ch eq "'"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1475 | 677 | ▶ | if ($in_sq && $ch eq "'") { $in_sq = 0; $cur .= $ch; next } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$in_sq and $ch eq "'"
MC/DC100%2/2
$in_sq and $ch eq "'"
$in_sq
$ch eq "'"
Truth table100%3/3
A and B
A= $in_sqB= $ch eq "'"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1476 | 673 | ▶ | if (!$in_sq && !$in_dq && $ch eq '"') { $in_dq = 1; $cur .= $ch; next } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
not $in_sq || $in_dq and $ch eq "\""
$in_sq || $in_dq
MC/DC100%3/3
not $in_sq || $in_dq and $ch eq "\""
$in_sq
$in_dq
$ch eq "\""
Truth table100%4/4
not(A || B) and C
A= $in_sqB= $in_dqC= $ch eq "\""
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1477 | 574 | ▶ | if ($in_dq && $ch eq '"') { $in_dq = 0; $cur .= $ch; next } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$in_dq and $ch eq "\""
MC/DC100%2/2
$in_dq and $ch eq "\""
$in_dq
$ch eq "\""
Truth table100%3/3
A and B
A= $in_dqB= $ch eq "\""
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1478 | 475 | ▶ | if (!$in_sq && !$in_dq) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$in_sq or $in_dq
MC/DC100%2/2
$in_sq or $in_dq
$in_sq
$in_dq
Truth table100%3/3
A or B
A= $in_sqB= $in_dq
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1479 | 275 | ▶ | if ($ch eq '(' || $ch eq '[' || $ch eq '{') { $depth++ } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition66%4/6
$ch eq "(" or $ch eq "[" or $ch eq "{"
$ch eq "(" or $ch eq "["
MC/DC33%1/3
$ch eq "(" or $ch eq "[" or $ch eq "{"
$ch eq "("
$ch eq "["
$ch eq "{"
Truth table50%2/4
A or B or C
A= $ch eq "("B= $ch eq "["C= $ch eq "{"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1480 | 1 | ▶ | elsif ($ch eq ')' || $ch eq ']' || $ch eq '}') { $depth-- } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%4/6
$ch eq ")" or $ch eq "]" or $ch eq "}"
$ch eq ")" or $ch eq "]"
MC/DC33%1/3
$ch eq ")" or $ch eq "]" or $ch eq "}"
$ch eq ")"
$ch eq "]"
$ch eq "}"
Truth table50%2/4
A or B or C
A= $ch eq ")"B= $ch eq "]"C= $ch eq "}"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1481 | ▶ | elsif ($ch eq ',' && $depth == 0) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$ch eq "," and $depth == 0
MC/DC100%2/2
$ch eq "," and $depth == 0
$ch eq ","
$depth == 0
Truth table100%3/3
A and B
A= $ch eq ","B= $depth == 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1482 | 42 | $cur =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1483 | 42 | push @args, $cur; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1484 | 42 | $cur = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1485 | 42 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1486 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1487 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1488 | 433 | $cur .= $ch; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1489 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1490 | 155 | $cur =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1491 | 155 | ▶ | push @args, $cur if $cur ne ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1492 | 155 | return @args; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1493 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1494 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1495 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1496 | # _html_escape - Escape HTML special characters | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1497 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1498 | 37 | ▶ | sub _html_escape { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_html_escape: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1499 | 37 | my ($s) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1500 | 37 | ▶ | return '' unless defined $s; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1501 | 37 | $s =~ s/&/&/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1502 | 37 | $s =~ s/</</g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1503 | 37 | $s =~ s/>/>/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1504 | 37 | $s =~ s/"/"/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1505 | 37 | $s =~ s/'/'/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1506 | 37 | return $s; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1507 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1508 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1509 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1510 | # _to_json - Minimal JSON serializer (no external dependency) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1511 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1512 | 13 | ▶ | sub _to_json { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_to_json: called CC=9 SCAR=22.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1513 | 13 | my ($val) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1514 | 13 | ▶ | return 'null' unless defined $val; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1515 | 12 | ▶ | return 'true' if ref($val) eq 'SCALAR' && $$val == 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $val eq "SCALAR" and $$val == 1
MC/DC0%0/2
ref $val eq "SCALAR" and $$val == 1
ref $val eq "SCALAR"
$$val == 1
Truth table33%1/3
A and B
A= ref $val eq "SCALAR"B= $$val == 1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1516 | 12 | ▶ | return 'false' if ref($val) eq 'SCALAR' && $$val == 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $val eq "SCALAR" and $$val == 0
MC/DC0%0/2
ref $val eq "SCALAR" and $$val == 0
ref $val eq "SCALAR"
$$val == 0
Truth table33%1/3
A and B
A= ref $val eq "SCALAR"B= $$val == 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1517 | 12 | ▶ | if (ref($val) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1518 | 2 | return '[' . join(',', map { _to_json($_) } @$val) . ']'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1519 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1520 | 10 | ▶ | if (ref($val) eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1521 | return '{' . join(',', map { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1522 | 0 | _to_json($_) . ':' . _to_json($val->{$_}) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1523 | } sort keys %$val) . '}'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1524 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1525 | 9 | ▶ | if ($val =~ /^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1526 | 6 | return $val; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1527 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1528 | 3 | $val =~ s/\\/\\\\/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1529 | 3 | $val =~ s/"/\\"/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1530 | 3 | $val =~ s/\n/\\n/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1531 | 3 | $val =~ s/\r/\\r/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1532 | 3 | $val =~ s/\t/\\t/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1533 | 3 | return "\"$val\""; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1534 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1535 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1536 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1537 | # Back to main package -- demo when run directly | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1538 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1539 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1540 | # Run as script: perl lib/HP/Handy.pm | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1541 | 13 | ▶ | unless (caller) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1542 | 0 | my $tmpl = HP::Handy->new(auto_escape => 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1543 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1544 | 0 | my $source = <<'TMPL'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1545 | <!DOCTYPE html> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1546 | <html> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1547 | <head><meta charset="utf-8"><title>HP::Handy Demo</title> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1548 | <style>body{font-family:sans-serif;max-width:600px;margin:40px auto;padding:0 20px} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1549 | h1{color:#336699}table{border-collapse:collapse;width:100%} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1550 | td { padding: 6px 10px; border: 1px solid #ccc } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1551 | th { padding: 6px 10px; border: 1px solid #ccc } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1552 | tr:nth-child(even) { background: #f8f8f8 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1553 | </style> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1554 | </head> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1555 | <body> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1556 | <h1>HP::Handy Demo</h1> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1557 | <p>Version: {{ version }}</p> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1558 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1559 | <h2>Variable and Filter</h2> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1560 | <p>Hello, {{ name | upper }}!</p> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1561 | <p>Escaped: {{ snippet }}</p> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1562 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1563 | <h2>For Loop</h2> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1564 | <table> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1565 | <tr><th>#</th><th>Item</th><th>First?</th><th>Last?</th></tr> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1566 | {% for item in items %} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1567 | <tr> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1568 | <td>{{ loop.index }}</td> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1569 | <td>{{ item }}</td> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1570 | <td>{{ "yes" if loop.first else "no" }}</td> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1571 | <td>{{ "yes" if loop.last else "no" }}</td> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1572 | </tr> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1573 | {% endfor %} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1574 | </table> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1575 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1576 | <h2>Conditional</h2> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1577 | {% if score >= 90 %} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1578 | <p>Grade: A</p> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1579 | {% elif score >= 70 %} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1580 | <p>Grade: B</p> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1581 | {% else %} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1582 | <p>Grade: C</p> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1583 | {% endif %} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1584 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1585 | <h2>Set and Default Filter</h2> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1586 | {% set greeting = "Konnichiwa" %} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1587 | <p>{{ greeting | default("Hello") }}</p> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1588 | <p>Missing: {{ missing | default("(none)") }}</p> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1589 | </body> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1590 | </html> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1591 | TMPL | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1592 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1593 | 0 | my $html = $tmpl->render_string($source, { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1594 | version => $HP::Handy::VERSION, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1595 | name => 'World', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1596 | snippet => '<script>alert("xss")</script>', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1597 | items => [ 'Perl', 'Python', 'Ruby', 'JavaScript' ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1598 | score => 85, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1599 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1600 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1601 | 0 | print $html; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1602 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1603 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1604 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1605 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1606 | # HP::Handy::SafeString - trusted HTML string (bypasses auto_escape) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1607 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1608 | package HP::Handy::SafeString; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1609 | use overload | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1610 | 2 | ▶ | q("") => sub { ${$_[0]} }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1611 | 13 | fallback => 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1612 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1613 | package HP::Handy; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1614 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1615 | 13 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1616 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1617 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||