blib/lib/Text/Xslate/PP.pm

filter: Statementstmt 95.3% 326 / 342 Branchbran 87.5% 140 / 160 Conditioncond 92.1% 35 / 38 MC/DCmcdc 88.0% 22 / 25 Subroutinesub 100% 47 / 47 Podpod 16.0% 4 / 25 totaltotal 90.1% 574 / 637 CC 95 SCAR 20.0
CC
95
Cov
93%
_assemble
21.2
tx_match
11.0
tx_load_template
10.1
CRAP 7.4
« blib/lib/Text/Xslate/Runner.pm ↑ Summary blib/lib/Text/Xslate.pm »
1 package Text::Xslate::PP;
2 # Text::Xslate in pure Perl
3 190 ▶use 5.008_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 190 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 190 ▶use version; our $VERSION = version->declare('v3.5.9');
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 190 ▶BEGIN{
Subroutine BEGIN: called CC=2 SCAR=6.9
9 190 ▶ $ENV{XSLATE} = ($ENV{XSLATE} || '') . '[pp]';
Condition100%2/2
$ENV{'XSLATE'} || ''
l!l
5185
MC/DC100%1/1
$ENV{'XSLATE'} || ''
$ENV{'XSLATE'}
Truth table100%2/2
A
A= $ENV{'XSLATE'}
Aresult
11
00
10 }
11  
12 190 ▶use Text::Xslate::Util qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
13 $DEBUG
14 p
15 190 );
16  
17 190 ▶use constant _PP_ERROR_VERBOSE => scalar($DEBUG =~ /\b pp=verbose \b/xms);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 190 ▶use constant _DUMP_LOAD => scalar($DEBUG =~ /\b dump=load \b/xms);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 190 ▶use Text::Xslate::PP::Const qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 190 ▶use Text::Xslate::PP::State;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 190 ▶use Text::Xslate::PP::Type::Raw;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 190 ▶use Text::Xslate::PP::Opcode;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 190 ▶use Text::Xslate::PP::Method;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 190 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
28 190 ▶use overload ();
Subroutine BEGIN: called CC=1 SCAR=0.0
29 190 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 # it must be loaded dynamically
32 190 require Text::Xslate;
33  
34 190 my $state_class = 'Text::Xslate::PP::Opcode';
35  
36 190 $VERSION =~ s/_//; # for developers versions
37  
38 190 if(_PP_ERROR_VERBOSE) {
39 Carp->import('verbose');
40 }
41  
42 # fix up @ISA
43 {
44 190 package
45 Text::Xslate;
46 190 ▶ if(!our %OPS) {
Branch: 1 branch
BranchTrueFalseTotal
unless (our %OPS) 190 0 190
47 # the compiler use %Text::Xslate::OPS in order to optimize the code
48 190 *OPS = \%Text::Xslate::PP::OPS;
49 }
50 190 our @ISA = qw(Text::Xslate::PP);
51 package
52 Text::Xslate::PP;
53 190 our @ISA = qw(Text::Xslate::Engine);
54 }
55  
56 190 our $_depth = 0;
57 190 our $_current_st;
58  
59 190 our($_orig_die_handler, $_orig_warn_handler);
60  
61 190 our %html_escape = (
62 '&' => '&',
63 '<' => '&lt;',
64 '>' => '&gt;',
65 '"' => '&quot;',
66 "'" => '&#39;', # IE8 doesn't support &apos; in title
67 );
68 190 our $html_metachars = sprintf '[%s]', join '', map { quotemeta } keys %html_escape;
69  
70 298 ▶sub _register_builtin_methods {
Subroutine _register_builtin_methods: called CC=1 SCAR=0.0
71 298 my($self, $funcs) = @_;
72 298 Text::Xslate::PP::Method::tx_register_builtin_methods($funcs);
73 }
74  
75 #
76 # public APIs
77 #
78  
79 1367 ▶sub render_string {
Subroutine render_string: called CC=1 SCAR=0.0
Pod undocumented
80 1367 my($self, $string, $vars) = @_;
81 1367 $self->load_string($string);
82 1303 return $self->render('<string>', $vars);
83 }
84  
85 2338 ▶sub render {
Subroutine render: called CC=7 SCAR=19.5
Pod undocumented
86 2338 my ( $self, $name, $vars ) = @_;
87  
88 2338 ▶ Carp::croak("Usage: Text::Xslate::render(self, name, vars)")
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 or @_ == 3 1 2337 2338
Condition100%3/3
@_ == 2 or @_ == 3
l!l&&r!l&&!r
20221351
MC/DC100%2/2
@_ == 2 or @_ == 3
@_ == 2 @_ == 3
Truth table100%3/3
A or B
A= @_ == 2B= @_ == 3
ABresult
1X1
011
000
89 if !( @_ == 2 or @_ == 3 );
90 2337 ▶ unless ( ref $self ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 1 2336 2337
91 1 Carp::croak( "Invalid xslate instance" );
92 }
93  
94 2336 ▶ if(!defined $vars) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $vars) 378 1958 2336
95 378 $vars = {};
96 }
97  
98 2336 ▶ if ( !defined $name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 0 2336 2336
99 0 Carp::croak("Xslate: Template name is not given");
100 }
101  
102 2336 ▶ unless ( ref $vars eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $vars eq "HASH") 1 2335 2336
103 1 Carp::croak( sprintf("Xslate: Template variables must be a HASH reference, not %s", $vars ) );
104 }
105  
106 2335 my $st = $self->tx_load_template( $name, 0 );
107  
108 2329 local $_orig_die_handler = $SIG{__DIE__};
109 2329 local $_orig_warn_handler = $SIG{__WARN__};
110 2329 local $SIG{__DIE__} = \&_die;
111 2329 local $SIG{__WARN__} = \&_warn;
112  
113 2329 return tx_execute( $st, $vars );
114 }
115  
116 4 ▶sub validate {
Subroutine validate: called CC=4 SCAR=15.2
Pod undocumented
117 4 my ( $self, $name ) = @_;
118  
119 4 ▶ Carp::croak("Usage: Text::Xslate::render(self, name)")
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 0 4 4
120 if !( @_ == 2 );
121 4 ▶ unless ( ref $self ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 0 4 4
122 0 Carp::croak( "Invalid xslate instance" );
123 }
124  
125 4 ▶ if ( !defined $name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 0 4 4
126 0 Carp::croak("Xslate: Template name is not given");
127 }
128  
129 4 local $self->{cache} = 0; # do not touch the cache
130 4 $self->tx_load_template( $name, 0 );
131 2 return;
132 }
133  
134 2 ▶sub current_engine {
Subroutine current_engine: called CC=2 SCAR=6.9
Pod undocumented
135 2 ▶ return defined($_current_st) ? $_current_st->engine : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $Text::Xslate::PP::_current_st ? : 1 1 2
136 }
137  
138 3 ▶sub current_vars {
Subroutine current_vars: called CC=2 SCAR=6.9
Pod undocumented
139 3 ▶ return defined($_current_st) ? $_current_st->vars : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $Text::Xslate::PP::_current_st ? : 2 1 3
140 }
141  
142 2 ▶sub current_file {
Subroutine current_file: called CC=2 SCAR=6.9
Pod undocumented
143 return defined($_current_st)
144 ? $_current_st->code->[ $_current_st->{ pc } ]->{file}
145 2 ▶ : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $Text::Xslate::PP::_current_st ? : 1 1 2
146 }
147  
148 4 ▶sub current_line {
Subroutine current_line: called CC=2 SCAR=6.9
Pod undocumented
149 return defined($_current_st)
150 ? $_current_st->code->[ $_current_st->{ pc } ]->{line}
151 4 ▶ : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $Text::Xslate::PP::_current_st ? : 3 1 4
152 }
153  
154 8 ▶sub print {
Subroutine print: called CC=3 SCAR=11.4
Pod undocumented
155 8 shift;
156 8 ▶ if(defined $_current_st) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Text::Xslate::PP::_current_st) { } 8 0 8
157 8 foreach my $s(@_) {
158 11 $_current_st->print($s);
159 }
160 }
161 else {
162 0 Carp::croak('You cannot call print() method outside render()');
163 }
164 8 return '';
165 }
166  
167 # >> copied and modified from Text::Xslate
168  
169 3572 ▶sub _assemble {
Subroutine _assemble: called CC=20 SCAR=30.6
170 3572 my ( $self, $asm, $name, $fullpath, $cachepath, $mtime ) = @_;
171  
172 3572 ▶ unless ( defined $name ) { # $name ... filename
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 0 3572 3572
173 0 $name = '<string>';
174 0 $fullpath = $cachepath = undef;
175 0 $mtime = time();
176 }
177  
178 3572 my $st = $state_class->new();
179  
180 3572 $st->symbol({ %{$self->{ function }} });
181  
182 3572 my $tmpl = [];
183  
184 3572 $self->{ template }->{ $name } = $tmpl;
185 3572 $self->{ tmpl_st }->{ $name } = $st;
186  
187 3572 $tmpl->[ Text::Xslate::PP::TXo_MTIME ] = $mtime;
188 3572 $tmpl->[ Text::Xslate::PP::TXo_CACHEPATH ] = $cachepath;
189 3572 $tmpl->[ Text::Xslate::PP::TXo_FULLPATH ] = $fullpath;
190  
191 3572 $st->tmpl( $tmpl );
192 3572 $st->engine( $self ); # weak_ref!
193  
194 3572 $st->{sa} = undef;
195 3572 $st->{sb} = undef;
196  
197 # stack frame
198 3572 $st->frame( [] );
199 3572 $st->current_frame( -1 );
200  
201 3572 my $len = scalar( @$asm );
202  
203 3572 $st->push_frame('main', $len);
204  
205 3572 $st->code_len( $len );
206  
207 3572 my $code = $st->code([]);
208 3572 my $macro;
209  
210 3572 my $oi_line = -1;
211 3572 my $oi_file = $name;
212 3572 ▶ for ( my $i = 0; $i < $len; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $len) 41508 3572 45080
213 41508 my $c = $asm->[ $i ];
214  
215 41508 ▶ if ( ref $c ne 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $c ne "ARRAY") 0 41508 41508
216 0 Carp::croak( sprintf( "Oops: Broken code found on [%d]", $i ) );
217 }
218  
219 41508 my ( $opname, $arg, $line, $file ) = @{$c};
220 41508 my $opnum = $OPS{ $opname };
221  
222 41508 ▶ unless ( defined $opnum ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $opnum) 0 41508 41508
223 0 Carp::croak( sprintf( "Oops: Unknown opcode '%s' on [%d]", $opname, $i ) );
224 }
225  
226 41508 ▶ if(defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line) 17913 23595 41508
227 17913 $oi_line = $line;
228 }
229 41508 ▶ if(defined $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file) 6312 35196 41508
230 6312 $oi_file = $file;
231 }
232  
233 41508 $code->[$i] = {
234 # opcode
235 opname => $opname,
236  
237 # opinfo
238 line => $oi_line,
239 file => $oi_file,
240 };
241  
242 41508 $code->[ $i ]->{ exec_code } = $OPCODE[ $opnum ];
243  
244 41508 my $oparg = $OPARGS[ $opnum ];
245  
246 41508 ▶ if ( $oparg & TXARGf_SV ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($oparg & 1) { } 19541 21967 41508
247  
248 # This line croak at 'concat'!
249 # Carp::croak( sprintf( "Oops: Opcode %s must have an argument on [%d]", $opname, $i ) )
250 # unless ( defined $arg );
251  
252 19541 ▶ if( $oparg & TXARGf_KEY ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($oparg & 4) { } 4222 15319 19541
elsif ($oparg & 2) { } 3684 11635 15319
253 4222 $code->[ $i ]->{ arg } = $arg;
254 }
255 elsif ( $oparg & TXARGf_INT ) {
256 3684 $code->[ $i ]->{ arg } = int($arg);
257  
258 3684 ▶ if( $oparg & TXARGf_PC ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($oparg & 16) 1190 2494 3684
259 1190 my $abs_addr = $i + $arg;
260  
261 1190 ▶ if( $abs_addr >= $len ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($abs_addr >= $len) 0 1190 1190
262 0 Carp::croak(
263 sprintf( "Oops: goto address %d is out of range (must be 0 <= addr <= %d)", $arg, $len )
264 );
265 }
266  
267 1190 $code->[ $i ]->{ arg } = $abs_addr;
268 }
269  
270 }
271 else {
272 11635 $code->[ $i ]->{ arg } = $arg;
273 }
274  
275 }
276 else {
277 21967 ▶ if( defined $arg ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg) 0 21967 21967
278 0 Carp::croak( sprintf( "Oops: Opcode %s has an extra argument on [%d]", $opname, $i ) );
279 }
280 21967 $code->[ $i ]->{ arg } = undef;
281 }
282  
283 # special cases
284 41508 ▶ if( $opnum == $OPS{ macro_begin } ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($opnum == $Text::Xslate::PP::OPS{'macro_begin'}) { } 330 41178 41508
elsif ($opnum == $Text::Xslate::PP::OPS{'macro_nargs'}) { } 91 41087 41178
elsif ($opnum == $Text::Xslate::PP::OPS{'macro_outer'}) { } 13 41074 41087
elsif ($opnum == $Text::Xslate::PP::OPS{'depend'}) { } 103 40971 41074
285 330 my $name = $code->[ $i ]->{ arg };
286 330 ▶ if(!exists $st->symbol->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $st->symbol->{$name}) { } 330 0 330
287 330 require Text::Xslate::PP::Type::Macro;
288 330 $macro = Text::Xslate::PP::Type::Macro->new(
289 name => $name,
290 addr => $i,
291 state => $st,
292 );
293 330 $st->symbol->{ $name } = $macro;
294 }
295 else {
296 0 $macro = undef;
297 }
298 }
299 elsif( $opnum == $OPS{ macro_nargs } ) {
300 91 ▶ if($macro) {
Branch: 1 branch
BranchTrueFalseTotal
if ($macro) 91 0 91
301 91 $macro->nargs($code->[$i]->{arg});
302 }
303 }
304 elsif( $opnum == $OPS{ macro_outer } ) {
305 13 ▶ if($macro) {
Branch: 1 branch
BranchTrueFalseTotal
if ($macro) 13 0 13
306 13 $macro->outer($code->[$i]->{arg});
307 }
308 }
309 elsif( $opnum == $OPS{ depend } ) {
310 103 push @{ $tmpl }, $code->[ $i ]->{ arg };
311 }
312  
313 }
314  
315 3572 push @{$code}, {
316 3572 exec_code => $OPCODE[ $OPS{end} ],
317 file => $oi_file,
318 line => $oi_line,
319 opname => 'end',
320 }; # for threshold
321 3572 return;
322 }
323  
324 {
325 package
326 Text::Xslate::Util;
327  
328 190 sub escaped_string; *escaped_string = \&mark_raw;
329 1321 ▶ sub mark_raw {
Subroutine mark_raw: called CC=3 SCAR=11.0
Pod documented
330 1321 my($str) = @_;
331 1321 ▶ if(defined $str) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $str) 1319 2 1321
332 1319 ▶ return ref($str) eq Text::Xslate::PP::TXt_RAW()
Branch: 1 branch
BranchTrueFalseTotal
ref $str eq 'Text::Xslate::Type::Raw' ? : 2 1317 1319
333 ? $str
334 : bless \$str, Text::Xslate::PP::TXt_RAW();
335 }
336 2 return $str; # undef
337 }
338 17 ▶ sub unmark_raw {
Subroutine unmark_raw: called CC=2 SCAR=6.9
Pod documented
339 17 my($str) = @_;
340 return ref($str) eq Text::Xslate::PP::TXt_RAW()
341 17 ▶ ? ${$str}
Branch: 1 branch
BranchTrueFalseTotal
ref $str eq 'Text::Xslate::Type::Raw' ? : 13 4 17
342 : $str;
343 }
344  
345 36 ▶ sub html_escape {
Subroutine html_escape: called CC=3 SCAR=11.0
Pod documented
346 36 my($s) = @_;
347 36 ▶ return $s if
Branch: 1 branch
BranchTrueFalseTotal
if ref $s eq "Text::Xslate::Type::Raw" or not defined $s 5 31 36
Condition100%3/3
ref $s eq "Text::Xslate::Type::Raw" or not defined $s
l!l&&r!l&&!r
3231
MC/DC100%2/2
ref $s eq "Text::Xslate::Type::Raw" or not defined $s
ref $s eq "Text::Xslate::Type::Raw" not defined $s
Truth table100%3/3
A or not(B)
A= ref $s eq "Text::Xslate::Type::Raw"B= not defined $s
ABresult
1X1
011
000
348 ref($s) eq Text::Xslate::PP::TXt_RAW()
349 or !defined($s);
350  
351 31 $s =~ s/($html_metachars)/$html_escape{$1}/xmsgeo;
352 31 return bless \$s, Text::Xslate::PP::TXt_RAW();
353 }
354  
355 190 my $uri_unsafe_rfc3986 = qr/[^A-Za-z0-9\-\._~]/;
356 2150 ▶ sub uri_escape {
Subroutine uri_escape: called CC=3 SCAR=11.0
Pod documented
357 2150 my($s) = @_;
358 2150 ▶ return $s if not defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 2 2148 2150
359 # XXX: This must be the same as uri_escape() in XS.
360 # See also tx_uri_escape() in xs/Text-Xslate.xs.
361 2148 ▶ utf8::encode($s) if utf8::is_utf8($s);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($s) 2 2146 2148
362 2148 $s =~ s/($uri_unsafe_rfc3986)/sprintf '%%' . '%02X', ord $1/xmsgeo;
363 2148 return $s;
364 }
365  
366 5 ▶ sub is_array_ref { ref($_[0]) eq 'ARRAY' }
Subroutine is_array_ref: called CC=1 SCAR=0.0
Pod undocumented
367 5 ▶ sub is_hash_ref { ref($_[0]) eq 'HASH' }
Subroutine is_hash_ref: called CC=1 SCAR=0.0
Pod undocumented
368 2 ▶ sub is_code_ref { ref($_[0]) eq 'CODE' }
Subroutine is_code_ref: called CC=1 SCAR=0.0
Pod undocumented
369  
370 6 ▶ sub merge_hash { +{ %{ $_[0] }, %{ $_[1] } } }
Subroutine merge_hash: called CC=1 SCAR=0.0
Pod undocumented
371 }
372  
373 #
374 # INTERNAL
375 #
376  
377 190 ▶sub tx_check_itr_ar {
Subroutine tx_check_itr_ar: called CC=4 SCAR=13.9
Pod undocumented
378 202 my ( $st, $ar, $frame, $line ) = @_;
379 202 ▶ return $ar if ref($ar) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $ar eq "ARRAY" 189 13 202
380  
381 13 ▶ if ( defined $ar ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ar) 6 7 13
382 6 ▶ if(my $x = Text::Xslate::PP::sv_is_ref($ar, 'ARRAY', '@{}')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $x = sv_is_ref($ar, "ARRAY", "\@{}")) 2 4 6
383 2 return $x;
384 }
385  
386 4 $st->error( [$frame, $line],
387 "Iterator variables must be an ARRAY reference, not %s",
388 Text::Xslate::Util::neat( $ar ) );
389 }
390 11 return [];
391 }
392  
393 6 ▶sub sv_is_ref {
Subroutine sv_is_ref: called CC=5 SCAR=16.1
Pod undocumented
394 6 my($sv, $t, $ov) = @_;
395 6 ▶ return $sv if ref($sv) eq $t;
Branch: 1 branch
BranchTrueFalseTotal
if ref $sv eq $t 0 6 6
396  
397 6 ▶ if(Scalar::Util::blessed($sv)
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($sv) and my $m = overload::Method($sv, $ov)) 3 3 6
Condition100%3/3
builtin::blessed($sv) and my $m = overload::Method($sv, $ov)
!ll&&!rl&&r
213
MC/DC100%2/2
builtin::blessed($sv) and my $m = overload::Method($sv, $ov)
builtin::blessed($sv) my $m = overload::Method($sv, $ov)
Truth table100%3/3
A and B
A= builtin::blessed($sv)B= my $m = overload::Method($sv, $ov)
ABresult
0X0
100
111
398 && (my $m = overload::Method($sv, $ov))) {
399 3 $sv = $sv->$m(undef, undef);
400 3 ▶ return $sv if ref($sv) eq $t;
Branch: 1 branch
BranchTrueFalseTotal
if ref $sv eq $t 2 1 3
401 }
402 4 return undef;
403 }
404  
405 585 ▶sub tx_sv_eq {
Subroutine tx_sv_eq: called CC=3 SCAR=11.0
Pod undocumented
406 585 my($x, $y) = @_;
407 585 ▶ if ( defined $x ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $x) { } 581 4 585
408 581 ▶ return defined $y && $x eq $y;
Condition100%3/3
defined $y && $x eq $y
!ll&&!rl&&r
115215251
MC/DC100%2/2
defined $y && $x eq $y
defined $y $x eq $y
Truth table100%3/3
A && B
A= defined $yB= $x eq $y
ABresult
0X0
100
111
409 }
410 else {
411 4 return !defined $y;
412 }
413 }
414  
415 32 ▶sub tx_match { # simple smart matching
Subroutine tx_match: called CC=11 SCAR=24.0
Pod undocumented
416 32 my($x, $y) = @_;
417  
418 32 ▶ if(ref($y) eq 'ARRAY') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $y eq 'ARRAY') { } 4 28 32
elsif (ref $y eq 'HASH') { } 3 25 28
elsif (defined $y) { } 22 3 25
419 4 foreach my $item(@{$y}) {
420 9 ▶ if(defined($item)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $item) { } 7 2 9
421 7 ▶ if(defined($x) && $x eq $item) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $x and $x eq $item) 2 5 7
Condition100%3/3
defined $x and $x eq $item
!ll&&!rl&&r
232
MC/DC100%2/2
defined $x and $x eq $item
defined $x $x eq $item
Truth table100%3/3
A and B
A= defined $xB= $x eq $item
ABresult
0X0
100
111
422 2 return 1;
423 }
424 }
425 else {
426 2 ▶ if(not defined($x)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $x) 1 1 2
427 1 return 1;
428 }
429 }
430 }
431 1 return '';
432 }
433 elsif(ref($y) eq 'HASH') {
434 3 ▶ return defined($x) && exists $y->{$x};
Condition66%2/3
defined $x && exists $y->{$x}
!ll&&!rl&&r
012
MC/DC50%1/2
defined $x && exists $y->{$x}
defined $x exists $y->{$x}
Truth table66%2/3
A && B
A= defined $xB= exists $y->{$x}
ABresult
0X0
100
111
435 }
436 elsif(defined($y)) {
437 22 ▶ return defined($x) && $x eq $y;
Condition100%3/3
defined $x && $x eq $y
!ll&&!rl&&r
5710
MC/DC100%2/2
defined $x && $x eq $y
defined $x $x eq $y
Truth table100%3/3
A && B
A= defined $xB= $x eq $y
ABresult
0X0
100
111
438 }
439 else {
440 3 return !defined($x);
441 }
442 }
443  
444 94 ▶sub tx_concat {
Subroutine tx_concat: called CC=6 SCAR=17.9
Pod undocumented
445 94 my($lhs, $rhs) = @_;
446 94 ▶ if (!defined $lhs){
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $lhs) { } 5 89 94
elsif (not defined $rhs) { } 4 85 89
447 5 return $rhs;
448 }elsif(!defined $rhs){
449 4 return $lhs;
450 }
451 85 ▶ if(ref($lhs) eq TXt_RAW) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $lhs eq 'Text::Xslate::Type::Raw') { } 10 75 85
452 10 ▶ if(ref($rhs) eq TXt_RAW) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rhs eq 'Text::Xslate::Type::Raw') { } 5 5 10
453 5 return Text::Xslate::Util::mark_raw(${ $lhs } . ${ $rhs });
454 }
455 else {
456 5 return Text::Xslate::Util::mark_raw(${ $lhs } . Text::Xslate::Util::html_escape($rhs));
457 }
458 }
459 else {
460 75 ▶ if(ref($rhs) eq TXt_RAW) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rhs eq 'Text::Xslate::Type::Raw') { } 7 68 75
461 7 return Text::Xslate::Util::mark_raw(Text::Xslate::Util::html_escape($lhs) . ${ $rhs });
462 }
463 else {
464 68 return $lhs . $rhs;
465 }
466 }
467 }
468  
469 8 ▶sub tx_repeat {
Subroutine tx_repeat: called CC=4 SCAR=14.0
Pod undocumented
470 8 my($lhs, $rhs) = @_;
471 8 ▶ if(!defined($lhs)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $lhs) { } 2 6 8
elsif (not &Scalar::Util::looks_like_number($rhs)) { } 0 6 6
472 2 $_current_st->warn(undef, "Use of nil for repeat operator");
473 }
474 elsif(!Scalar::Util::looks_like_number($rhs)) {
475 0 $_current_st->error(undef, "Repeat count must be a number, not %s",
476 Text::Xslate::Util::neat($rhs));
477 }
478 else {
479 6 ▶ if( ref( $lhs ) eq TXt_RAW ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $lhs eq 'Text::Xslate::Type::Raw') { } 2 4 6
480 2 return Text::Xslate::Util::mark_raw( Text::Xslate::Util::unmark_raw($lhs) x $rhs );
481 }
482 else {
483 4 return $lhs x $rhs;
484 }
485 }
486 2 return '';
487 }
488  
489  
490 3585 ▶sub tx_load_template {
Subroutine tx_load_template: called CC=10 SCAR=23.2
Pod undocumented
491 3585 my ( $self, $name, $from_include ) = @_;
492  
493 3585 ▶ unless ( ref $self ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 0 3585 3585
494 0 Carp::croak( "Invalid xslate instance" );
495 }
496  
497 3585 my $ttable = $self->{ template };
498 3585 my $retried = 0;
499  
500 3585 ▶ if(ref $ttable ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ttable ne "HASH") 1 3584 3585
501 1 Carp::croak(
502 sprintf( "Xslate: Cannot load template '%s': %s", $name, "template table is not a HASH reference" )
503 );
504 }
505  
506 RETRY:
507 5593 ▶ if( $retried > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($retried > 1) 0 5593 5593
508 0 Carp::croak(
509 sprintf( "Xslate: Cannot load template '%s': %s", $name, "retried reloading, but failed" )
510 );
511 }
512  
513 5593 ▶ if ( not exists $ttable->{ $name } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $ttable->{$name}) 194 5399 5593
514 194 $self->load_file( $name, undef, $from_include );
515 185 $retried++;
516 185 goto RETRY;
517 }
518  
519 5399 my $tmpl = $ttable->{ $name };
520  
521 5399 ▶ if(ref($tmpl) ne 'ARRAY' or not exists $self->{tmpl_st}{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $tmpl ne "ARRAY" or not exists $self->{"tmpl_st"}{$name}) 2 5397 5399
Condition100%3/3
ref $tmpl ne "ARRAY" or not exists $self->{"tmpl_st"}{$name}
l!l&&r!l&&!r
115397
MC/DC100%2/2
ref $tmpl ne "ARRAY" or not exists $self->{"tmpl_st"}{$name}
ref $tmpl ne "ARRAY" not exists $self->{"tmpl_st"}{$name}
Truth table100%3/3
A or not(B)
A= ref $tmpl ne "ARRAY"B= not exists $self->{"tmpl_st"}{$name}
ABresult
1X1
011
000
522 2 Carp::croak(
523 sprintf( "Xslate: Cannot load template '%s': template entry is invalid", $name ),
524 );
525 }
526  
527 5397 my $cache_mtime = $tmpl->[ TXo_MTIME ];
528  
529 5397 ▶ if(not defined $cache_mtime) { # cache => 2 (release mode)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cache_mtime) 1514 3883 5397
530 1514 return $self->{ tmpl_st }->{ $name };
531 }
532  
533 3883 ▶ if( $retried > 0 or tx_all_deps_are_fresh( $tmpl, $cache_mtime ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($retried > 0 or tx_all_deps_are_fresh($tmpl, $cache_mtime)) { } 2059 1824 3883
Condition100%3/3
$retried > 0 or tx_all_deps_are_fresh($tmpl, $cache_mtime)
l!l&&r!l&&!r
2000591824
MC/DC100%2/2
$retried > 0 or tx_all_deps_are_fresh($tmpl, $cache_mtime)
$retried > 0 tx_all_deps_are_fresh($tmpl, $cache_mtime)
Truth table100%3/3
A or B
A= $retried > 0B= tx_all_deps_are_fresh($tmpl, $cache_mtime)
ABresult
1X1
011
000
534 2059 return $self->{ tmpl_st }->{ $name };
535 }
536 else{
537 1824 $self->load_file( $name, $cache_mtime, $from_include );
538 1824 $retried++;
539 1824 goto RETRY;
540 }
541  
542 0 Carp::croak("Oops: Not reached");
543 }
544  
545  
546 1883 ▶sub tx_all_deps_are_fresh {
Subroutine tx_all_deps_are_fresh: called CC=8 SCAR=20.9
Pod undocumented
547 1883 my ( $tmpl, $cache_mtime ) = @_;
548 1883 my $len = scalar @{$tmpl};
549  
550 1883 ▶ for ( my $i = TXo_FULLPATH; $i < $len; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $len) 1895 59 1954
551 1895 my $deppath = $tmpl->[ $i ];
552  
553 1895 ▶ next if ref $deppath;
Branch: 1 branch
BranchTrueFalseTotal
if ref $deppath 39 1856 1895
554  
555 1856 my $mtime = ( stat( $deppath ) )[9];
556 1856 ▶ if ( defined($mtime) and $mtime > $cache_mtime ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mtime and $mtime > $cache_mtime) 1824 32 1856
Condition66%2/3
defined $mtime and $mtime > $cache_mtime
!ll&&!rl&&r
0321824
MC/DC50%1/2
defined $mtime and $mtime > $cache_mtime
defined $mtime $mtime > $cache_mtime
Truth table66%2/3
A and B
A= defined $mtimeB= $mtime > $cache_mtime
ABresult
0X0
100
111
557 1824 my $main_cache = $tmpl->[ TXo_CACHEPATH ];
558 1824 ▶ if ( $i != TXo_FULLPATH and $main_cache ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i != 2 and $main_cache) 2 1822 1824
Condition66%2/3
$i != 2 and $main_cache
!ll&&!rl&&r
182202
MC/DC50%1/2
$i != 2 and $main_cache
$i != 2 $main_cache
Truth table66%2/3
A and B
A= $i != 2B= $main_cache
ABresult
0X0
100
111
559 2 ▶ unlink $main_cache or warn $!;
Branch: 1 branch
BranchTrueFalseTotal
unlink $main_cache or warn $! 2 0 2
560 }
561 1824 if(_DUMP_LOAD) {
562 printf STDERR " tx_all_depth_are_fresh: %s is too old (%d > %d)\n",
563 $deppath, $mtime, $cache_mtime;
564 }
565 1824 return 0;
566 }
567  
568 }
569  
570 59 return 1;
571 }
572  
573 3571 ▶sub tx_execute {
Subroutine tx_execute: called CC=3 SCAR=11.0
Pod undocumented
574 3571 my ( $st, $vars ) = @_;
575 190 ▶ no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
576  
577 3571 ▶ if ( $_depth > 100 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Text::Xslate::PP::_depth > 100) 4 3567 3571
578 4 Carp::croak("Execution is too deep (> 100)");
579 }
580 3567 if(_PP_ERROR_VERBOSE and ref $st->{code}->[0]->{ exec_code } ne 'CODE') {
581 Carp::croak("Oops: Not a CODE reference: "
582 . Text::Xslate::Util::neat($st->{code}->[0]->{ exec_code }));
583 }
584  
585 3567 local $st->{pc} = 0;
586 3567 local $st->{vars} = $vars;
587  
588 3567 local $_depth = $_depth + 1;
589 3567 local $_current_st = $st;
590  
591 3567 local $st->{local_stack};
592 3567 local $st->{SP} = [];
593  
594 3567 local $st->{sa};
595 3567 local $st->{sb};
596 3567 local $st->{output} = '';
597  
598 3567 local $st->{current_frame} = $st->{current_frame};
599  
600 3567 eval {
601 3567 $st->{code}->[0]->{ exec_code }->( $st );
602 };
603  
604 3567 @{$st->{frame}->[-1]} = Text::Xslate::PP::TXframe_START_LVAR - 1;
605  
606 3567 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 425 3142 3567
607 425 my $e = $@;
608 425 die $e;
609 }
610 3142 return $st->{output};
611 }
612  
613  
614 525 ▶sub _error_handler {
Subroutine _error_handler: called CC=8 SCAR=20.8
615 525 my ( $str, $die ) = @_;
616 525 my $st = $_current_st;
617  
618 525 local $SIG{__WARN__} = $_orig_warn_handler;
619 525 local $SIG{__DIE__} = $_orig_die_handler;
620  
621 525 ▶ if(!_PP_ERROR_VERBOSE && $str =~ s/at .+Text.Xslate.PP.+ line \d+\.\n$//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ s/at .+Text.Xslate.PP.+ line \d+\.\n$//) 8 517 525
622 8 $str = Carp::shortmess($str);
623 }
624  
625 525 ▶ Carp::croak( $str ) unless defined $st;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $st 0 525 525
626  
627 525 my $engine = $st->engine;
628  
629 525 my $cframe = $st->frame->[ $st->current_frame ];
630 525 my $name = $cframe->[ Text::Xslate::PP::TXframe_NAME ];
631  
632 525 my $opcode = $st->code->[ $st->{ pc } ];
633 525 my $file = $opcode->{file};
634 525 ▶ if($file eq '<string>' && exists $engine->{string_buffer}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file eq "<string>" and exists $engine->{"string_buffer"}) 99 426 525
Condition100%3/3
$file eq "<string>" and exists $engine->{"string_buffer"}
!ll&&!rl&&r
419799
MC/DC100%2/2
$file eq "<string>" and exists $engine->{"string_buffer"}
$file eq "<string>" exists $engine->{"string_buffer"}
Truth table100%3/3
A and B
A= $file eq "<string>"B= exists $engine->{"string_buffer"}
ABresult
0X0
100
111
635 99 $file = \$engine->{string_buffer};
636 }
637  
638 my $mess = Text::Xslate::Util::make_error($engine, $str, $file, $opcode->{line},
639 525 sprintf( "&%s[%d]", $name, $st->{pc} ));
640  
641 525 ▶ if ( !$die ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $die) { } 62 463 525
642 # $h can ignore warnings
643 62 ▶ if ( my $h = $engine->{ warn_handler } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $h = $engine->{'warn_handler'}) { } 55 7 62
644 55 $h->( $mess );
645 }
646 else {
647 7 warn $mess;
648 }
649 }
650 else {
651 # $h cannot ignore errors
652 463 ▶ if(my $h = $engine->{ die_handler } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $h = $engine->{"die_handler"}) 2 461 463
653 2 $h->( $mess );
654 }
655 463 die $mess; # MUST DIE!
656 }
657 56 return;
658 }
659  
660 62 ▶sub _warn {
Subroutine _warn: called CC=1 SCAR=0.0
661 62 _error_handler( $_[0], 0 );
662 }
663  
664 463 ▶sub _die {
Subroutine _die: called CC=1 SCAR=0.0
665 463 _error_handler( $_[0], 1 );
666 }
667  
668 {
669 package
670 Text::Xslate::PP::Guard;
671  
672 47 ▶ sub DESTROY { $_[0]->() }
Subroutine DESTROY: called CC=1 SCAR=0.0
673 }
674  
675 190 1;
676 __END__
« blib/lib/Text/Xslate/Runner.pm ↑ Summary blib/lib/Text/Xslate.pm »