Simple/Base/Include.pm

filter: Statementstmt 89.7% 105 / 117 Branchbran 79.1% 38 / 48 Conditioncond 55.5% 5 / 9 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 94.1% 16 / 17 Podpod 100% 1 / 1 totaltotal 84.2% 166 / 197 CC 30 SCAR 19.5
CC
30
Cov
85%
include
14.8
_interpolate_share_setup
6.2
_interpolate
6.0
CRAP 7.0
« Simple/Tokenizer.pm ↑ Summary Simple/Base/Examine.pm »
1 ## no critic (ProhibitUnusedPrivateSubroutines)
2 package Text::Template::Simple::Base::Include;
3 32 $Text::Template::Simple::Base::Include::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 32 ▶use constant E_IN_MONOLITH =>
Subroutine BEGIN: called CC=1 SCAR=0.0
10 'qq~%s Interpolated includes don\'t work under monolith option. '
11 .'Please disable monolith and use the \'SHARE\' directive in the include '
12 32 .'command: %s~';
13 32 ▶use constant E_IN_DIR => q(q~%s '%s' is a directory~);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 32 ▶use constant E_IN_SLURP => 'q~%s %s~';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 32 ▶use constant TYPE_MAP => qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
16 @ ARRAY
17 % HASH
18 * GLOB
19 \ REFERENCE
20 32 );
21  
22 124 ▶sub _include_no_monolith {
Subroutine _include_no_monolith: called CC=1 SCAR=0.0
23 # no monolith eh?
24 124 my($self, $type, $file, $opt) = @_;
25  
26 124 my $rv = $self->_mini_compiler(
27 $self->_internal('no_monolith') => {
28 OBJECT => $self->[FAKER_SELF],
29 FILE => escape(q{~} => $file),
30 TYPE => escape(q{~} => $type),
31 } => {
32 flatten => 1,
33 }
34 );
35 124 ++$self->[NEEDS_OBJECT];
36 124 return $rv;
37 }
38  
39 9 ▶sub _include_static {
Subroutine _include_static: called CC=2 SCAR=7.2
40 9 my($self, $file, $text, $err, $opt) = @_;
41 9 ▶ return $self->[MONOLITH]
Branch: 1 branch
BranchTrueFalseTotal
$self->[7] ? : 0 9 9
42 ? sprintf('q~%s~;', escape(q{~} => $text))
43 : $self->_include_no_monolith( T_STATIC, $file, $opt )
44 ;
45 }
46  
47 118 ▶sub _include_dynamic {
Subroutine _include_dynamic: called CC=5 SCAR=16.1
48 118 my($self, $file, $text, $err, $opt) = @_;
49 118 my $rv = EMPTY_STRING;
50  
51 118 ++$self->[INSIDE_INCLUDE];
52 118 ▶ $self->[COUNTER_INCLUDE] ||= {};
Condition100%2/2
$self->[21] ||= {}
l!l
5068
MC/DC100%1/1
$self->[21] ||= {}
$self->[21]
Truth table100%2/2
A
A= $self->[21]
Aresult
11
00
53  
54 # ++$self->[COUNTER_INCLUDE]{ $file } if $self->[TYPE_FILE] eq $file;
55  
56 118 ▶ if ( ++$self->[COUNTER_INCLUDE]{ $file } >= MAX_RECURSION ) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$self->[21]{$file} >= 50) { } 1 117 118
57 # failsafe
58 1 $self->[DEEP_RECURSION] = 1;
59 1 ▶ LOG( DEEP_RECURSION => $file ) if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if DEBUG() 0 1 1
60 1 my $w = L( warning => 'tts.base.include.dynamic.recursion',
61 $err, MAX_RECURSION, $file );
62 1 $rv .= sprintf 'q~%s~', escape( q{~} => $w );
63 }
64 else {
65 # local stuff is for file name access through $0 in templates
66 117 ▶ $rv .= $self->[MONOLITH]
Branch: 1 branch
BranchTrueFalseTotal
$self->[7] ? : 2 115 117
67 ? $self->_include_dynamic_monolith( $file, $text )
68 : $self->_include_no_monolith( T_DYNAMIC, $file, $opt )
69 ;
70 }
71  
72 118 --$self->[INSIDE_INCLUDE]; # critical: always adjust this
73 118 return $rv;
74 }
75  
76 2 ▶sub _include_dynamic_monolith {
Subroutine _include_dynamic_monolith: called CC=1 SCAR=0.0
77 2 my($self,$file, $text) = @_;
78 2 my $old = $self->[FILENAME];
79 2 $self->[FILENAME] = $file;
80 2 my $result = $self->_parse( $text );
81 2 $self->[FILENAME] = $old;
82 2 return $result;
83 }
84  
85 137 ▶sub include {
Subroutine include: called CC=14 SCAR=26.9
Pod documented
86 137 my $self = shift;
87 137 ▶ my $type = shift || 0;
Condition50%1/2
shift() || 0
l!l
1370
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
88 137 my $file = shift;
89 137 my $opt = shift;
90 137 ▶ my $is_static = T_STATIC == $type ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
10 == $type ? : 11 126 137
91 137 ▶ my $is_dynamic = T_DYNAMIC == $type ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
9 == $type ? : 126 11 137
92 137 ▶ my $known = $is_static || $is_dynamic;
Condition66%2/3
$is_static || $is_dynamic
l!l&&r!l&&!r
111260
MC/DC0%0/2
$is_static || $is_dynamic
$is_static $is_dynamic
Truth table66%2/3
A || B
A= $is_staticB= $is_dynamic
ABresult
1X1
011
000
93  
94 137 ▶ fatal('tts.base.include._include.unknown', $type) if not $known;
Branch: 1 branch
BranchTrueFalseTotal
unless $known 0 137 137
95  
96 137 $file = trim $file;
97  
98 137 my $err = $self->_include_error( $type );
99 137 my $exists = $self->io->file_exists( $file );
100 137 my $interpolate;
101  
102 137 ▶ if ( $exists ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exists) { } 127 10 137
103 127 $file = $exists; # file path correction
104 }
105 else {
106 10 $interpolate = 1; # just guessing ...
107 10 ▶ return sprintf E_IN_MONOLITH, $err, $file if $self->[MONOLITH];
Branch: 1 branch
BranchTrueFalseTotal
if $self->[7] 1 9 10
108 }
109  
110 136 ▶ if ( $self->io->is_dir( $file ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->io->is_dir($file)) 0 136 136
111 0 return sprintf E_IN_DIR, $err, escape(q{~} => $file);
112 }
113  
114 136 ▶ $self->_debug_include_type( $file, $type ) if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if DEBUG() 0 136 136
115  
116 136 ▶ if ( $interpolate ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($interpolate) 9 127 136
117 9 my $rv = $self->_interpolate( $file, $type );
118 9 $self->[NEEDS_OBJECT]++;
119 9 ▶ LOG(INTERPOLATE_INC => "TYPE: $type; DATA: $file; RV: $rv") if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if DEBUG() 0 9 9
120 9 return $rv;
121 }
122  
123 127 my $text = eval { $self->io->slurp($file); };
124 127 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 127 127
125 0 return sprintf E_IN_SLURP, $err, $@;
126 }
127  
128 127 ▶ my $meth = '_include_' . ($is_dynamic ? 'dynamic' : 'static');
Branch: 1 branch
BranchTrueFalseTotal
$is_dynamic ? : 118 9 127
129 127 return $self->$meth( $file, $text, $err, $opt );
130 }
131  
132 0 ▶sub _debug_include_type {
Subroutine _debug_include_type: not called CC=1 SCAR=6.9
133 0 my($self, $file, $type) = @_;
134 0 require Text::Template::Simple::Tokenizer;
135 my $toke = Text::Template::Simple::Tokenizer->new(
136 0 @{ $self->[DELIMITERS] },
137 $self->[PRE_CHOMP],
138 $self->[POST_CHOMP]
139 );
140 0 LOG( INCLUDE => $toke->_visualize_tid($type) . " => '$file'" );
141 0 return;
142 }
143  
144 9 ▶sub _interpolate {
Subroutine _interpolate: called CC=6 SCAR=17.9
145 9 my $self = shift;
146 9 my $file = shift;
147 9 my $type = shift;
148 9 my $etitle = $self->_include_error($type);
149  
150 # so that, you can pass parameters, apply filters etc.
151 9 my %inc = (INCLUDE => map { trim $_ } split RE_PIPE_SPLIT, $file );
152  
153 9 ▶ if ( $self->io->file_exists( $inc{INCLUDE} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->io->file_exists($inc{"INCLUDE"})) 5 4 9
154 # well... constantly working around :p
155 5 $inc{INCLUDE} = qq{'$inc{INCLUDE}'};
156 }
157  
158 # die "You can not pass parameters to static includes"
159 # if $inc{PARAM} && T_STATIC == $type;
160  
161  
162 9 ▶ $self->_interpolate_share_setup( \%inc ) if $inc{SHARE};
Branch: 1 branch
BranchTrueFalseTotal
if $inc{"SHARE"} 3 6 9
163  
164 9 ▶ my $share = $inc{SHARE} ? sprintf(q{'%s', %s}, ($inc{SHARE}) x 2) : 'undef';
Branch: 1 branch
BranchTrueFalseTotal
$inc{'SHARE'} ? : 3 6 9
165 9 ▶ my $filter = $inc{FILTER} ? escape( q{'} => $inc{FILTER} ) : EMPTY_STRING;
Branch: 1 branch
BranchTrueFalseTotal
$inc{'FILTER'} ? : 1 8 9
166  
167 return
168 $self->_mini_compiler(
169 $self->_internal('sub_include') => {
170 OBJECT => $self->[FAKER_SELF],
171 INCLUDE => escape( q{'} => $inc{INCLUDE} ),
172 ERROR_TITLE => escape( q{'} => $etitle ),
173 TYPE => $type,
174 9 ▶ PARAMS => $inc{PARAM} ? qq{[$inc{PARAM}]} : 'undef',
Branch: 1 branch
BranchTrueFalseTotal
$inc{'PARAM'} ? : 2 7 9
175 FILTER => $filter,
176 SHARE => $share,
177 } => {
178 flatten => 1,
179 }
180 );
181 }
182  
183 3 ▶sub _interpolate_share_setup {
Subroutine _interpolate_share_setup: called CC=5 SCAR=18.2
184 3 my($self, $inc) = @_;
185 3 my @vars = map { trim $_ } split RE_FILTER_SPLIT, $inc->{SHARE};
186 3 my %type = TYPE_MAP;
187 3 my @buf;
188 3 foreach my $var ( @vars ) {
189 5 ▶ if ( $var !~ m{ \A \$ }xms ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($var =~ / \A \$ /msx) 0 5 5
190 0 my($char) = $var =~ m{ \A (.) }xms;
191 0 ▶ my $type_name = $type{ $char } || '<UNKNOWN>';
Condition0%0/2
$type{$char} || '<UNKNOWN>'
l!l
00
MC/DC0%0/1
$type{$char} || '<UNKNOWN>'
$type{$char}
Truth table0%0/2
A
A= $type{$char}
Aresult
11
00
192 0 fatal('tts.base.include._interpolate.bogus_share', $type_name, $var);
193 }
194 5 $var =~ tr/;//d;
195 5 ▶ if ( $var =~ m{ [^a-zA-Z0-9_\$] }xms ) { ## no critic (ProhibitEnumeratedClasses)
Branch: 1 branch
BranchTrueFalseTotal
if ($var =~ / [^a-zA-Z0-9_\$] /msx) 0 5 5
196 0 fatal('tts.base.include._interpolate.bogus_share_notbare', $var);
197 }
198 5 push @buf, $var;
199 }
200 3 $inc->{SHARE} = join q{,}, @buf;
201 3 return;
202 }
203  
204 279 ▶sub _include_error {
Subroutine _include_error: called CC=3 SCAR=11.1
205 279 my($self, $type) = @_;
206 279 ▶ my $val = T_DYNAMIC == $type ? 'dynamic'
Branch: 2 branches
BranchTrueFalseTotal
9 == $type ? : 266 13 279
10 == $type ? : 13 0 13
207 : T_STATIC == $type ? 'static'
208 : 'unknown'
209 ;
210 279 return sprintf '[ %s include error ]', $val;
211 }
212  
213 32 1;
214  
215 __END__
« Simple/Tokenizer.pm ↑ Summary Simple/Base/Examine.pm »