blib/lib/Template/Tiny/Strict.pm

filter: Statementstmt 93.0% 80 / 86 Branchbran 87.5% 35 / 40 Conditioncond 88.8% 8 / 9 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 100% 8 / 8 Podpod 66.6% 2 / 3 totaltotal 90.7% 137 / 151 CC 26 SCAR 20.2
CC
26
Cov
91%
process
11.5
_expression
9.0
_process
5.0
CRAP 7.5
↑ Summary
1 package Template::Tiny::Strict;
2  
3 # ABSTRACT: Template Toolkit reimplemented in as little code as possible
4  
5 # Load overhead: 40k
6  
7 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 our $VERSION = '1.16';
10  
11 # Evaluatable expression
12 4 my $EXPR = qr/ [a-z_][\w.]* /xs;
13  
14 # Opening [% tag including whitespace chomping rules
15 4 my $LEFT = qr/
16 (?:
17 (?: (?:^|\n) [ \t]* )? \[\%\-
18 |
19 \[\% \+?
20 ) \s*
21 /xs;
22  
23 # Closing %] tag including whitespace chomping rules
24 4 my $RIGHT = qr/
25 \s* (?:
26 \+? \%\]
27 |
28 \-\%\] (?: [ \t]* \n )?
29 )
30 /xs;
31  
32 # Preparsing run for nesting tags
33 4 my $PREPARSE = qr/
34 $LEFT ( IF | UNLESS | FOREACH ) \s+
35 (
36 (?: \S+ \s+ IN \s+ )?
37 \S+ )
38 $RIGHT
39 (?!
40 .*?
41 $LEFT (?: IF | UNLESS | FOREACH ) \b
42 )
43 ( .*? )
44 (?:
45 $LEFT ELSE $RIGHT
46 (?!
47 .*?
48 $LEFT (?: IF | UNLESS | FOREACH ) \b
49 )
50 ( .+? )
51 )?
52 $LEFT END $RIGHT
53 /xs;
54  
55 # Condition set
56 4 my $CONDITION = qr/
57 \[\%\s
58 ( ([IUF])\d+ ) \s+
59 (?:
60 ([a-z]\w*) \s+ IN \s+
61 )?
62 ( $EXPR )
63 \s\%\]
64 ( .*? )
65 (?:
66 \[\%\s \1 \s\%\]
67 ( .+? )
68 )?
69 \[\%\s \1 \s\%\]
70 /xs;
71  
72 sub new {
73 19 ▶ my ( $class, %arg_for ) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
74 bless {
75 TRIM => $arg_for{TRIM},
76 forbid_undef => $arg_for{forbid_undef},
77 forbid_unused => $arg_for{forbid_unused},
78 19 _undefined => {},
79 _used => {},
80 } => $class;
81 }
82  
83 # Copy and modify
84 sub preprocess {
85 3 ▶ my $self = shift;
Subroutine preprocess: called CC=1 SCAR=0.0
Pod undocumented
86 3 my $text = shift;
87 3 $self->_preprocess( \$text );
88 3 return $text;
89 }
90  
91 sub process {
92 16 ▶ my $self = shift;
Subroutine process: called CC=11 SCAR=24.4
Pod documented
93 16 my $copy = ${ shift() };
94 16 ▶ my $stash = shift || {};
Condition50%1/2
shift() || {}
l!l
160
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
95 16 $self->{_undefined} = {};
96 16 $self->{_used} = {};
97  
98 16 local $@ = '';
99 16 local $^W = 0;
100  
101 # Preprocess to establish unique matching tag sets
102 16 $self->_preprocess( \$copy );
103  
104 # Process down the nested tree of conditions
105 16 my $result = $self->_process( $stash, $copy );
106 16 my $errors = '';
107 16 ▶ if ( $self->{forbid_undef} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"forbid_undef"}) 6 10 16
108 6 ▶ if ( my %errors = %{ $self->{_undefined} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(%errors) = %{$self->{"_undefined"};}) 3 3 6
109 3 $errors = join "\n" => sort keys %errors;
110 }
111 }
112 16 ▶ if ( $self->{forbid_unused} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"forbid_unused"}) 6 10 16
113 6 my @unused;
114 6 foreach my $var ( keys %$stash ) {
115 10 ▶ unless ( $self->{_used}{$var} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_used"}{$var}) 4 6 10
116 4 push @unused => $var;
117 }
118 }
119 6 ▶ if ( my $unused = join ', ' => sort @unused ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $unused = join(", ", sort(@unused))) 3 3 6
120 3 $errors
121 .= "\nThe following variables were passed to the template but unused: '$unused'";
122 }
123 }
124 16 ▶ if ($errors) {
Branch: 1 branch
BranchTrueFalseTotal
if ($errors) 5 11 16
125 5 require Carp;
126 5 Carp::croak($errors);
127 }
128  
129 11 ▶ if (@_) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 11 0 11
elsif (defined wantarray) { } 0 0 0
130 11 ${ $_[0] } = $result;
131 }
132 elsif ( defined wantarray ) {
133 0 require Carp;
134 0 Carp::carp(
135 'Returning of template results is deprecated in Template::Tiny::Strict 0.11'
136 );
137 0 return $result;
138 }
139 else {
140 0 print $result;
141 }
142 }
143  
144 ######################################################################
145 # Support Methods
146  
147 # The only reason this is a standalone is so we can
148 # do more in-depth testing.
149 sub _preprocess {
150 19 ▶ my $self = shift;
Subroutine _preprocess: called CC=3 SCAR=11.0
151 19 my $copy = shift;
152  
153 # Preprocess to establish unique matching tag sets
154 19 my $id = 0;
155 19 ▶ 1 while $$copy =~ s/
Branch: 1 branch
BranchTrueFalseTotal
$$copy =~ s/ $PREPARSE /my $tag = substr($1, 0, 1) . ++$id; "[% $tag $2 %]$3[% $tag %]" . (defined $4 ? "$4[% $tag %]" : "");/sex and do { 1 } 20 19 39
156 $PREPARSE
157 /
158 20 my $tag = substr($1, 0, 1) . ++$id;
159 20 ▶ "\[\% $tag $2 \%\]$3\[\% $tag \%\]"
Branch: 1 branch
BranchTrueFalseTotal
defined $4 ? : 3 17 20
160 . (defined($4) ? "$4\[\% $tag \%\]" : '');
161 /sex;
162 }
163  
164 sub _process {
165 36 ▶ my ( $self, $stash, $text ) = @_;
Subroutine _process: called CC=5 SCAR=16.1
166  
167 36 $text =~ s/
168 $CONDITION
169 /
170 ($2 eq 'F')
171 ? $self->_foreach($stash, $3, $4, $5)
172 18 ▶ : eval {
Branch: 2 branches
BranchTrueFalseTotal
$2 eq 'F' ? : 2 16 18
eval { do { $2 eq 'U' xor not !$self->_expression($stash, $4) } } ? : 8 8 16
173 16 ▶ $2 eq 'U'
Condition100%4/4
$2 eq "U" xor not !$self->_expression($stash, $4)
l&&rl&&!r!l&&r!l&&!r
1177
MC/DC100%2/2
$2 eq "U" xor not !$self->_expression($stash, $4)
$2 eq "U" not !$self->_expression($stash, $4)
Truth table100%4/4
A xor B
A= $2 eq "U"B= not !$self->_expression($stash, $4)
ABresult
110
101
011
000
174 xor
175 !! # Force boolification
176 $self->_expression($stash, $4)
177 }
178 ? $self->_process($stash, $5)
179 : $self->_process($stash, $6)
180 /gsex;
181  
182 # Resolve expressions
183 36 $text =~ s/
184 $LEFT ( $EXPR ) $RIGHT
185 /
186 41 eval {
187 41 $self->_expression($stash, $1)
188 . '' # Force stringification
189 }
190 /gsex;
191  
192 # Trim the document
193 36 ▶ $text =~ s/^\s*(.+?)\s*\z/$1/s if $self->{TRIM};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"TRIM"} 1 35 36
194  
195 36 return $text;
196 }
197  
198 # Special handling for foreach
199 sub _foreach {
200 2 ▶ my ( $self, $stash, $term, $expr, $text ) = @_;
Subroutine _foreach: called CC=2 SCAR=7.2
201  
202 # Resolve the expression
203 2 my $list = $self->_expression( $stash, $expr );
204 2 ▶ unless ( ref $list eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $list eq "ARRAY") 0 2 2
205 0 return '';
206 }
207  
208 # Iterate
209 return join '',
210 2 map { $self->_process( { %$stash, $term => $_ }, $text ) } @$list;
211 }
212  
213 # Evaluates a stash expression
214 sub _expression {
215 59 ▶ my $cursor = $_[1];
Subroutine _expression: called CC=9 SCAR=22.0
216 59 my @path = split /\./, $_[2];
217 59 $_[0]->{_used}{ $path[0] } = 1;
218 59 foreach (@path) {
219  
220 # Support for private keys
221 83 ▶ return undef if substr( $_, 0, 1 ) eq '_';
Branch: 1 branch
BranchTrueFalseTotal
if substr($_, 0, 1) eq "_" 2 81 83
222  
223 # Split by data type
224 81 my $type = ref $cursor;
225 81 ▶ if ( $type eq 'ARRAY' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'ARRAY') { } 7 74 81
elsif ($type eq 'HASH') { } 72 2 74
elsif ($type) { } 2 0 2
226 7 ▶ return '' unless /^(?:0|[0-9]\d*)\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^(?:0|[0-9]\d*)\z/ 1 6 7
227 6 $cursor = $cursor->[$_];
228 }
229 elsif ( $type eq 'HASH' ) {
230 72 $cursor = $cursor->{$_};
231 }
232 elsif ($type) {
233 2 $cursor = $cursor->$_();
234 }
235 else {
236 0 return '';
237 }
238 }
239 56 ▶ if ( $_[0]->{forbid_undef} && !defined $cursor ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]{"forbid_undef"} and not defined $cursor) 3 53 56
Condition100%3/3
$_[0]{"forbid_undef"} and not defined $cursor
!ll&&!rl&&r
4943
MC/DC100%2/2
$_[0]{"forbid_undef"} and not defined $cursor
$_[0]{"forbid_undef"} not defined $cursor
Truth table100%3/3
A and not(B)
A= $_[0]{"forbid_undef"}B= not defined $cursor
ABresult
0X0
100
111
240 3 my $path = join '.' => @path;
241 3 $_[0]->{_undefined}{"Undefined value in template path '$path'"} = 1;
242 3 return '';
243 }
244  
245 53 return $cursor;
246 }
247  
248 4 1;
249  
250 __END__
↑ Summary