Plex/Internal.pm

filter: Statementstmt 79.7% 205 / 257 Branchbran 42.0% 21 / 50 Conditioncond 50.0% 9 / 18 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 84.9% 62 / 73 Podpod 0.0% 0 / 5 totaltotal 72.4% 299 / 413 CC 48 SCAR 9.1
CC
48
Cov
72%
new
14.4
_prepare_template
12.4
jmap
6.0
CRAP 2.5
« Plex.pm ↑ Summary
1 package Template::Plex::Internal;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 ▶use Template::Plex;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use List::Util qw<min max>;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 #use Symbol qw<delete_package>;
10 3 ▶use Carp qw<carp croak>;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 ▶use feature qw<say state refaliasing lexical_subs>;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶no warnings "experimental";
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 #use File::Basename qw<dirname basename>;
16 3 ▶use File::Spec::Functions qw<catfile>;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use File::Basename qw<dirname>;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20  
21 #our %EXPORT_TAGS = ( 'all' => [ qw( plex plx block pl plex_clear jmap) ] );
22  
23 2 our @EXPORT_OK = qw<block pl jmap>;# @{ $EXPORT_TAGS{'all'} } );
24  
25  
26 2 my $Include=qr|\@\{\s*\[\s*include\s*\(\s*(.*?)\s*\)\s*\] \s* \}|x;
27 2 my $Init=qr|\@\{\s*\[\s*init\s*\{(?:.*?)\}\s*\] \s* \}|smx;
28  
29  
30 sub new; #forward declare new;
31  
32 sub lexical{
33 20 ▶ my $href=shift;
Subroutine lexical: called CC=5 SCAR=17.0
Pod undocumented
34 20 ▶ croak "NEED A HASH REF " unless ref $href eq "HASH" or !defined $href;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $href eq "HASH" or not defined $href 0 19 19
Condition33%1/3
ref $href eq "HASH" or not defined $href
l!l&&r!l&&!r
1900
MC/DC0%0/2
ref $href eq "HASH" or not defined $href
ref $href eq "HASH" not defined $href
Truth table33%1/3
A or not(B)
A= ref $href eq "HASH"B= not defined $href
ABresult
1X1
011
000
35 20 ▶ $href//={};
Condition50%1/2
$href //= {}
l!l
190
MC/DC0%0/1
$href //= {}
$href
Truth table50%1/2
A
A= $href
Aresult
11
00
36 20 \my %fields=$href;
37  
38 20 my $string="";
39 20 for my $k (keys %fields){
40 12 $string.= "\\my \$$k=\\\$fields{$k};\n";
41 }
42 20 $string;
43 }
44  
45 sub bootstrap{
46 20 ▶ my $plex=shift;
Subroutine bootstrap: called CC=5 SCAR=16.3
47 20 \my $_data_=\shift;
48 20 my $href=shift;
49 20 my %opts=@_;
50  
51 20 ▶ $href//={};
Condition50%1/2
$href //= {}
l!l
190
MC/DC0%0/1
$href //= {}
$href
Truth table50%1/2
A
A= $href
Aresult
11
00
52 20 \my %fields=$href;
53  
54 20 my $out="package $opts{package} {
55 use Template::Plex::Internal qw<pl block jmap>;
56 no warnings qw<syntax>;
57 ";
58  
59 20 $out.='my $self=$plex;
60 ';
61  
62 20 $out.= ' \my %fields=$href;
63 ';
64 20 ▶$out.=' my %options=%opts;
Branch: 1 branch
BranchTrueFalseTotal
if keys %opts 19 0 19
65 ' if keys %opts;
66 20 for($opts{use}->@*){
67 1 $out.="use $_;\n";
68 }
69  
70 20 ▶$out.=lexical($href) unless $opts{no_alias}; #add aliased variables from hash
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"no_alias"} 19 0 19
71 20 $out.='
72 my %cache; #Stores code refs using caller as keys
73  
74 sub clear {
75 %cache=();
76 }
77  
78 sub skip{
79 goto _PLEX_SKIP;
80 }
81  
82 $plex->[Template::Plex::skip_]=\&skip;
83  
84  
85 my sub init :prototype(&){
86 $self->_init(@_);
87 }
88  
89 sub slot {
90 $self->slot(@_);
91 }
92 sub fill_slot {
93 $self->fill_slot(@_);
94 }
95 sub inherit {
96 $self->inherit(@_);
97 }
98  
99 sub load {
100 $self->load(@_);
101 }
102  
103 sub cache {
104  
105 my ($id, $path, $var, @opts)=@_;
106 #we want to cache based on the caller
107 $id=$path.join "", caller;
108 #unshift @_, $id;
109 $self->cache($id,$path, $var,@opts);
110 }
111  
112 sub immediate {
113 my ($id, $path, $var, @opts)=@_;
114 #we want to cache based on the caller
115 $id=$path.join "", caller;
116 my $template=$self->cache($id, $path,$var, @opts);
117 if($template){
118 return $template->render;
119 }
120 "";
121 }
122  
123  
124 sub {
125 no warnings \'uninitialized\';
126 no strict;
127 #my $plex=shift;
128 my $self=shift;
129  
130 \\my %fields=shift//\\%fields;
131  
132  
133 ##__START
134 return $self->prefix.
135 qq
136 {'.
137 $_data_
138 . '}
139 .$self->postfix;
140  
141 _PLEX_SKIP:
142 "";
143  
144 }
145 };';
146  
147 };
148  
149 # First argument the template string/text. This is any valid perl code
150 # Second argument is a hash ref to default or base level fields
151 # returns a code reference which when called renders the template with the values
152 sub _prepare_template{
153 3 ▶ no warnings qw<syntax>;
Subroutine BEGIN: called CC=1 SCAR=0.0
154 20 ▶ my ($plex, undef, $href, %opts)=@_;
Subroutine _prepare_template: called CC=9 SCAR=25.2
155 20 ▶ $href//={};
Condition50%1/2
$href //= {}
l!l
190
MC/DC0%0/1
$href //= {}
$href
Truth table50%1/2
A
A= $href
Aresult
11
00
156 20 \my %fields=$href;
157 20 \my %meta=\%opts;
158  
159 #$plex now variable is now of base class
160 20 ▶ $plex=($opts{base}//"Template::Plex")->new($plex);
Condition100%2/2
$opts{"base"} // "Template::Plex"
l!l
118
MC/DC100%1/1
$opts{"base"} // "Template::Plex"
$opts{"base"}
Truth table100%2/2
A
A= $opts{"base"}
Aresult
11
00
161  
162 20 $plex->[Template::Plex::meta_]=\%opts;
163 20 $plex->[Template::Plex::args_]=$href;
164  
165 20 my $prog=&Template::Plex::Internal::bootstrap;
166 3 ▶ my $ref=eval $prog;
Subroutine BEGIN: called CC=1 SCAR=0.5
Subroutine BEGIN: called CC=1 SCAR=0.5
Subroutine BEGIN: called CC=1 SCAR=0.5
Subroutine BEGIN: called CC=1 SCAR=0.5
Subroutine BEGIN: called CC=1 SCAR=0.5
Subroutine BEGIN: called CC=1 SCAR=0.5
Subroutine BEGIN: called CC=1 SCAR=0.5
Subroutine BEGIN: called CC=1 SCAR=0.5
Subroutine BEGIN: called CC=1 SCAR=0.5
Subroutine BEGIN: called CC=1 SCAR=0.5
Subroutine BEGIN: called CC=1 SCAR=0.5
Subroutine BEGIN: called CC=1 SCAR=0.5
Subroutine BEGIN: called CC=1 SCAR=0.5
Subroutine BEGIN: called CC=1 SCAR=0.5
Subroutine BEGIN: called CC=1 SCAR=0.5
Subroutine BEGIN: called CC=1 SCAR=0.5
Subroutine cache: called CC=1 SCAR=0.5
Subroutine cache: called CC=1 SCAR=0.5
Subroutine cache: called CC=1 SCAR=0.5
Subroutine cache: called CC=1 SCAR=0.5
Subroutine clear: called CC=1 SCAR=0.5
Subroutine clear: called CC=1 SCAR=0.5
Subroutine clear: called CC=1 SCAR=0.5
Subroutine clear: called CC=1 SCAR=0.5
Subroutine fill_slot: called CC=1 SCAR=0.5
Subroutine fill_slot: called CC=1 SCAR=0.5
Subroutine fill_slot: called CC=1 SCAR=0.5
Subroutine fill_slot: called CC=1 SCAR=0.5
Subroutine immediate: called CC=2 SCAR=7.8
Subroutine immediate: called CC=2 SCAR=7.8
Subroutine immediate: not called CC=2 SCAR=7.8
Subroutine immediate: not called CC=2 SCAR=7.8
Subroutine inherit: called CC=1 SCAR=0.5
Subroutine inherit: called CC=1 SCAR=0.5
Subroutine inherit: not called CC=1 SCAR=0.5
Subroutine inherit: not called CC=1 SCAR=0.5
Subroutine load: called CC=1 SCAR=0.5
Subroutine load: not called CC=1 SCAR=0.5
Subroutine load: not called CC=1 SCAR=0.5
Subroutine load: not called CC=1 SCAR=0.5
Subroutine skip: called CC=1 SCAR=0.5
Subroutine skip: called CC=1 SCAR=0.5
Subroutine skip: called CC=1 SCAR=0.5
Subroutine skip: called CC=1 SCAR=0.5
Subroutine slot: called CC=1 SCAR=0.5
Subroutine slot: called CC=1 SCAR=0.5
Subroutine slot: called CC=1 SCAR=0.5
Subroutine slot: called CC=1 SCAR=0.5
Subroutine BEGIN: not called CC=1 SCAR=0.5
Subroutine BEGIN: not called CC=1 SCAR=0.5
Subroutine BEGIN: not called CC=1 SCAR=0.5
Branch: 4 branches
BranchTrueFalseTotal
if ($template) 1 0 1
if ($template) 1 0 1
if ($template) 0 0 0
if ($template) 0 0 0
167 20 ▶ if($@ and !$ref){
Branch: 1 branch
BranchTrueFalseTotal
if ($@ and not $ref) 0 19 19
Condition33%1/3
$@ and not $ref
!ll&&!rl&&r
1900
MC/DC0%0/2
$@ and not $ref
$@ not $ref
Truth table33%1/3
A and not(B)
A= $@B= not $ref
ABresult
0X0
100
111
168 1 my $error=$@;
169  
170 1 my $line=1;
171 1 my $start;
172 #my @lines=map { $start= $line if /##__START/;$line++ . $_."\n"; } split "\n", $prog;
173 1 ▶ my @lines=map { $start = $line if /##__START/; $line++;$_."\n" } split "\n", $prog;
Branch: 1 branch
BranchTrueFalseTotal
if /##__START/ 0 0 0
174 1 $start+=2;
175 1 my @error_lines;
176  
177 1 $error=~s/line (\d+)/do{push @error_lines, $1;"line ".($1-$start)}/eg;
178 1 $error=~s/\(eval (\d+)\)/"(".$opts{file}.")"/eg;
179 1 my $min=min @error_lines;
180 1 my $max=max @error_lines;
181 1 ▶ $min-=5; $min=$start if $min<$start;
Branch: 1 branch
BranchTrueFalseTotal
if $min < $start 0 0 0
182 0 ▶ $max+=5; $max=$#lines-7 if $max>($#lines-7);
Branch: 1 branch
BranchTrueFalseTotal
if $max > $#lines - 7 0 0 0
183 0 my $counter=$min-$start+1;
184 0 my $out=$error;
185 0 for ($min..$max){
186 0 $out.=$counter++." ".$lines[$_];
187 }
188 0 croak $out;
189 }
190 19 $plex->[Template::Plex::sub_]=$ref;
191 19 $plex;
192 }
193  
194 #a little helper to allow 'including' templates into each other
195 sub _munge {
196 1 ▶ my ($input, %options)=@_;
Subroutine _munge: called CC=3 SCAR=12.7
197  
198 #test for literals
199 1 my $path;
200 1 ▶ if($input =~ /^"(.*)"$/){
Branch: 2 branches
BranchTrueFalseTotal
if ($input =~ /^"(.*)"$/) { } 0 0 0
elsif ($input =~ /^'(.*)'$/) { } 0 0 0
201 #literal
202 1 $path=$1;
203 }
204 elsif($input =~ /^'(.*)'$/){
205 #literal
206 1 $path=$1;
207 }
208 else {
209 #not supported?
210 #
211 }
212 1 Template::Plex::Internal->new(\&_prepare_template,$path,"",%options);
213 }
214  
215 sub _subst_inject {
216 21 ▶ \my $buffer=\(shift);
Subroutine _subst_inject: called CC=2 SCAR=7.1
217 19 ▶ while($buffer=~s|$Include|_munge($1, @_)|e){
Branch: 1 branch
BranchTrueFalseTotal
if ($buffer =~ s/$Include/_munge($1, @_);/e) 0 19 19
218 #TODO: Possible point for diagnostics?
219 };
220 }
221  
222 sub _block_fix {
223 #remove any new line immediately after a ]} pair
224 20 ▶ \my $buffer=\(shift);
Subroutine _block_fix: called CC=1 SCAR=0.0
225 #$buffer=~s/^\]\}$/]}/gms;
226
227 20 $buffer=~s/^(\@\{\[.*?\]\})\n/$1/gms;
228 ##############################################
229 # while($buffer=~s/^\]\}\n/]}/gs){ #
230 # } #
231 # while($buffer=~s/^(@\{\[.*?\]\})\n/$1/gs){ #
232 # } #
233 ##############################################
234  
235 }
236  
237 sub _init_fix{
238 20 ▶ \my $buffer=\$_[0];
Subroutine _init_fix: called CC=2 SCAR=6.9
239 #Look for an init block
240 #unless($buffer=~/\@\[\{\s*init\s*\{
241 20 ▶ unless($buffer=~$Init){
Branch: 1 branch
BranchTrueFalseTotal
unless ($buffer =~ /$Init/msx) 6 13 19
242 #carp __PACKAGE__." no init block detected. Adding dummy";
243 6 $buffer="\@{[init{}]}".$buffer;
244 }
245 }
246  
247 2 my $prepare=\&_prepare_template;
248  
249 2 my %cache;
250  
251  
252 sub clear {
253 0 ▶ %cache=();
Subroutine clear: called CC=1 SCAR=6.9
Pod undocumented
254 }
255  
256  
257 sub block :prototype(&) {
258 1 ▶ $_[0]->();
Subroutine block: called CC=1 SCAR=0.0
Pod undocumented
259 1 return "";
260 }
261 2 *pl=\*block;
262  
263  
264  
265 sub new{
266 19 ▶ my $plex=bless [], shift;
Subroutine new: called CC=12 SCAR=26.7
Pod undocumented
267 19 my ($prepare, $path, $args, %options)=@_;
268 19 my $root=$options{root};
269 #croak "plex: even number of arguments required" if (@_-1)%2;
270 19 ▶ croak "Template::Plex::Internal first argument must be defined" unless defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 1 19 20
271 #croak "plex: at least 2 arguments needed" if ((@_-1) < 2);
272  
273 19 my $data=do {
274 19 local $/=undef;
275 19 ▶ if(ref($path) eq "GLOB"){
Branch: 2 branches
BranchTrueFalseTotal
if (ref $path eq 'GLOB') { } 0 19 19
elsif (ref $path eq 'ARRAY') { } 12 7 19
276 #file handle
277 0 $options{file}="$path";
278 0 <$path>;
279 }
280 elsif(ref($path) eq "ARRAY"){
281 #process as inline template
282 12 $options{file}="$path";
283 12 join "", @$path;
284 }
285 else{
286 #Assume a path
287 #Prepend the root if present
288 7 $options{file}=$path;
289 7 ▶ $path=catfile $root, $path if $root;
Branch: 1 branch
BranchTrueFalseTotal
if $root 7 0 7
290 7 my $fh;
291 7 ▶ if(open $fh, "<", $path){
Branch: 1 branch
BranchTrueFalseTotal
if (open $fh, '<', $path) { } 7 0 7
292 <$fh>
293 7 }
294 else {
295 0 croak "Could not open file: $path $!";
296 0 "";
297 }
298 }
299 };
300  
301 19 ▶ $args//={}; #set to empty hash if not defined
Condition100%2/2
$args //= {}
l!l
181
MC/DC100%1/1
$args //= {}
$args
Truth table100%2/2
A
A= $args
Aresult
11
00
302
303 19 ▶ chomp $data unless $options{no_eof_chomp};
Branch: 1 branch
BranchTrueFalseTotal
unless $options{"no_eof_chomp"} 19 0 19
304 #Perform inject substitution
305 19 ▶ _subst_inject($data, root=>$root) unless $options{no_include};
Branch: 1 branch
BranchTrueFalseTotal
unless $options{"no_include"} 19 0 19
306 #Perform suppurfluous EOL removal
307 19 ▶ _block_fix($data) unless $options{no_block_fix};
Branch: 1 branch
BranchTrueFalseTotal
unless $options{"no_block_fix"} 19 0 19
308 19 ▶ _init_fix($data) unless $options{no_init_fix};
Branch: 1 branch
BranchTrueFalseTotal
unless $options{"no_init_fix"} 19 0 19
309 19 ▶ if($args){
Branch: 1 branch
BranchTrueFalseTotal
if ($args) { } 19 0 19
310 #Only call this from top level call
311 #Returns the render sub
312  
313 19 state $package=0;
314 19 $package++;
315 19 $options{package}="Template::Plex::temp".$package; #force a unique package if non specified
316 19 $prepare->($plex, $data, $args, %options); #Prepare in the correct scope
317 }
318 else {
319 0 $data;
320 }
321 }
322  
323  
324 #Join map
325 sub jmap :prototype(&$@){
326 0 ▶ my ($sub,$delimiter)=(shift,shift); #block is first
Subroutine jmap: not called CC=2 SCAR=17.9
Pod undocumented
327 0 ▶ $delimiter//=""; #delimiter is whats left
Condition0%0/2
$delimiter //= ""
l!l
00
MC/DC0%0/1
$delimiter //= ""
$delimiter
Truth table0%0/2
A
A= $delimiter
Aresult
11
00
328 0 join $delimiter, map &$sub, @_;
329 }
330  
331  
332  
333 2 1;
« Plex.pm ↑ Summary