blib/lib/Template/Tiny/Strict.pm

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