| 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 | 12 | ▶ | 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 | 12 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 12 | ▶ | 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 | 12 | ▶ | use warnings; local $^W = 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 12 | ▶ | BEGIN { pop @INC if $INC[-1] eq '.' } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 12 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 12 | ▶ | use File::Spec; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 12 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 12 | $VERSION = '0.01'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 12 | $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 | 12 | '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 | 12 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | # Constructor | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | 683 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 683 | my ($class, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | my $self = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | template_dir => defined $args{template_dir} ? $args{template_dir} : '.', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | auto_escape => defined $args{auto_escape} ? $args{auto_escape} : 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | trim_blocks => defined $args{trim_blocks} ? $args{trim_blocks} : 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | lstrip_blocks => defined $args{lstrip_blocks} ? $args{lstrip_blocks} : 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | block_start => defined $args{block_start} ? $args{block_start} : '{%', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | block_end => defined $args{block_end} ? $args{block_end} : '%}', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | var_start => defined $args{var_start} ? $args{var_start} : '{{', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | var_end => defined $args{var_end} ? $args{var_end} : '}}', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | comment_start => defined $args{comment_start} ? $args{comment_start} : '{#', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | 683 | ▶ | comment_end => defined $args{comment_end} ? $args{comment_end} : '#}', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 10 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | _filters => { %FILTERS }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | _tests => { %TESTS }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | _blocks => {}, # block name => template string (for inheritance) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | _macros => {}, # macro name => { args=>[], defaults=>[], body=>'' } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | _extends => '', # parent template name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | 683 | bless $self, $class; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | 683 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | # add_filter - Register a custom filter | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | 7 | ▶ | sub add_filter { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_filter: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | 7 | my ($self, $name, $code) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | 7 | ▶ | croak "add_filter: name required" unless defined $name; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | 7 | ▶ | croak "add_filter: code must be coderef" unless ref($code) eq 'CODE'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | 6 | $self->{_filters}{$name} = $code; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | 6 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | # add_test - Register a custom test | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | 4 | ▶ | sub add_test { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
add_test: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | 4 | my ($self, $name, $code) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | 4 | ▶ | croak "add_test: name required" unless defined $name; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | 4 | ▶ | croak "add_test: code must be coderef" unless ref($code) eq 'CODE'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | 4 | $self->{_tests}{$name} = $code; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | 4 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | # render_file - Render a template file with variables | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | 11 | ▶ | sub render_file { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
render_file: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | 11 | my ($self, $filename, $vars) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 11 | ▶ | $vars = {} unless defined $vars; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | 11 | my $source = $self->_load_file($filename); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | 9 | return $self->_render($source, $vars, $filename); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | # render_string - Render a template string with variables | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | 669 | ▶ | sub render_string { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
render_string: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | 669 | my ($self, $source, $vars) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | 669 | ▶ | $vars = {} unless defined $vars; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | 669 | return $self->_render($source, $vars, '<string>'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | # _load_file - Read template from disk | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | 26 | ▶ | sub _load_file { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_load_file: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | 26 | my ($self, $filename) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | # Prevent path traversal | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | 26 | ▶ | if ($filename =~ /\.\./) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | 1 | croak "HP::Handy: path traversal not allowed in '$filename'"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | 25 | my $path; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | 25 | ▶ | if (File::Spec->file_name_is_absolute($filename)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | 0 | $path = $filename; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | 25 | $path = File::Spec->catfile($self->{template_dir}, $filename); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | 25 | local *HPHANDY_FH; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | 25 | ▶ | open(HPHANDY_FH, "< $path") or croak "HP::Handy: cannot open '$path': $!"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | 23 | local $/; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | 23 | my $source = <HPHANDY_FH>; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | 23 | close(HPHANDY_FH); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | 23 | ▶ | return defined $source ? $source : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | # _render - Core rendering engine | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | 678 | ▶ | sub _render { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_render: called CC=10 SCAR=23.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | 678 | my ($self, $source, $vars, $filename) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | 678 | ▶ | $filename = defined $filename ? $filename : '<string>'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | # Reset per-render state | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | 678 | $self->{_blocks} = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | 678 | $self->{_macros} = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | 678 | $self->{_extends} = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | # First pass: collect blocks and macros, detect extends | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | 678 | $source = $self->_preprocess($source, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | # If extends was found, do inheritance (iteratively for multi-level) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | 678 | ▶ | while ($self->{_extends} ne '') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | 7 | my $parent_src = $self->_load_file($self->{_extends}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | 7 | $self->{_extends} = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | # Collect parent blocks/macros without overwriting child definitions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | 7 | $parent_src = $self->_preprocess_parent($parent_src, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | # Apply child blocks to parent template | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | 7 | $source = $self->_apply_inheritance($parent_src, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | # Now resolve all known blocks so deeper levels see merged content. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | # For each block in _blocks, expand any other known blocks inside it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | 7 | my $merged = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | 7 | my $passes = 10; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | 7 | $merged = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | 7 | for my $bname (keys %{$self->{_blocks}}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | 10 | my $prev = $self->{_blocks}{$bname}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | 10 | my $val = $prev; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | 10 | my $lim2 = 10; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | 10 | ▶ | while ($lim2-- > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | 10 | my $prev2 = $val; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | 10 | $val =~ s/\{%-?\s*block\s+(\w+)\s*-?%\}((?:(?!\{%-?\s*block\b)[\s\S])*?)\{%-?\s*endblock(?:\s+\1)?\s*-?%\}/ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | 0 | ▶ | exists $self->{_blocks}{$1} ? $self->{_blocks}{$1} : "\x00BLK\x01$1\x01$2\x00EBLK\x00" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | /gse; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | 10 | ▶ | last if $val eq $prev2; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | 10 | $val =~ s/\x00BLK\x01(\w+)\x01(.*?)\x00EBLK\x00/$2/gs; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | 10 | $val =~ s/\{%-?\s*endblock(?:\s+\w+)?\s*-?%\}//g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | 10 | ▶ | if ($val ne $prev) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | 0 | $self->{_blocks}{$bname} = $val; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | 0 | $merged = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | 678 | return $self->_eval_template($source, $vars, $filename); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | # _preprocess - Scan for extends/block/macro; strip them for non-inherited use | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | 678 | ▶ | sub _preprocess { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_preprocess: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | 678 | my ($self, $source, $vars) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | # Detect {% extends "..." %} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | 678 | ▶ | if ($source =~ s/\{%-?\s*extends\s+["']([^"']+)["']\s*-?%\}[ \t]*\n?//) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | 6 | $self->{_extends} = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | # Collect {% macro name(args) %}...{% endmacro %} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | 678 | ▶ | while ($source =~ s/\{%-?\s*macro\s+(\w+)\s*\(([^)]*)\)\s*-?%\}(.*?)\{%-?\s*endmacro\s*-?%\}//s) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | 10 | my ($mname, $argstr, $body) = ($1, $2, $3); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | 10 | my (@args, @defaults); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | 10 | for my $a (split /\s*,\s*/, $argstr) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | 16 | $a =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | 16 | ▶ | next if $a eq ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | 16 | ▶ | if ($a =~ /^(\w+)\s*=\s*(.+)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | 2 | push @args, $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | 2 | push @defaults, $2; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | 14 | push @args, $a; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | 14 | push @defaults, undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | 10 | $self->{_macros}{$mname} = { args => \@args, defaults => \@defaults, body => $body }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | # Collect {% block name %}...{% endblock %} from child | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | 678 | ▶ | while ($source =~ s/\{%-?\s*block\s+(\w+)\s*-?%\}(.*?)\{%-?\s*endblock(?:\s+\1)?\s*-?%\}/$self->_store_block($1, $2)/se) {} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | 678 | return $source; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | 10 | ▶ | sub _store_block { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_store_block: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | 10 | my ($self, $name, $content) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | 10 | ▶ | $self->{_blocks}{$name} = $content unless exists $self->{_blocks}{$name}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | 10 | return ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | # _preprocess_parent - Scan parent for extends/macro; collect NEW blocks only | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | # (does NOT strip block tags -- those must survive for _apply_inheritance) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | 7 | ▶ | sub _preprocess_parent { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_preprocess_parent: called CC=8 SCAR=30.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | 7 | my ($self, $source, $vars) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | # Detect {% extends "..." %} in parent (for multi-level) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | 7 | ▶ | if ($source =~ s/\{%-?\s*extends\s+["']([^"']+)["']\s*-?%\}[ \t]*\n?//) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | 1 | $self->{_extends} = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 466 | # Collect macros from parent (without overwriting child macros) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | 7 | ▶ | while ($source =~ s/\{%-?\s*macro\s+(\w+)\s*\(([^)]*)\)\s*-?%\}(.*?)\{%-?\s*endmacro\s*-?%\}//s) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | 0 | my ($mname, $argstr, $body) = ($1, $2, $3); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | 0 | ▶ | next if exists $self->{_macros}{$mname}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | 0 | my (@args, @defaults); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | 0 | for my $a (split /\s*,\s*/, $argstr) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | 0 | $a =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 473 | 0 | ▶ | next if $a eq ""; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 474 | 0 | ▶ | if ($a =~ /^(\w+)\s*=\s*(.+)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 475 | 0 | push @args, $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 476 | 0 | push @defaults, $2; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 477 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 478 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 479 | 0 | push @args, $a; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 480 | 0 | push @defaults, undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 481 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 482 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 483 | 0 | $self->{_macros}{$mname} = { args => \@args, defaults => \@defaults, body => $body }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 484 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 486 | # Collect NEW blocks (without overwriting child blocks). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 487 | # Expand already-known child blocks within the body before storing. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 488 | 7 | my $tmp = $source; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 489 | 7 | ▶ | while ($tmp =~ s/\{%-?\s*block\s+(\w+)\s*-?%\}(.*)\{%-?\s*endblock(?:\s+\1)?\s*-?%\}/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 490 | 7 | $self->_store_block_expanded($1, $2) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 491 | /se) {} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 493 | 7 | return $source; # keep block tags intact for _apply_inheritance | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 494 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 496 | 7 | ▶ | sub _store_block_expanded { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_store_block_expanded: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 497 | 7 | my ($self, $name, $body) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 498 | 7 | ▶ | unless (exists $self->{_blocks}{$name}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 499 | # Expand already-resolved child blocks inside this body. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 500 | # Use _apply_inheritance logic on the snippet. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 501 | 1 | my $expanded = $body; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 502 | 1 | my $lim = 10; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 503 | 1 | ▶ | while ($lim-- > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 504 | 2 | my $prev = $expanded; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 505 | # Replace innermost (non-nested) blocks that are known | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 506 | 2 | $expanded =~ s/\{%-?\s*block\s+(\w+)\s*-?%\}((?:(?!\{%-?\s*block\b)[\s\S])*?)\{%-?\s*endblock(?:\s+\1)?\s*-?%\}/ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 507 | 1 | ▶ | exists $self->{_blocks}{$1} ? $self->{_blocks}{$1} : "__BLK__" . $1 . "__SEP__" . $2 . "__EBLK__" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 508 | /gse; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 509 | 2 | ▶ | last if $expanded eq $prev; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 510 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 511 | # Clean up any un-expanded block tags | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 512 | 1 | $expanded =~ s/__BLK__(\w+)__SEP__(.*?)__EBLK__/$2/gs; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 513 | 1 | $expanded =~ s/\{%-?\s*endblock(?:\s+\w+)?\s*-?%\}//g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 514 | 1 | $self->{_blocks}{$name} = $expanded; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 515 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 516 | 7 | return ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 517 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 520 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 521 | # _apply_inheritance - Render parent with child blocks | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 522 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 523 | 7 | ▶ | sub _apply_inheritance { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_apply_inheritance: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 524 | 7 | my ($self, $parent_src, $vars) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 526 | # Iteratively substitute innermost (non-nested) blocks first. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 527 | # A block body that contains no other {% block %} tags is "innermost". | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 528 | 7 | my $limit = 20; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 529 | 7 | ▶ | while ($limit-- > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 530 | 15 | my $changed = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 531 | 15 | $parent_src =~ s/\{%-?\s*block\s+(\w+)\s*-?%\}((?:(?!\{%-?\s*block\b)[\s\S])*?)\{%-?\s*endblock(?:\s+\1)?\s*-?%\}/ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 532 | 10 | ▶ | do { $changed = 1; exists $self->{_blocks}{$1} ? $self->{_blocks}{$1} : $2 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 533 | /gse; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 534 | 15 | ▶ | last unless $changed; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 535 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 537 | # Remove any orphaned {% endblock %} left from outer block substitutions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 538 | 7 | $parent_src =~ s/\{%-?\s*endblock(?:\s+\w+)?\s*-?%\}//g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 540 | 7 | return $parent_src; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 541 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 543 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 544 | # _eval_template - Evaluate template directives recursively | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 545 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 546 | 697 | ▶ | sub _eval_template { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_eval_template: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 547 | 697 | my ($self, $source, $vars, $filename) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 548 | 697 | ▶ | $filename = defined $filename ? $filename : '<string>'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 550 | # Apply trim_blocks / lstrip_blocks | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 551 | 697 | ▶ | if ($self->{trim_blocks}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 552 | 8 | $source =~ s/(%\}|#\})[ \t]*\n/$1\n/g; # already minimal; remove newline after tag | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 553 | 8 | $source =~ s/(%\}|#\})\n/$1/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 554 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 555 | 697 | ▶ | if ($self->{lstrip_blocks}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 556 | 8 | $source =~ s/^[ \t]*(\{[%#])/$1/mg; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 557 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 558 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 559 | 697 | my $bs = quotemeta($self->{block_start}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 560 | 697 | my $be = quotemeta($self->{block_end}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 561 | 697 | my $vs = quotemeta($self->{var_start}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 562 | 697 | my $ve = quotemeta($self->{var_end}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 563 | 697 | my $cs = quotemeta($self->{comment_start}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 564 | 697 | my $ce = quotemeta($self->{comment_end}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 565 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 566 | # Protect {% raw %}...{% endraw %} blocks BEFORE comment stripping, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 567 | # so that {# ... #} inside raw blocks is preserved as-is. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 568 | 697 | my @raw_chunks; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 569 | 697 | my $raw_ph = "\x00RAW\x00"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 570 | 697 | my $bs_re = quotemeta($self->{block_start}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 571 | 697 | my $be_re = quotemeta($self->{block_end}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 572 | 697 | $source =~ s/${bs_re}-?\s*raw\s*-?${be_re}(.*?)${bs_re}-?\s*endraw\s*-?${be_re}/ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 573 | 9 | my $idx = scalar @raw_chunks; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 574 | 9 | push @raw_chunks, $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 575 | 9 | "${raw_ph}${idx}\x00" | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 576 | /gse; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 578 | # Remove comments (after raw protection so {# #} inside raw is safe) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 579 | 697 | $source =~ s/$cs.*?$ce//gs; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 581 | # Tokenise into chunks | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 582 | # Tokens: [ 'text', $str ] or [ 'var', $expr ] or [ 'tag', $stmt ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 583 | 697 | my @tokens = $self->_tokenize($source); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 585 | # Restore raw chunks in text tokens | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 586 | 697 | for my $tok (@tokens) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 587 | 1692 | ▶ | 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/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 588 | 9 | $tok->[1] =~ s/\Q${raw_ph}\E(\d+)\x00/$raw_chunks[$1]/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 589 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 590 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 591 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 592 | 697 | return $self->_eval_tokens(\@tokens, $vars, 0, $filename); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 593 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 594 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 595 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 596 | # _tokenize - Split source into text/var/tag tokens | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 597 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 598 | 697 | ▶ | sub _tokenize { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_tokenize: called CC=28 SCAR=33.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 599 | 697 | my ($self, $source) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 601 | 697 | my $bs = $self->{block_start}; # {% | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 602 | 697 | my $be = $self->{block_end}; # %} | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 603 | 697 | my $vs = $self->{var_start}; # {{ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 604 | 697 | my $ve = $self->{var_end}; # }} | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 605 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 606 | # Build split pattern | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 607 | 697 | my $qbs = quotemeta($bs); my $qbe = quotemeta($be); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 608 | 697 | my $qvs = quotemeta($vs); my $qve = quotemeta($ve); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 609 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 610 | 697 | my @tokens; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 611 | 697 | my $pos = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 612 | 697 | my $len = length($source); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 613 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 614 | 697 | ▶ | while ($pos < $len) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 615 | # Find next tag start | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 616 | 1355 | my $var_pos = index($source, $vs, $pos); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 617 | 1355 | my $block_pos = index($source, $bs, $pos); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 618 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 619 | # No more tags | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 620 | 1355 | ▶ | 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 621 | 52 | push @tokens, [ 'text', substr($source, $pos) ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 622 | 52 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 623 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 624 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 625 | # Determine which comes first | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 626 | 1303 | my $next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 627 | 1303 | ▶ | 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 628 | 479 | $next = $var_pos; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 629 | 479 | my $end = index($source, $ve, $next + length($vs)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 630 | 479 | ▶ | if ($end < 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 631 | 0 | push @tokens, [ 'text', substr($source, $pos) ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 632 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 633 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 634 | 479 | ▶ | if ($next > $pos) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 635 | 37 | push @tokens, [ 'text', substr($source, $pos, $next - $pos) ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 636 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 637 | 479 | my $raw_expr = substr($source, $next + length($vs), $end - $next - length($vs)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 638 | # Whitespace control dash: "{{- expr -}}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 639 | # The dash must be the very first/last non-whitespace character AND | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 640 | # must be followed/preceded by whitespace (not part of the expression). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 641 | # Rule: if raw_expr starts with "-" followed by space/end, it's WS control. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 642 | # "-5" or "-x" is a negative expression, NOT whitespace control. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 643 | 479 | my $strip_left = ($raw_expr =~ /^-(?:\s|$)/); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 644 | 479 | my $strip_right = ($raw_expr =~ /(?:^|\s)-\s*$/); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 645 | 479 | my $expr = $raw_expr; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 646 | 479 | $expr =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 647 | 479 | ▶ | $expr =~ s/^-\s+// if $strip_left; # remove leading "- " | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 648 | 479 | ▶ | $expr =~ s/\s+-$// if $strip_right; # remove trailing " -" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 649 | # Strip whitespace from preceding text token if {{- }} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 650 | 479 | ▶ | 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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 651 | 6 | $tokens[-1][1] =~ s/\s+$//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 652 | 6 | ▶ | splice @tokens, -1 if $tokens[-1][1] eq ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 653 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 654 | 479 | push @tokens, [ 'var', $expr, $strip_right ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 655 | 479 | $pos = $end + length($ve); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 656 | # Strip leading whitespace from next text if {{ -}} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 657 | 479 | ▶ | 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 658 | 6 | my $rest = substr($source, $pos); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 659 | 6 | my $stripped = $rest; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 660 | 6 | $stripped =~ s/^\s+//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 661 | 6 | $pos += length($rest) - length($stripped); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 662 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 663 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 664 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 665 | 824 | $next = $block_pos; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 666 | 824 | my $end = index($source, $be, $next + length($bs)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 667 | 824 | ▶ | if ($end < 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 668 | 0 | push @tokens, [ 'text', substr($source, $pos) ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 669 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 670 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 671 | 824 | ▶ | if ($next > $pos) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 672 | # Whitespace control: {% - strips preceding whitespace | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 673 | 304 | my $text_before = substr($source, $pos, $next - $pos); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 674 | 304 | my $stmt_raw = substr($source, $next + length($bs), $end - $next - length($bs)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 675 | 304 | ▶ | if ($stmt_raw =~ /^-/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 676 | 2 | $text_before =~ s/\s+$//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 677 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 678 | 304 | ▶ | push @tokens, [ 'text', $text_before ] if $text_before ne ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 679 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 680 | 824 | my $stmt_full = substr($source, $next + length($bs), $end - $next - length($bs)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 681 | 824 | my $rstrip_tag = ($stmt_full =~ /\s*-\s*$/); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 682 | 824 | my $stmt = $stmt_full; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 683 | 824 | $stmt =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 684 | 824 | $stmt =~ s/^-\s*|\s*-$//g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 685 | 824 | push @tokens, [ 'tag', $stmt ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 686 | 824 | $pos = $end + length($be); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 687 | # Whitespace control: -%} strips following whitespace | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 688 | 824 | ▶ | 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 689 | 2 | substr($source, $pos) =~ s/^\s+//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 690 | 2 | ▶ | $pos = $len - length($source) + $pos if $source =~ s/^(\s+)//; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 691 | # Simpler: just advance pos past leading whitespace | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 692 | 2 | my $rest = substr($source, $pos); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 693 | 2 | my $stripped = $rest; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 694 | 2 | $stripped =~ s/^\s+//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 695 | 2 | $pos += length($rest) - length($stripped); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 696 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 697 | # trim_blocks: eat newline after %} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 698 | 824 | ▶ | 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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 699 | 0 | $pos++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 700 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 701 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 702 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 703 | 697 | return @tokens; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 704 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 705 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 706 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 707 | # _eval_tokens - Execute a token list and return rendered string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 708 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 709 | 1256 | ▶ | sub _eval_tokens { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_eval_tokens: called CC=31 SCAR=43.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 710 | 1256 | my ($self, $tokens, $vars, $start, $filename) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 711 | 1256 | ▶ | $start = 0 unless defined $start; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 712 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 713 | 1256 | my $out = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 714 | 1256 | my $i = $start; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 715 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 716 | 1256 | ▶ | while ($i <= $#$tokens) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 717 | 1528 | my ($type, $content) = @{$tokens->[$i]}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 718 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 719 | 1528 | ▶ | if ($type eq 'text') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 720 | 378 | $out .= $content; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 721 | 378 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 722 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 723 | elsif ($type eq 'var') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 724 | 663 | my $val = $self->_eval_expr($content, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 725 | 663 | ▶ | if (ref($val) eq 'HP::Handy::SafeString') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 726 | 20 | $val = $$val; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 727 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 728 | ▶ | 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 729 | 15 | $val = _html_escape($val); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 730 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 731 | 663 | ▶ | $out .= defined $val ? $val : ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 732 | 663 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 733 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 734 | elsif ($type eq 'tag') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 735 | 487 | my $stmt = $content; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 736 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 737 | # --- set --- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 738 | 487 | ▶ | if ($stmt =~ /^set\s+(\w+)\s*=\s*(.+)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 10 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 739 | 54 | my ($vname, $expr) = ($1, $2); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 740 | 54 | $vars->{$vname} = $self->_eval_expr($expr, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 741 | 54 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 742 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 743 | # --- set block (multi-line) --- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 744 | elsif ($stmt =~ /^set\s+(\w+)\s*$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 745 | 0 | my $vname = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 746 | 0 | my ($body, $ni) = $self->_collect_until($tokens, $i + 1, 'endset'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 747 | 0 | $vars->{$vname} = $self->_eval_tokens($tokens, $vars, $i + 1, $filename); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 748 | # re-render just the body tokens | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 749 | 0 | my @body_tokens = @{$tokens}[$i+1 .. $ni-1]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 750 | 0 | $vars->{$vname} = $self->_eval_tokens(\@body_tokens, $vars, 0, $filename); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 751 | 0 | $i = $ni + 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 752 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 753 | # --- if / elif / else / endif --- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 754 | elsif ($stmt =~ /^if\s+(.+)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 755 | 287 | my $cond_expr = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 756 | 287 | my ($result, $ni) = $self->_eval_if($tokens, $i, $vars, $filename); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 757 | 287 | $out .= $result; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 758 | 287 | $i = $ni; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 759 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 760 | # --- for / endfor --- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 761 | elsif ($stmt =~ /^for\s+(.+?)\s+in\s+(.+?)(?:\s+if\s+(.+))?$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 762 | 125 | my ($loop_var, $iter_expr, $cond_expr) = ($1, $2, $3); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 763 | 125 | my ($result, $ni) = $self->_eval_for($tokens, $i, $vars, $loop_var, $iter_expr, $cond_expr, $filename); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 764 | 125 | $out .= $result; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 765 | 125 | $i = $ni; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 766 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 767 | # --- include --- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 768 | elsif ($stmt =~ /^include\s+["']([^"']+)["'](?:\s+ignore\s+missing)?$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 769 | 8 | my $inc_file = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 770 | 8 | my $inc_src; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 771 | 8 | eval { $inc_src = $self->_load_file($inc_file) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 772 | 8 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 773 | 1 | ▶ | $inc_src = '' if $stmt =~ /ignore\s+missing/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 774 | 1 | ▶ | croak $@ unless $stmt =~ /ignore\s+missing/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 775 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 776 | 8 | ▶ | $out .= $self->_eval_template($inc_src, { %$vars }, $inc_file) if defined $inc_src; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 777 | 8 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 778 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 779 | # --- call macro --- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 780 | ▶ | 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}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 781 | 0 | my ($mname, $argstr) = ($1, $2); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 782 | 0 | $out .= $self->_call_macro($mname, $argstr, $vars, $filename); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 783 | 0 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 784 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 785 | # --- raw / endraw --- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 786 | elsif ($stmt eq 'raw') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 787 | 0 | my ($raw, $ni) = $self->_collect_raw($tokens, $i + 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 788 | 0 | $out .= $raw; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 789 | 0 | $i = $ni + 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 790 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 791 | # --- with / endwith --- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 792 | elsif ($stmt =~ /^with(?:\s+(.+))?$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 793 | 13 | my $assigns = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 794 | 13 | my %local_vars = %$vars; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 795 | 13 | ▶ | if (defined $assigns) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 796 | 13 | for my $pair (_split_args($assigns)) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 797 | 15 | ▶ | if ($pair =~ /^(\w+)\s*=\s*(.+)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 798 | 15 | $local_vars{$1} = $self->_eval_expr($2, { %local_vars }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 799 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 800 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 801 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 802 | 13 | my ($body_tokens_ref, $ni) = $self->_collect_until($tokens, $i + 1, 'endwith'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 803 | 13 | $out .= $self->_eval_tokens($body_tokens_ref, { %local_vars }, 0, $filename); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 804 | 13 | $i = $ni + 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 805 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 806 | # --- block (standalone, no inheritance) --- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 807 | elsif ($stmt =~ /^block\s+(\w+)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 808 | 0 | my $bname = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 809 | 0 | my ($body_tokens_ref, $ni) = $self->_collect_until($tokens, $i + 1, "endblock $bname", 'endblock'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 810 | 0 | ▶ | if (exists $self->{_blocks}{$bname}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 811 | 0 | $out .= $self->_eval_template($self->{_blocks}{$bname}, $vars, $filename); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 812 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 813 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 814 | 0 | $out .= $self->_eval_tokens($body_tokens_ref, $vars, 0, $filename); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 815 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 816 | 0 | $i = $ni + 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 817 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 818 | # skip endxxx tags (consumed by their openers above) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 819 | ▶ | 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)/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 820 | 0 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 821 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 822 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 823 | # Unknown tag: pass through as-is for forward compatibility | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 824 | 0 | $out .= $self->{block_start} . ' ' . $stmt . ' ' . $self->{block_end}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 825 | 0 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 826 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 827 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 828 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 829 | 0 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 830 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 831 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 832 | 1256 | return $out; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 833 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 834 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 835 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 836 | # _eval_if - Evaluate if/elif/else/endif block | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 837 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 838 | 287 | ▶ | sub _eval_if { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_eval_if: called CC=15 SCAR=27.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 839 | 287 | my ($self, $tokens, $start, $vars, $filename) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 840 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 841 | # Collect all branches: [ [cond_expr, [tokens...]], ..., [undef, [tokens...]] ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 842 | 287 | my @branches; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 843 | 287 | my $cur_cond = $tokens->[$start][1]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 844 | 287 | $cur_cond =~ s/^if\s+//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 845 | 287 | my @cur_body; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 846 | 287 | my $depth = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 847 | 287 | my $i = $start + 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 848 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 849 | 287 | ▶ | while ($i <= $#$tokens) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 850 | 661 | my ($type, $content) = @{$tokens->[$i]}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 851 | 661 | ▶ | if ($type eq 'tag') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 852 | 343 | ▶ | if ($content =~ /^if\b/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 853 | 8 | $depth++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 854 | 8 | push @cur_body, $tokens->[$i]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 855 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 856 | elsif ($content eq 'endif') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 857 | 295 | $depth--; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 858 | 295 | ▶ | if ($depth == 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 859 | 287 | push @branches, [ $cur_cond, [@cur_body] ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 860 | 287 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 861 | 287 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 862 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 863 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 864 | 8 | push @cur_body, $tokens->[$i]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 865 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 866 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 867 | ▶ | 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+(.+)$/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 868 | 13 | push @branches, [ $cur_cond, [@cur_body] ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 869 | 13 | $cur_cond = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 870 | 13 | @cur_body = (); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 871 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 872 | ▶ | 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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 873 | 16 | push @branches, [ $cur_cond, [@cur_body] ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 874 | 16 | $cur_cond = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 875 | 16 | @cur_body = (); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 876 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 877 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 878 | 11 | push @cur_body, $tokens->[$i]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 879 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 880 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 881 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 882 | 318 | push @cur_body, $tokens->[$i]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 883 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 884 | 374 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 885 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 886 | 287 | ▶ | 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 887 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 888 | 287 | my $result = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 889 | 287 | for my $branch (@branches) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 890 | 376 | my ($cond, $body) = @$branch; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 891 | 376 | ▶ | if (!defined $cond || $self->_eval_expr($cond, $vars)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $cond or $self->_eval_expr($cond, $vars)
MC/DC100%2/2
not defined $cond or $self->_eval_expr($cond, $vars)
not defined $cond
$self->_eval_expr($cond, $vars)
Truth table100%3/3
not(A) or B
A= not defined $condB= $self->_eval_expr($cond, $vars)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 892 | 214 | $result = $self->_eval_tokens($body, $vars, 0, $filename); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 893 | 214 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 894 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 895 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 896 | 287 | return ($result, $i); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 897 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 898 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 899 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 900 | # _eval_for - Evaluate for loop | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 901 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 902 | 125 | ▶ | sub _eval_for { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_eval_for: called CC=22 SCAR=31.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 903 | 125 | my ($self, $tokens, $start, $vars, $loop_var, $iter_expr, $cond_expr, $filename) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 904 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 905 | # Collect loop body tokens (until endfor at depth 1) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 906 | 125 | my (@body_tokens, @else_tokens); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 907 | 125 | my $in_else = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 908 | 125 | my $depth = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 909 | 125 | my $i = $start + 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 910 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 911 | 125 | ▶ | while ($i <= $#$tokens) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 912 | 388 | my ($type, $content) = @{$tokens->[$i]}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 913 | 388 | ▶ | if ($type eq 'tag') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 914 | 193 | ▶ | if ($content =~ /^(?:for|if|block|macro|with)\b/) { $depth++ } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 915 | elsif ($content =~ /^end(?:for|if|block|macro|with)/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 916 | 150 | $depth--; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 917 | 150 | ▶ | 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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 918 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 919 | ▶ | 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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 920 | 10 | $in_else = 1; $i++; next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 921 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 922 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 923 | 253 | ▶ | if ($in_else) { push @else_tokens, $tokens->[$i] } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 924 | 243 | else { push @body_tokens, $tokens->[$i] } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 925 | 253 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 926 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 927 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 928 | # Evaluate iterable | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 929 | 125 | my $iter = $self->_eval_expr($iter_expr, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 930 | 125 | my @items; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 931 | 125 | ▶ | if (ref($iter) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 932 | 114 | @items = @$iter; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 933 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 934 | elsif (ref($iter) eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 935 | 5 | @items = map { [ $_, $iter->{$_} ] } sort keys %$iter; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 936 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 937 | elsif (defined $iter) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 938 | 5 | @items = ($iter); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 939 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 940 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 941 | # Apply loop filter | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 942 | 125 | ▶ | if (defined $cond_expr) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 943 | 11 | my @filtered; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 944 | 11 | for my $item (@items) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 945 | 35 | my %loop_vars = %$vars; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 946 | 35 | $self->_assign_loop_var(\%loop_vars, $loop_var, $item); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 947 | 35 | ▶ | push @filtered, $item if $self->_eval_expr($cond_expr, \%loop_vars); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 948 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 949 | 11 | @items = @filtered; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 950 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 951 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 952 | 125 | ▶ | if (!@items) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 953 | 9 | return ($self->_eval_tokens(\@else_tokens, $vars, 0, $filename), $i); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 954 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 955 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 956 | 116 | my $total = scalar @items; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 957 | 116 | my $result = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 958 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 959 | 116 | for my $idx (0 .. $#items) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 960 | 323 | my %loop_vars = %$vars; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 961 | 323 | $self->_assign_loop_var(\%loop_vars, $loop_var, $items[$idx]); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 962 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 963 | # loop special variable | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 964 | $loop_vars{loop} = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 965 | index => $idx + 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 966 | index0 => $idx, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 967 | revindex => $total - $idx, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 968 | revindex0 => $total - $idx - 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 969 | first => ($idx == 0) ? 1 : 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 970 | last => ($idx == $#items) ? 1 : 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 971 | length => $total, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 972 | depth => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 973 | depth0 => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 974 | odd => ($idx % 2 == 0) ? 0 : 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 975 | even => ($idx % 2 == 0) ? 1 : 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 976 | 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 977 | 323 | ▶ | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 978 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 979 | 323 | $result .= $self->_eval_tokens(\@body_tokens, \%loop_vars, 0, $filename); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 980 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 981 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 982 | 116 | return ($result, $i); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 983 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 984 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 985 | 358 | ▶ | sub _assign_loop_var { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_assign_loop_var: called CC=3 SCAR=11.3
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 986 | 358 | my ($self, $vars, $loop_var, $item) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 987 | # Tuple unpacking: "k, v" in "for k, v in ..." | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 988 | 358 | ▶ | if ($loop_var =~ /^(\w+)\s*,\s*(\w+)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 989 | 10 | my ($k, $v) = ($1, $2); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 990 | 10 | ▶ | if (ref($item) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 991 | 10 | $vars->{$k} = $item->[0]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 992 | 10 | $vars->{$v} = $item->[1]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 993 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 994 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 995 | 0 | $vars->{$k} = $item; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 996 | 0 | $vars->{$v} = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 997 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 998 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 999 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1000 | 348 | $vars->{$loop_var} = $item; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1001 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1002 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1003 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1004 | 0 | ▶ | sub _loop_changed { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_loop_changed: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1005 | 0 | my ($attr, $cur, $prev) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1006 | 0 | ▶ | return 1 unless defined $prev; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1007 | 0 | my $cv = _get_attr($cur, $attr); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1008 | 0 | my $pv = _get_attr($prev, $attr); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1009 | 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1010 | : (!defined $cv || !defined $pv) ? 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1011 | : $cv ne $pv ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1012 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1013 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1014 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1015 | # _collect_until - Collect tokens until end tag (depth-aware) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1016 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1017 | 13 | ▶ | sub _collect_until { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_collect_until: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1018 | 13 | my ($self, $tokens, $start, @end_tags) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1019 | 13 | my %ends = map { $_ => 1 } @end_tags; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1020 | 13 | my @body; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1021 | 13 | my $depth = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1022 | 13 | my $i = $start; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1023 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1024 | # Map each opener tag to its closer. We track depth only for | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1025 | # openers whose closer matches one of the @end_tags we are looking for, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1026 | # plus any nested use of the SAME opener. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1027 | 13 | my %opener_to_closer = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1028 | 'if' => 'endif', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1029 | 'for' => 'endfor', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1030 | 'block' => 'endblock', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1031 | 'macro' => 'endmacro', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1032 | 'with' => 'endwith', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1033 | 'raw' => 'endraw', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1034 | 'set' => 'endset', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1035 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1036 | 13 | my %closer_to_opener = reverse %opener_to_closer; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1037 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1038 | 13 | ▶ | while ($i <= $#$tokens) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1039 | 42 | my ($type, $content) = @{$tokens->[$i]}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1040 | 42 | ▶ | if ($type eq 'tag') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1041 | 24 | my $bare = $content; $bare =~ s/\s.*//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1042 | 24 | ▶ | if (exists $ends{$content}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1043 | 14 | $depth--; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1044 | 14 | ▶ | last if $depth == 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1045 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1046 | elsif (exists $opener_to_closer{$bare}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1047 | # Push only when the closer of THIS opener is in @end_tags | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1048 | # (i.e. same kind of nesting as what we're looking for) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1049 | 6 | my $closer = $opener_to_closer{$bare}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1050 | 6 | ▶ | if (exists $ends{$closer}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1051 | 1 | $depth++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1052 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1053 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1054 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1055 | 29 | push @body, $tokens->[$i]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1056 | 29 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1057 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1058 | 13 | return (\@body, $i); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1059 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1060 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1061 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1062 | # _collect_raw - Collect raw text tokens until endraw | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1063 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1064 | 0 | ▶ | sub _collect_raw { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_collect_raw: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1065 | 0 | my ($self, $tokens, $start) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1066 | 0 | my $raw = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1067 | 0 | my $i = $start; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1068 | 0 | ▶ | while ($i <= $#$tokens) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1069 | 0 | my ($type, $content) = @{$tokens->[$i]}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1070 | 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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1071 | 0 | $raw .= $content; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1072 | 0 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1073 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1074 | 0 | return ($raw, $i); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1075 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1076 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1077 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1078 | # _call_macro - Call a defined macro | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1079 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1080 | 11 | ▶ | sub _call_macro { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_call_macro: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1081 | 11 | my ($self, $mname, $argstr, $vars, $filename) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1082 | 11 | my $macro = $self->{_macros}{$mname}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1083 | 11 | ▶ | return '' unless defined $macro; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1084 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1085 | # Parse positional args | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1086 | 11 | my @arg_vals = _split_args($argstr); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1087 | 11 | my %macro_vars = %$vars; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1088 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1089 | 11 | my @margs = @{ $macro->{args} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1090 | 11 | my @mdefaults = @{ $macro->{defaults} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1091 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1092 | 11 | for my $idx (0 .. $#margs) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1093 | 18 | ▶ | if ($idx < @arg_vals) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1094 | 17 | $macro_vars{$margs[$idx]} = $self->_eval_expr($arg_vals[$idx], $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1095 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1096 | elsif (defined $mdefaults[$idx]) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1097 | 1 | $macro_vars{$margs[$idx]} = $self->_eval_expr($mdefaults[$idx], $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1098 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1099 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1100 | 0 | $macro_vars{$margs[$idx]} = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1101 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1102 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1103 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1104 | # varargs / kwargs accessible as special vars | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1105 | 11 | $macro_vars{varargs} = []; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1106 | 11 | $macro_vars{kwargs} = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1107 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1108 | 11 | return $self->_eval_template($macro->{body}, { %macro_vars }, $filename); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1109 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1110 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1111 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1112 | # _eval_expr - Evaluate a Jinja2 expression | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1113 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1114 | 2681 | ▶ | sub _eval_expr { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_eval_expr: called CC=150 SCAR=51.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1115 | 2681 | my ($self, $expr, $vars) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1116 | 2681 | $expr =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1117 | 2681 | ▶ | 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 ""
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1118 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1119 | # Literal: None / True / False | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1120 | 2681 | ▶ | 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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1121 | 2678 | ▶ | 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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1122 | 2675 | ▶ | 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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1123 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1124 | # Literal: integer / float | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1125 | 2671 | ▶ | return $expr + 0 if $expr =~ /^-?\d+(\.\d+)?$/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1126 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1127 | # Unary minus: -expr (where expr is not a literal number) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1128 | 2344 | ▶ | 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/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1129 | 1 | my $val = $self->_eval_expr($1, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1130 | 1 | ▶ | return defined $val ? -$val : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1131 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1132 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1133 | # Literal: string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1134 | 2343 | ▶ | 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 =~ /^'((?:[^'\\]|\\.)*)'$/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1135 | 203 | my $s = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1136 | 203 | $s =~ s/\\n/\n/g; $s =~ s/\\t/\t/g; $s =~ s/\\r/\r/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1137 | 203 | $s =~ s/\\(.)/$1/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1138 | 203 | return $s; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1139 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1140 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1141 | # Literal: [] list | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1142 | 2140 | ▶ | if ($expr =~ /^\[(.+)\]$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1143 | 7 | my @elems = _split_args($1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1144 | 7 | return [ map { $self->_eval_expr($_, $vars) } @elems ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1145 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1146 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1147 | # Literal: {} dict | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1148 | 2133 | ▶ | if ($expr =~ /^\{(.+)\}$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1149 | 1 | return $self->_eval_dict($1, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1150 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1151 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1152 | # Literal: range() -- only when no pipe follows (e.g. range(3) but not range(3)|join) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1153 | 2132 | ▶ | if ($expr =~ /^range\(([^|]*)\)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1154 | 22 | return $self->_eval_range($1, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1155 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1156 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1157 | # Conditional expression: a if cond else b | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1158 | 2110 | ▶ | if ($expr =~ /^(.+?)\s+if\s+(.+?)\s+else\s+(.+)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1159 | 25 | my ($a, $cond, $b) = ($1, $2, $3); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1160 | 25 | ▶ | return $self->_eval_expr($cond, $vars) ? $self->_eval_expr($a, $vars) : $self->_eval_expr($b, $vars); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1161 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1162 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1163 | # 'not' operator | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1164 | 2085 | ▶ | if ($expr =~ /^not\s+(.+)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1165 | 67 | ▶ | return $self->_eval_expr($1, $vars) ? 0 : 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1166 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1167 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1168 | # 'in' / 'not in' operator | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1169 | 2018 | ▶ | if ($expr =~ /^(.+?)\s+not\s+in\s+(.+)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1170 | 4 | ▶ | return $TESTS{in}->($self->_eval_expr($1, $vars), $self->_eval_expr($2, $vars)) ? 0 : 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1171 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1172 | 2014 | ▶ | if ($expr =~ /^(.+?)\s+in\s+(.+)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1173 | 19 | return $TESTS{in}->($self->_eval_expr($1, $vars), $self->_eval_expr($2, $vars)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1174 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1175 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1176 | # Boolean operators: and / or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1177 | 1995 | ▶ | if ($expr =~ /^(.+?)\s+or\s+(.+)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1178 | 12 | my $a = $self->_eval_expr($1, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1179 | 12 | ▶ | return $a if $a; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1180 | 8 | return $self->_eval_expr($2, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1181 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1182 | 1983 | ▶ | if ($expr =~ /^(.+?)\s+and\s+(.+)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1183 | 17 | my $a = $self->_eval_expr($1, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1184 | 17 | ▶ | return $a ? $self->_eval_expr($2, $vars) : $a; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1185 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1186 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1187 | # Filter pipeline: expr | filter1 | filter2(arg) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1188 | # Must be checked before comparison operators so that ">" inside | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1189 | # filter arg strings (e.g. replace("-->","x")) is not misread. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1190 | 1966 | ▶ | if ($expr =~ /^(.+?)\s*\|\s*(\w+)(.*)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1191 | 241 | my ($lhs, $fname, $rest) = ($1, $2, $3); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1192 | 241 | my $val = $self->_eval_expr($lhs, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1193 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1194 | 241 | my $fargs_str = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1195 | 241 | my $remaining = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1196 | 241 | ▶ | if ($rest =~ /^\s*\(/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1197 | # Find matching closing paren, respecting quotes and nesting | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1198 | 102 | my $depth2 = 0; my $in_sq2 = 0; my $in_dq2 = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1199 | 102 | my $found2 = -1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1200 | 102 | my $rs = $rest; $rs =~ s/^\s*//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1201 | 102 | for my $ci (0 .. length($rs) - 1) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1202 | 577 | my $c = substr($rs, $ci, 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1203 | 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 "'"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1204 | 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 "'"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1205 | 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 "\""
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1206 | 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 "\""
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1207 | 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1208 | 240 | ▶ | if ($c eq '(') { $depth2++ } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1209 | 102 | ▶ | elsif ($c eq ')') { $depth2--; if ($depth2 == 0) { $found2 = $ci; last } } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1210 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1211 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1212 | 102 | ▶ | if ($found2 >= 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1213 | 102 | $fargs_str = substr($rs, 1, $found2 - 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1214 | 102 | $remaining = substr($rs, $found2 + 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1215 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1216 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1217 | elsif ($rest =~ /^\s*(\|.+)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1218 | 35 | $remaining = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1219 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1220 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1221 | 241 | my @fargs; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1222 | 241 | ▶ | if ($fargs_str ne '') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1223 | 102 | @fargs = map { $self->_eval_expr($_, $vars) } _split_args($fargs_str); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1224 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1225 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1226 | 241 | my $fn = $self->{_filters}{$fname}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1227 | 241 | ▶ | $val = $fn ? $fn->($val, @fargs) : $val; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1228 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1229 | # Continue pipeline | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1230 | 241 | ▶ | if ($remaining =~ s/^\s*\|\s*//) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1231 | 49 | return $self->_eval_expr("__PIPEVAL__ | $remaining", { %$vars, '__PIPEVAL__' => $val }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1232 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1233 | 192 | return $val; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1234 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1235 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1236 | # Comparison operators | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1237 | 1725 | ▶ | if ($expr =~ /^(.+?)\s*(==|!=|>=|<=|>|<)\s*(.+)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1238 | 107 | my ($lhs, $op, $rhs) = ($1, $2, $3); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1239 | 107 | my $l = $self->_eval_expr($lhs, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1240 | 107 | my $r = $self->_eval_expr($rhs, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1241 | 107 | ▶ | 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1242 | 107 | ▶ | 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+)$/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1243 | 107 | ▶ | if ($op eq '==') { return $numeric ? ($l == $r ? 1 : 0) : ($l eq $r ? 1 : 0) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1244 | 85 | ▶ | if ($op eq '!=') { return $numeric ? ($l != $r ? 1 : 0) : ($l ne $r ? 1 : 0) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1245 | 70 | ▶ | if ($op eq '>') { return $l > $r ? 1 : 0 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1246 | 28 | ▶ | if ($op eq '<') { return $l < $r ? 1 : 0 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1247 | 18 | ▶ | if ($op eq '>=') { return $l >= $r ? 1 : 0 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1248 | 6 | ▶ | if ($op eq '<=') { return $l <= $r ? 1 : 0 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1249 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1250 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1251 | # Arithmetic: match ** and // before single-char operators | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1252 | 1618 | ▶ | if ($expr =~ /^(.+?)\s*(\*\*|\/\/)\s*(.+)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1253 | 4 | my ($lhs, $op, $rhs) = ($1, $2, $3); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1254 | 4 | my $l = $self->_eval_expr($lhs, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1255 | 4 | my $r = $self->_eval_expr($rhs, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1256 | 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1257 | 4 | ▶ | if ($op eq '//') { return $r != 0 ? int($l / $r) : undef } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1258 | 1 | ▶ | if ($op eq '**') { return $l ** $r } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1259 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1260 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1261 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1262 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1263 | # Arithmetic: scan for operator at depth-0 (respects parentheses) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1264 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1265 | 1614 | my $depth3 = 0; my $in_sq3 = 0; my $in_dq3 = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1266 | 1614 | my $op_pos = -1; my $op_char = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1267 | 1614 | for my $ci (0 .. length($expr) - 1) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1268 | 6561 | my $c = substr($expr, $ci, 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1269 | 6561 | ▶ | 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 "'"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1270 | 6559 | ▶ | 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 "'"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1271 | 6557 | ▶ | 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 "\""
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1272 | 6531 | ▶ | 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 "\""
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1273 | 6505 | ▶ | 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1274 | 6461 | ▶ | 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 "{"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1275 | 6379 | ▶ | 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 "}"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1276 | 6301 | ▶ | next if $depth3 != 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1277 | 5802 | ▶ | if ($c =~ /^[+\-*\/%~]$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1278 | 59 | ▶ | next if $ci == 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1279 | 58 | my $prev = substr($expr, $ci - 1, 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1280 | 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 ""
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1281 | 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 "*"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1282 | 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 "/"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1283 | 58 | $op_pos = $ci; $op_char = $c; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1284 | 58 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1285 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1286 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1287 | 1614 | ▶ | if ($op_pos > 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1288 | 58 | my $lhs = substr($expr, 0, $op_pos); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1289 | 58 | my $rhs = substr($expr, $op_pos + 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1290 | 58 | $lhs =~ s/\s+$//; $rhs =~ s/^\s+//; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1291 | 58 | my $l = $self->_eval_expr($lhs, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1292 | 58 | my $r = $self->_eval_expr($rhs, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1293 | 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1294 | 58 | ▶ | if ($op_char eq '+') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1295 | 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/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1296 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1297 | 32 | ▶ | if ($op_char eq '~') { return $l . $r } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1298 | 16 | ▶ | if ($op_char eq '-') { return $l - $r } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1299 | 14 | ▶ | if ($op_char eq '*') { return $l * $r } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1300 | 2 | ▶ | if ($op_char eq '/') { return $r != 0 ? $l / $r : undef } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1301 | 1 | ▶ | if ($op_char eq '%') { return $r != 0 ? $l % $r : undef } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1302 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1303 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1304 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1305 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1306 | # Parenthesised expression -- must be before is/is not so that | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1307 | # (expr is test) does not mis-parse the leading "(" as part of lhs. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1308 | 1556 | ▶ | if ($expr =~ /^\((.+)\)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1309 | 35 | return $self->_eval_expr($1, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1310 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1311 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1312 | # is / is not test | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1313 | 1521 | ▶ | if ($expr =~ /^(.+?)\s+is\s+not\b\s+(\w+)(?:\s+(.+))?$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1314 | 9 | my ($lhs, $test, $arg) = ($1, $2, $3); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1315 | 9 | my $val = $self->_eval_expr($lhs, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1316 | 9 | ▶ | my $targ = defined $arg ? $self->_eval_expr($arg, $vars) : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1317 | 9 | my $fn = $self->{_tests}{$test}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1318 | 9 | ▶ | return $fn ? ($fn->($val, $targ) ? 0 : 1) : 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1319 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1320 | 1512 | ▶ | if ($expr =~ /^(.+?)\s+is\s+(\w+)(?:\s+(.+))?$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1321 | 134 | my ($lhs, $test, $arg) = ($1, $2, $3); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1322 | 134 | my $val = $self->_eval_expr($lhs, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1323 | 134 | ▶ | my $targ = defined $arg ? $self->_eval_expr($arg, $vars) : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1324 | 134 | my $fn = $self->{_tests}{$test}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1325 | 134 | ▶ | return $fn ? ($fn->($val, $targ) ? 1 : 0) : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1326 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1327 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1328 | # Attribute access: obj.attr or obj["key"] or obj['key'] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1329 | 1378 | ▶ | if ($expr =~ /^(.+?)\.(\w+)(?:\(([^)]*)\))?$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1330 | 157 | my ($obj_expr, $attr, $call_args) = ($1, $2, $3); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1331 | 157 | my $obj = $self->_eval_expr($obj_expr, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1332 | 157 | ▶ | if (defined $call_args) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1333 | # Method call (filters on object) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1334 | 0 | my $fn = $self->{_filters}{$attr}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1335 | 0 | my @args = map { $self->_eval_expr($_, $vars) } _split_args($call_args); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1336 | 0 | ▶ | return $fn ? $fn->($obj, @args) : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1337 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1338 | 157 | return _get_attr($obj, $attr); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1339 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1340 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1341 | 1221 | ▶ | if ($expr =~ /^(.+?)\[["'](\w+)["']\]$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1342 | 5 | my ($obj_expr, $key) = ($1, $2); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1343 | 5 | my $obj = $self->_eval_expr($obj_expr, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1344 | 5 | return _get_attr($obj, $key); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1345 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1346 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1347 | 1216 | ▶ | if ($expr =~ /^(.+?)\[(-?\d+)\]$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1348 | 12 | my ($obj_expr, $idx) = ($1, $2); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1349 | 12 | my $obj = $self->_eval_expr($obj_expr, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1350 | 12 | ▶ | return ref($obj) eq 'ARRAY' ? $obj->[$idx] : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1351 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1352 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1353 | # Slice: list[start:end] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1354 | 1204 | ▶ | if ($expr =~ /^(.+?)\[(-?\d*):(-?\d*)\]$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1355 | 5 | my ($obj_expr, $s, $e) = ($1, $2, $3); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1356 | 5 | my $obj = $self->_eval_expr($obj_expr, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1357 | 5 | ▶ | return undef unless ref($obj) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1358 | 5 | my $len = scalar @$obj; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1359 | 5 | ▶ | my $si = ($s ne '') ? int($s) : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1360 | 5 | ▶ | my $ei = ($e ne '') ? int($e) : $len; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1361 | 5 | ▶ | $si += $len if $si < 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1362 | 5 | ▶ | $ei += $len if $ei < 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1363 | 5 | ▶ | $si = 0 if $si < 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1364 | 5 | ▶ | $ei = $len if $ei > $len; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1365 | 5 | return [ @{$obj}[$si .. $ei - 1] ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1366 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1367 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1368 | # Function/macro call: name(args) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1369 | 1199 | ▶ | if ($expr =~ /^(\w+)\s*\(([^)]*)\)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1370 | 11 | my ($fname, $argstr) = ($1, $2); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1371 | # Macro call | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1372 | 11 | ▶ | if (exists $self->{_macros}{$fname}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1373 | 11 | return $self->_call_macro($fname, $argstr, $vars, '<expr>'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1374 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1375 | # Built-in functions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1376 | 0 | ▶ | if ($fname eq 'range') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1377 | 0 | return $self->_eval_range($argstr, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1378 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1379 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1380 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1381 | # Variable lookup | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1382 | 1188 | ▶ | if ($expr =~ /^(\w+)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1383 | 1178 | ▶ | return exists $vars->{$expr} ? $vars->{$expr} : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1384 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1385 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1386 | 10 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1387 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1388 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1389 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1390 | # _eval_dict - Parse and evaluate a dict literal { key: val, ... } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1391 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1392 | 1 | ▶ | sub _eval_dict { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_eval_dict: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1393 | 1 | my ($self, $inner, $vars) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1394 | 1 | my %h; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1395 | # Simple k:v split (no nested dicts) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1396 | 1 | for my $pair (_split_args($inner)) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1397 | 1 | ▶ | if ($pair =~ /^(.+?)\s*:\s*(.+)$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1398 | 1 | my $k = $self->_eval_expr($1, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1399 | 1 | my $v = $self->_eval_expr($2, $vars); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1400 | 1 | ▶ | $h{$k} = $v if defined $k; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1401 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1402 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1403 | 1 | return { %h }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1404 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1405 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1406 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1407 | # _eval_range - Evaluate range(stop) / range(start, stop[, step]) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1408 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1409 | 22 | ▶ | sub _eval_range { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_eval_range: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1410 | 22 | my ($self, $args_str, $vars) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1411 | 22 | my @args = map { $self->_eval_expr($_, $vars) } _split_args($args_str); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1412 | 22 | my ($start, $stop, $step); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1413 | 22 | ▶ | if (@args == 1) { ($start, $stop, $step) = (0, int($args[0]), 1) } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1414 | 4 | elsif (@args == 2) { ($start, $stop, $step) = (int($args[0]), int($args[1]), 1) } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1415 | 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]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1416 | 22 | ▶ | $step = 1 if $step == 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1417 | 22 | my @result; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1418 | 22 | ▶ | if ($step > 0) { for (my $n = $start; $n < $stop; $n += $step) { push @result, $n } } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1419 | 2 | ▶ | else { for (my $n = $start; $n > $stop; $n += $step) { push @result, $n } } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1420 | 22 | return [ @result ]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1421 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1422 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1423 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1424 | # _get_attr - Get attribute from hash or array | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1425 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1426 | 189 | ▶ | sub _get_attr { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_attr: called CC=6 SCAR=24.3
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1427 | 189 | my ($obj, $attr) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1428 | 189 | ▶ | return undef unless defined $obj; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1429 | 189 | ▶ | if (ref($obj) eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1430 | 189 | ▶ | return exists $obj->{$attr} ? $obj->{$attr} : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1431 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1432 | 0 | ▶ | if (ref($obj) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1433 | 0 | ▶ | return $attr =~ /^-?\d+$/ ? $obj->[$attr] : scalar @$obj; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1434 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1435 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1436 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1437 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1438 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1439 | # _split_args - Split comma-separated arguments (respects nested parens/quotes) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1440 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1441 | 156 | ▶ | sub _split_args { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_split_args: called CC=25 SCAR=32.3
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1442 | 156 | my ($str) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1443 | 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/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1444 | 155 | my @args; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1445 | 155 | my $cur = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1446 | 155 | my $depth = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1447 | 155 | my $in_sq = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1448 | 155 | my $in_dq = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1449 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1450 | 155 | for my $ch (split //, $str) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1451 | 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 "'"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1452 | 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 "'"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1453 | 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 "\""
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1454 | 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 "\""
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1455 | 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1456 | 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 "{"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1457 | 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 "}"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1458 | ▶ | 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1459 | 42 | $cur =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1460 | 42 | push @args, $cur; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1461 | 42 | $cur = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1462 | 42 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1463 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1464 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1465 | 433 | $cur .= $ch; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1466 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1467 | 155 | $cur =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1468 | 155 | ▶ | push @args, $cur if $cur ne ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1469 | 155 | return @args; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1470 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1471 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1472 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1473 | # _html_escape - Escape HTML special characters | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1474 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1475 | 26 | ▶ | sub _html_escape { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_html_escape: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1476 | 26 | my ($s) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1477 | 26 | ▶ | return '' unless defined $s; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1478 | 26 | $s =~ s/&/&/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1479 | 26 | $s =~ s/</</g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1480 | 26 | $s =~ s/>/>/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1481 | 26 | $s =~ s/"/"/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1482 | 26 | $s =~ s/'/'/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1483 | 26 | return $s; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1484 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1485 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1486 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1487 | # _to_json - Minimal JSON serializer (no external dependency) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1488 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1489 | 13 | ▶ | sub _to_json { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_to_json: called CC=9 SCAR=22.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1490 | 13 | my ($val) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1491 | 13 | ▶ | return 'null' unless defined $val; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1492 | 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1493 | 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1494 | 12 | ▶ | if (ref($val) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1495 | 2 | return '[' . join(',', map { _to_json($_) } @$val) . ']'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1496 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1497 | 10 | ▶ | if (ref($val) eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1498 | return '{' . join(',', map { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1499 | 1 | _to_json($_) . ':' . _to_json($val->{$_}) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1500 | } sort keys %$val) . '}'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1501 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1502 | 9 | ▶ | if ($val =~ /^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1503 | 6 | return $val; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1504 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1505 | 3 | $val =~ s/\\/\\\\/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1506 | 3 | $val =~ s/"/\\"/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1507 | 3 | $val =~ s/\n/\\n/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1508 | 3 | $val =~ s/\r/\\r/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1509 | 3 | $val =~ s/\t/\\t/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1510 | 3 | return "\"$val\""; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1511 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1512 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1513 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1514 | # Back to main package -- demo when run directly | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1515 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1516 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1517 | # Run as script: perl lib/HP/Handy.pm | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1518 | 12 | ▶ | unless (caller) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1519 | 0 | my $tmpl = HP::Handy->new(auto_escape => 1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1520 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1521 | 0 | my $source = <<'TMPL'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1522 | <!DOCTYPE html> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1523 | <html> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1524 | <head><meta charset="utf-8"><title>HP::Handy Demo</title> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1525 | <style>body{font-family:sans-serif;max-width:600px;margin:40px auto;padding:0 20px} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1526 | h1{color:#336699}table{border-collapse:collapse;width:100%} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1527 | td { padding: 6px 10px; border: 1px solid #ccc } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1528 | th { padding: 6px 10px; border: 1px solid #ccc } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1529 | tr:nth-child(even) { background: #f8f8f8 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1530 | </style> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1531 | </head> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1532 | <body> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1533 | <h1>HP::Handy Demo</h1> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1534 | <p>Version: {{ version }}</p> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1535 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1536 | <h2>Variable and Filter</h2> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1537 | <p>Hello, {{ name | upper }}!</p> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1538 | <p>Escaped: {{ snippet }}</p> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1539 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1540 | <h2>For Loop</h2> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1541 | <table> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1542 | <tr><th>#</th><th>Item</th><th>First?</th><th>Last?</th></tr> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1543 | {% for item in items %} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1544 | <tr> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1545 | <td>{{ loop.index }}</td> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1546 | <td>{{ item }}</td> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1547 | <td>{{ "yes" if loop.first else "no" }}</td> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1548 | <td>{{ "yes" if loop.last else "no" }}</td> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1549 | </tr> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1550 | {% endfor %} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1551 | </table> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1552 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1553 | <h2>Conditional</h2> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1554 | {% if score >= 90 %} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1555 | <p>Grade: A</p> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1556 | {% elif score >= 70 %} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1557 | <p>Grade: B</p> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1558 | {% else %} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1559 | <p>Grade: C</p> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1560 | {% endif %} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1561 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1562 | <h2>Set and Default Filter</h2> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1563 | {% set greeting = "Konnichiwa" %} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1564 | <p>{{ greeting | default("Hello") }}</p> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1565 | <p>Missing: {{ missing | default("(none)") }}</p> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1566 | </body> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1567 | </html> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1568 | TMPL | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1569 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1570 | 0 | my $html = $tmpl->render_string($source, { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1571 | version => $HP::Handy::VERSION, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1572 | name => 'World', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1573 | snippet => '<script>alert("xss")</script>', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1574 | items => [ 'Perl', 'Python', 'Ruby', 'JavaScript' ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1575 | score => 85, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1576 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1577 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1578 | 0 | print $html; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1579 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1580 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1581 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1582 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1583 | # HP::Handy::SafeString - trusted HTML string (bypasses auto_escape) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1584 | ############################################################################### | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1585 | package HP::Handy::SafeString; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1586 | use overload | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1587 | 2 | ▶ | q("") => sub { ${$_[0]} }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1588 | 12 | fallback => 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1589 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1590 | package HP::Handy; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1591 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1592 | 12 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1593 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1594 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||