blib/lib/Template/Tiny/Strict.pm

filter: Statementstmt 93.3% 84 / 90 Branchbran 87.5% 35 / 40 Conditioncond 90.9% 10 / 11 MC/DCmcdc 83.3% 5 / 6 Subroutinesub 100% 9 / 9 Podpod 75.0% 3 / 4 totaltotal 91.2% 146 / 160 SCAR 33.1
CC
27
Cov
91%
process
11.4
_expression
9.0
_process
5.0
CRAP 27.4
↑ 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.17';
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=2 SCAR=6.9
Pod documented
74 return bless {
75 TRIM => $arg_for{TRIM},
76 forbid_undef => $arg_for{forbid_undef},
77 forbid_unused => $arg_for{forbid_unused},
78 19 ▶ name => ( $arg_for{name} // 'template' ),
Condition100%2/2
$arg_for{"name"} // "template"
l!l
514
MC/DC100%1/1
$arg_for{"name"} // "template"
$arg_for{"name"}
Truth table100%2/2
A
A= $arg_for{"name"}
Aresult
11
00
79 _undefined => {},
80 _used => {},
81 } => $class;
82 }
83  
84 5 ▶sub name { $_[0]->{name} }
Subroutine name: called CC=1 SCAR=0.0
Pod documented
85  
86 # Copy and modify
87 sub preprocess {
88 3 ▶ my $self = shift;
Subroutine preprocess: called CC=1 SCAR=0.0
Pod undocumented
89 3 my $text = shift;
90 3 $self->_preprocess( \$text );
91 3 return $text;
92 }
93  
94 sub process {
95 16 ▶ my $self = shift;
Subroutine process: called CC=11 SCAR=24.4
Pod documented
96 16 my $copy = ${ shift() };
97 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
98 16 $self->{_undefined} = {};
99 16 $self->{_used} = {};
100  
101 16 local $@ = '';
102 16 local $^W = 0;
103  
104 # Preprocess to establish unique matching tag sets
105 16 $self->_preprocess( \$copy );
106  
107 # Process down the nested tree of conditions
108 16 my $result = $self->_process( $stash, $copy );
109 16 my $errors = '';
110 16 ▶ if ( $self->{forbid_undef} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"forbid_undef"}) 6 10 16
111 6 ▶ if ( my %errors = %{ $self->{_undefined} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(%errors) = %{$self->{"_undefined"};}) 3 3 6
112 3 $errors = join "\n" => sort keys %errors;
113 }
114 }
115 16 ▶ if ( $self->{forbid_unused} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"forbid_unused"}) 6 10 16
116 6 my @unused;
117 6 foreach my $var ( keys %$stash ) {
118 10 ▶ unless ( $self->{_used}{$var} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_used"}{$var}) 4 6 10
119 4 push @unused => $var;
120 }
121 }
122 6 ▶ if ( my $unused = join ', ' => sort @unused ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $unused = join(", ", sort(@unused))) 3 3 6
123 3 $errors
124 .= "\nThe following variables were passed to the template but unused: '$unused'";
125 }
126 }
127 16 ▶ if ($errors) {
Branch: 1 branch
BranchTrueFalseTotal
if ($errors) 5 11 16
128 5 require Carp;
129 5 my $name = $self->name;
130 5 my $class = ref $self;
131 5 $errors = "$class processing for '$name' failed:\n$errors";
132 5 Carp::croak($errors);
133 }
134  
135 11 ▶ if (@_) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 11 0 11
elsif (defined wantarray) { } 0 0 0
136 11 ${ $_[0] } = $result;
137 }
138 elsif ( defined wantarray ) {
139 0 require Carp;
140 0 Carp::carp(
141 'Returning of template results is deprecated in Template::Tiny::Strict 0.11'
142 );
143 0 return $result;
144 }
145 else {
146 0 print $result;
147 }
148 }
149  
150 ######################################################################
151 # Support Methods
152  
153 # The only reason this is a standalone is so we can
154 # do more in-depth testing.
155 sub _preprocess {
156 19 ▶ my $self = shift;
Subroutine _preprocess: called CC=3 SCAR=11.0
157 19 my $copy = shift;
158  
159 # Preprocess to establish unique matching tag sets
160 19 my $id = 0;
161 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
162 $PREPARSE
163 /
164 20 my $tag = substr($1, 0, 1) . ++$id;
165 20 ▶ "\[\% $tag $2 \%\]$3\[\% $tag \%\]"
Branch: 1 branch
BranchTrueFalseTotal
defined $4 ? : 3 17 20
166 . (defined($4) ? "$4\[\% $tag \%\]" : '');
167 /sex;
168 }
169  
170 sub _process {
171 36 ▶ my ( $self, $stash, $text ) = @_;
Subroutine _process: called CC=5 SCAR=16.1
172  
173 36 $text =~ s/
174 $CONDITION
175 /
176 ($2 eq 'F')
177 ? $self->_foreach($stash, $3, $4, $5)
178 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
179 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
180 xor
181 !! # Force boolification
182 $self->_expression($stash, $4)
183 }
184 ? $self->_process($stash, $5)
185 : $self->_process($stash, $6)
186 /gsex;
187  
188 # Resolve expressions
189 36 $text =~ s/
190 $LEFT ( $EXPR ) $RIGHT
191 /
192 41 eval {
193 41 $self->_expression($stash, $1)
194 . '' # Force stringification
195 }
196 /gsex;
197  
198 # Trim the document
199 36 ▶ $text =~ s/^\s*(.+?)\s*\z/$1/s if $self->{TRIM};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"TRIM"} 1 35 36
200  
201 36 return $text;
202 }
203  
204 # Special handling for foreach
205 sub _foreach {
206 2 ▶ my ( $self, $stash, $term, $expr, $text ) = @_;
Subroutine _foreach: called CC=2 SCAR=7.2
207  
208 # Resolve the expression
209 2 my $list = $self->_expression( $stash, $expr );
210 2 ▶ unless ( ref $list eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $list eq "ARRAY") 0 2 2
211 0 return '';
212 }
213  
214 # Iterate
215 return join '',
216 2 map { $self->_process( { %$stash, $term => $_ }, $text ) } @$list;
217 }
218  
219 # Evaluates a stash expression
220 sub _expression {
221 59 ▶ my $cursor = $_[1];
Subroutine _expression: called CC=9 SCAR=22.0
222 59 my @path = split /\./, $_[2];
223 59 $_[0]->{_used}{ $path[0] } = 1;
224 59 foreach (@path) {
225  
226 # Support for private keys
227 83 ▶ return undef if substr( $_, 0, 1 ) eq '_';
Branch: 1 branch
BranchTrueFalseTotal
if substr($_, 0, 1) eq "_" 2 81 83
228  
229 # Split by data type
230 81 my $type = ref $cursor;
231 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
232 7 ▶ return '' unless /^(?:0|[0-9]\d*)\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^(?:0|[0-9]\d*)\z/ 1 6 7
233 6 $cursor = $cursor->[$_];
234 }
235 elsif ( $type eq 'HASH' ) {
236 72 $cursor = $cursor->{$_};
237 }
238 elsif ($type) {
239 2 $cursor = $cursor->$_();
240 }
241 else {
242 0 return '';
243 }
244 }
245 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
246 3 my $path = join '.' => @path;
247 3 $_[0]->{_undefined}{"Undefined value in template path '$path'"} = 1;
248 3 return '';
249 }
250  
251 53 return $cursor;
252 }
253  
254 4 1;
255  
256 __END__
↑ Summary