Simple/Base/Compiler.pm

filter: Statementstmt 95.3% 122 / 128 Branchbran 73.6% 53 / 72 Conditioncond 66.6% 32 / 48 MC/DCmcdc 44.8% 13 / 29 Subroutinesub 100% 12 / 12 Podpod n/a totaltotal 80.2% 232 / 289 CC 58 SCAR 24.6
CC
58
Cov
83%
_compile
21.3
_init_compile_opts
11.0
_wrap_compile
10.9
CRAP 11.7
↑ Summary Simple/IO.pm »
1 ## no critic (ProhibitUnusedPrivateSubroutines)
2 package Text::Template::Simple::Base::Compiler;
3 32 $Text::Template::Simple::Base::Compiler::VERSION = '0.91';
4 32 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 32 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 32 ▶use Text::Template::Simple::Util qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 32 ▶use Text::Template::Simple::Constants qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 253 ▶sub _init_compile_opts {
Subroutine _init_compile_opts: called CC=11 SCAR=24.0
11 253 my $self = shift;
12 253 ▶ my $opt = shift || {};
Condition100%2/2
shift() || {}
l!l
137116
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
13  
14 253 ▶ fatal('tts.base.compiler._compile.opt') if ref $opt ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $opt ne "HASH" 0 253 253
15  
16 # set defaults
17 253 ▶ $opt->{id} ||= EMPTY_STRING; # id is AUTO
Condition100%2/2
$opt->{"id"} ||= ""
l!l
2251
MC/DC100%1/1
$opt->{"id"} ||= ""
$opt->{"id"}
Truth table100%2/2
A
A= $opt->{"id"}
Aresult
11
00
18 253 ▶ $opt->{map_keys} ||= 0; # use normal behavior
Condition100%2/2
$opt->{"map_keys"} ||= 0
l!l
2251
MC/DC100%1/1
$opt->{"map_keys"} ||= 0
$opt->{"map_keys"}
Truth table100%2/2
A
A= $opt->{"map_keys"}
Aresult
11
00
19 253 ▶ $opt->{chkmt} ||= 0; # check mtime of file template?
Condition100%2/2
$opt->{"chkmt"} ||= 0
l!l
125128
MC/DC100%1/1
$opt->{"chkmt"} ||= 0
$opt->{"chkmt"}
Truth table100%2/2
A
A= $opt->{"chkmt"}
Aresult
11
00
20 253 ▶ $opt->{_sub_inc} ||= 0; # are we called from a dynamic include op?
Condition100%2/2
$opt->{"_sub_inc"} ||= 0
l!l
133120
MC/DC100%1/1
$opt->{"_sub_inc"} ||= 0
$opt->{"_sub_inc"}
Truth table100%2/2
A
A= $opt->{"_sub_inc"}
Aresult
11
00
21 253 ▶ $opt->{_filter} ||= EMPTY_STRING; # any filters?
Condition100%2/2
$opt->{"_filter"} ||= ""
l!l
1252
MC/DC100%1/1
$opt->{"_filter"} ||= ""
$opt->{"_filter"}
Truth table100%2/2
A
A= $opt->{"_filter"}
Aresult
11
00
22  
23 # first element is the shared names. if it's not defined, then there
24 # are no shared variables from top level
25 253 ▶ if ( ref $opt->{_share} eq 'ARRAY' && ! defined $opt->{_share}[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opt->{"_share"} eq "ARRAY" and not defined $opt->{"_share"}[0]) 6 247 253
Condition100%3/3
ref $opt->{"_share"} eq "ARRAY" and not defined $opt->{"_share"}[0]
!ll&&!rl&&r
24436
MC/DC100%2/2
ref $opt->{"_share"} eq "ARRAY" and not defined $opt->{"_share"}[0]
ref $opt->{"_share"} eq "ARRAY" not defined $opt->{"_share"}[0]
Truth table100%3/3
A and not(B)
A= ref $opt->{"_share"} eq "ARRAY"B= not defined $opt->{"_share"}[0]
ABresult
0X0
100
111
26 6 delete $opt->{_share};
27 }
28  
29 253 ▶ $opt->{as_is} = $opt->{_sub_inc} && $opt->{_sub_inc} == T_STATIC;
Condition100%3/3
$opt->{'_sub_inc'} && $opt->{'_sub_inc'} == 10
!ll&&!rl&&r
12012211
MC/DC100%2/2
$opt->{'_sub_inc'} && $opt->{'_sub_inc'} == 10
$opt->{'_sub_inc'} $opt->{'_sub_inc'} == 10
Truth table100%3/3
A && B
A= $opt->{'_sub_inc'}B= $opt->{'_sub_inc'} == 10
ABresult
0X0
100
111
30  
31 253 return $opt;
32 }
33  
34 125 ▶sub _validate_chkmt {
Subroutine _validate_chkmt: called CC=3 SCAR=11.0
35 125 my($self, $chkmt_ref, $tmpx) = @_;
36 125 ${$chkmt_ref} = $self->[TYPE] eq 'FILE'
37 ? (stat $tmpx)[STAT_MTIME]
38 125 ▶ : do {
Branch: 1 branch
BranchTrueFalseTotal
$self->[18] eq 'FILE' ? : 124 1 125
39 1 ▶ DEBUG && LOG( DISABLE_MT =>
Branch: 1 branch
BranchTrueFalseTotal
DEBUG() and LOG("DISABLE_MT", "Disabling chkmt. Template is not a file") 0 1 1
40 'Disabling chkmt. Template is not a file');
41 1 0;
42 };
43 125 return;
44 }
45  
46 9 ▶sub _compile_cache {
Subroutine _compile_cache: called CC=6 SCAR=19.5
47 9 my($self, $tmp, $opt, $id_ref, $code_ref) = @_;
48 9 my $method = $opt->{id};
49 9 ▶ my $auto_id = ! $method || $method eq 'AUTO';
Condition66%2/3
!$method || $method eq 'AUTO'
l!l&&r!l&&!r
702
MC/DC50%1/2
!$method || $method eq 'AUTO'
!$method $method eq 'AUTO'
Truth table66%2/3
not(A) || B
A= !$methodB= $method eq 'AUTO'
ABresult
1X1
011
000
50 9 ▶ ${ $id_ref } = $self->connector('Cache::ID')->new->generate(
Branch: 1 branch
BranchTrueFalseTotal
$auto_id ? : 7 2 9
51 $auto_id ? ( $tmp ) : ( $method, 'custom' )
52 );
53  
54 # prevent overwriting the compiled version in cache
55 # since we need the non-compiled version
56 0 ▶ ${ $id_ref } .= '_1' if $opt->{as_is};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"as_is"} 0 9 9
57  
58 9 ${ $code_ref } = $self->cache->hit( ${$id_ref}, $opt->{chkmt} );
59 0 ▶ LOG( CACHE_HIT => ${$id_ref} ) if DEBUG && ${$code_ref};
Branch: 1 branch
BranchTrueFalseTotal
if DEBUG() and ${$code_ref;} 0 9 9
Condition33%1/3
DEBUG() and ${$code_ref;}
!ll&&!rl&&r
900
MC/DC0%0/2
DEBUG() and ${$code_ref;}
DEBUG() ${$code_ref;}
Truth table33%1/3
A and B
A= DEBUG()B= ${$code_ref;}
ABresult
0X0
100
111
60 9 return;
61 }
62  
63 253 ▶sub _compile {
Subroutine _compile: called CC=20 SCAR=30.6
64 253 my $self = shift;
65 253 ▶ my $tmpx = shift || fatal('tts.base.compiler._compile.notmp');
Condition33%1/3
shift() || fatal('tts.base.compiler._compile.notmp')
l!l&&r!l&&!r
25300
MC/DC0%0/2
shift() || fatal('tts.base.compiler._compile.notmp')
shift() fatal('tts.base.compiler._compile.notmp')
Truth table33%1/3
A || B
A= shift()B= fatal('tts.base.compiler._compile.notmp')
ABresult
1X1
011
000
66 253 ▶ my $param = shift || [];
Condition100%2/2
shift() || []
l!l
13240
MC/DC100%1/1
shift() || []
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
67 253 my $opt = $self->_init_compile_opts( shift );
68  
69 253 ▶ fatal('tts.base.compiler._compile.param') if ref $param ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $param ne "ARRAY" 0 253 253
70  
71 253 my $tmp = $self->_examine( $tmpx );
72 251 ▶ return $tmp if $self->[TYPE] eq 'ERROR';
Branch: 1 branch
BranchTrueFalseTotal
if $self->[18] eq "ERROR" 0 251 251
73  
74 251 ▶ if ( $opt->{_sub_inc} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"_sub_inc"}) 133 118 251
75 # TODO:generate a single error handler for includes, merge with _include()
76 # tmpx is a "file" included from an upper level compile()
77 133 my $etitle = $self->_include_error( T_DYNAMIC );
78 133 my $exists = $self->io->file_exists( $tmpx );
79 133 ▶ return $etitle . " '$tmpx' is not a file" if not $exists;
Branch: 1 branch
BranchTrueFalseTotal
unless $exists 0 133 133
80 # TODO: remove this second call somehow, reduce to a single call
81 133 $tmp = $self->_examine( $exists ); # re-examine
82 133 $self->[NEEDS_OBJECT]++; # interpolated includes will need that
83 }
84  
85 251 ▶ $self->_validate_chkmt( \$opt->{chkmt}, $tmpx ) if $opt->{chkmt};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"chkmt"} 125 126 251
86  
87 251 ▶ LOG( COMPILE => $opt->{id} ) if DEBUG && defined $opt->{id};
Branch: 1 branch
BranchTrueFalseTotal
if DEBUG() and defined $opt->{"id"} 0 251 251
Condition33%1/3
DEBUG() and defined $opt->{"id"}
!ll&&!rl&&r
25100
MC/DC0%0/2
DEBUG() and defined $opt->{"id"}
DEBUG() defined $opt->{"id"}
Truth table33%1/3
A and B
A= DEBUG()B= defined $opt->{"id"}
ABresult
0X0
100
111
88  
89 251 my $cache_id = EMPTY_STRING;
90  
91 251 my($CODE);
92 251 ▶ $self->_compile_cache( $tmp, $opt, \$cache_id, \$CODE ) if $self->[CACHE];
Branch: 1 branch
BranchTrueFalseTotal
if $self->[8] 9 242 251
93  
94 251 $self->cache->id( $cache_id ); # if $cache_id;
95 251 ▶ $self->[FILENAME] = $self->[TYPE] eq 'FILE' ? $tmpx : $self->cache->id;
Branch: 1 branch
BranchTrueFalseTotal
$self->[18] eq 'FILE' ? : 150 101 251
96  
97 251 ▶ my($shead, @sparam) = $opt->{_share} ? @{$opt->{_share}} : ();
Branch: 1 branch
BranchTrueFalseTotal
$opt->{'_share'} ? : 3 248 251
98  
99 251 ▶ LOG(
Branch: 1 branch
BranchTrueFalseTotal
if DEBUG() and $shead 0 251 251
Condition33%1/3
DEBUG() and $shead
!ll&&!rl&&r
25100
MC/DC0%0/2
DEBUG() and $shead
DEBUG() $shead
Truth table33%1/3
A and B
A= DEBUG()B= $shead
ABresult
0X0
100
111
100 SHARED_VARS => "Adding shared variables ($shead) from a dynamic include"
101 ) if DEBUG && $shead;
102  
103 251 ▶ $CODE = $self->_cache_miss( $cache_id, $shead, \@sparam, $opt, $tmp ) if ! $CODE;
Branch: 1 branch
BranchTrueFalseTotal
unless $CODE 249 2 251
104  
105 250 my @args;
106 250 ▶ push @args, $self if $self->[NEEDS_OBJECT]; # must be the first
Branch: 1 branch
BranchTrueFalseTotal
if $self->[23] 206 44 250
107 250 ▶ push @args, @sparam if @sparam;
Branch: 1 branch
BranchTrueFalseTotal
if @sparam 3 247 250
108 250 ▶ push @args, @{ $self->[ADD_ARGS] } if $self->[ADD_ARGS];
Branch: 1 branch
BranchTrueFalseTotal
if $self->[15] 6 244 250
109 250 push @args, @{ $param };
110 250 my $out = $CODE->( @args );
111  
112 $self->_call_filters( \$out, split RE_FILTER_SPLIT, $opt->{_filter} )
113 250 ▶ if $opt->{_filter};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"_filter"} 1 249 250
114  
115 250 return $out;
116 }
117  
118 249 ▶sub _cache_miss {
Subroutine _cache_miss: called CC=5 SCAR=16.1
119 249 my($self, $cache_id, $shead, $sparam, $opt, $tmp) = @_;
120 # we have a cache miss; parse and compile
121 249 ▶ LOG( CACHE_MISS => $cache_id ) if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if DEBUG() 0 249 249
122  
123 249 my $restore_header;
124 249 ▶ if ( $shead ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($shead) 3 246 249
125 3 my $param_x = join q{,}, ('shift') x @{ $sparam };
126 3 my $shared = sprintf q~my(%s) = (%s);~, $shead, $param_x;
127 3 $restore_header = $self->[HEADER];
128 3 ▶ $self->[HEADER] = $shared . q{;} . ( $self->[HEADER] || EMPTY_STRING );
Condition50%1/2
$self->[14] || ''
l!l
03
MC/DC0%0/1
$self->[14] || ''
$self->[14]
Truth table50%1/2
A
A= $self->[14]
Aresult
11
00
129 }
130  
131 249 my %popt = ( %{ $opt }, cache_id => $cache_id, as_is => $opt->{as_is} );
132 249 my $parsed = $self->_parse( $tmp, \%popt );
133 249 my $CODE = $self->cache->populate( $cache_id, $parsed, $opt->{chkmt} );
134 248 ▶ $self->[HEADER] = $restore_header if $shead;
Branch: 1 branch
BranchTrueFalseTotal
if $shead 3 245 248
135 248 return $CODE;
136 }
137  
138 2 ▶sub _call_filters {
Subroutine _call_filters: called CC=3 SCAR=12.0
139 2 my($self, $oref, @filters) = @_;
140 2 my $fname = $self->[FILENAME];
141  
142 2 APPLY_FILTERS: foreach my $filter ( @filters ) {
143 3 my $fref = DUMMY_CLASS->can( 'filter_' . $filter );
144 3 ▶ if ( ! $fref ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fref) 0 3 3
145 0 ${$oref} .= "\n[ filter warning ] Can not apply undefined filter"
146 . " $filter to $fname\n";
147 0 next;
148 }
149 3 $fref->( $self, $oref );
150 }
151  
152 2 return;
153 }
154  
155 250 ▶sub _wrap_compile {
Subroutine _wrap_compile: called CC=9 SCAR=23.9
156 250 my $self = shift;
157 250 ▶ my $parsed = shift or fatal('tts.base.compiler._wrap_compile.parsed');
Branch: 1 branch
BranchTrueFalseTotal
my $parsed = shift() or fatal("tts.base.compiler._wrap_compile.parsed") 250 0 250
158 250 ▶ LOG( CACHE_ID => $self->cache->id ) if $self->[WARN_IDS] && $self->cache->id;
Branch: 1 branch
BranchTrueFalseTotal
if $self->[17] and $self->cache->id 0 250 250
Condition33%1/3
$self->[17] and $self->cache->id
!ll&&!rl&&r
25000
MC/DC0%0/2
$self->[17] and $self->cache->id
$self->[17] $self->cache->id
Truth table33%1/3
A and B
A= $self->[17]B= $self->cache->id
ABresult
0X0
100
111
159 250 ▶ LOG( COMPILER => $self->[SAFE] ? 'Safe' : 'Normal' ) if DEBUG;
Branch: 2 branches
BranchTrueFalseTotal
if DEBUG() 0 250 250
$self->[13] ? : 0 0 0
160 250 my($CODE, $error);
161  
162 250 ▶ my $compiler = $self->[SAFE] ? COMPILER_SAFE : COMPILER;
Branch: 1 branch
BranchTrueFalseTotal
$self->[13] ? : 1 249 250
163  
164 250 $CODE = $compiler->compile( $parsed );
165  
166 250 ▶ if( $error = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error = $@) 1 249 250
167 1 my $error2;
168 1 ▶ $error .= $error2 if $error2;
Branch: 1 branch
BranchTrueFalseTotal
if $error2 0 1 1
169 }
170  
171 250 return $CODE, $error;
172 }
173  
174 140 ▶sub _mini_compiler {
Subroutine _mini_compiler: called CC=8 SCAR=21.7
175 # little dumb compiler for internal templates
176 140 my $self = shift;
177 140 ▶ my $template = shift || fatal('tts.base.compiler._mini_compiler.notmp');
Condition33%1/3
shift() || fatal('tts.base.compiler._mini_compiler.notmp')
l!l&&r!l&&!r
14000
MC/DC0%0/2
shift() || fatal('tts.base.compiler._mini_compiler.notmp')
shift() fatal('tts.base.compiler._mini_compiler.notmp')
Truth table33%1/3
A || B
A= shift()B= fatal('tts.base.compiler._mini_compiler.notmp')
ABresult
1X1
011
000
178 140 ▶ my $param = shift || fatal('tts.base.compiler._mini_compiler.noparam');
Condition33%1/3
shift() || fatal('tts.base.compiler._mini_compiler.noparam')
l!l&&r!l&&!r
14000
MC/DC0%0/2
shift() || fatal('tts.base.compiler._mini_compiler.noparam')
shift() fatal('tts.base.compiler._mini_compiler.noparam')
Truth table33%1/3
A || B
A= shift()B= fatal('tts.base.compiler._mini_compiler.noparam')
ABresult
1X1
011
000
179 140 ▶ my $opt = shift || {};
Condition100%2/2
shift() || {}
l!l
1373
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
180  
181 140 ▶ fatal('tts.base.compiler._mini_compiler.opt') if ref $opt ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $opt ne "HASH" 0 140 140
182 140 ▶ fatal('tts.base.compiler._mini_compiler.param') if ref $param ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $param ne "HASH" 0 140 140
183  
184 140 foreach my $var ( keys %{ $param } ) {
185 449 my $str = $param->{$var};
186 449 $template =~ s{<%\Q$var\E%>}{$str}xmsg;
187 }
188  
189 140 ▶ $template =~ s{\s+}{ }xmsg if $opt->{flatten}; # remove extra spaces
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"flatten"} 137 3 140
190 140 return $template;
191 }
192  
193 32 1;
194  
195 __END__
↑ Summary Simple/IO.pm »