Compiled.pm

filter: Statementstmt 88.5% 132 / 149 Branchbran 67.7% 42 / 62 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 80.0% 20 / 25 Podpod 100% 2 / 2 totaltotal 81.4% 202 / 248 CC 34 SCAR 28.6
CC
34
Cov
82%
_build_code_and_env
32.1
print
12.0
_lookup_escaping
6.0
CRAP 17.5
↑ Summary Compiled/Utils.pm »
1 2 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Template::Compiled;
6  
7 2 our $AUTHORITY = 'cpan:TOBYINK';
8 2 our $VERSION = '0.002';
9  
10 2 ▶use Eval::TypeTiny qw( eval_closure );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Types::Standard qw( -types slurpy Join );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Type::Params 1.002000 qw( compile_named );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Carp qw( croak confess );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use B qw( perlstring );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 ▶use constant PERL_NATIVE_ALIASES => ($] >= 5.02200);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 use overload
22 0 ▶ '""' => sub { $_[0]->template },
Subroutine __ANON__: not called CC=1 SCAR=6.9
23 0 ▶ 'bool' => sub { !!1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
24 0 ▶ '&{}' => sub { $_[0]->sub },
Subroutine __ANON__: not called CC=1 SCAR=6.9
25 2 fallback => 1,
26 2 ▶ ;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 2 has template => (
29 is => 'ro',
30 isa => Str->plus_coercions( Join["\n"] ),
31 required => !!1,
32 coerce => !!1,
33 );
34  
35 2 has signature => (
36 is => 'ro',
37 isa => Maybe[ArrayRef],
38 predicate => !!1,
39 required => !!0,
40 );
41  
42 has delimiters => (
43 is => 'lazy',
44 isa => Tuple[Str, Str],
45 2 ▶ builder => sub { [qw/ <? ?> /] },
Subroutine __ANON__: called CC=1 SCAR=0.0
46 2 );
47  
48 2 has escape => (
49 is => 'ro',
50 isa => CodeRef->plus_coercions(Str, \&_lookup_escaping),
51 required => !!0,
52 predicate => !!1,
53 coerce => !!1,
54 );
55  
56 has trim => (
57 is => 'lazy',
58 isa => Bool,
59 3 ▶ builder => sub { !!0 },
Subroutine _build_trim: called CC=1 SCAR=0.0
60 2 );
61  
62 has outdent => (
63 is => 'lazy',
64 isa => Int,
65 4 ▶ builder => sub { 0 },
Subroutine _build_outdent: called CC=1 SCAR=0.0
66 2 );
67  
68 2 has post_process => (
69 is => 'ro',
70 isa => CodeRef,
71 required => !!0,
72 predicate => !!1,
73 );
74  
75 has utils_package => (
76 is => 'lazy',
77 isa => Str,
78 5 ▶ builder => sub { __PACKAGE__ . '::Utils' },
Subroutine _build_utils_package: called CC=1 SCAR=0.0
79 2 );
80  
81 has 'sub' => (
82 is => 'lazy',
83 isa => CodeRef,
84 builder => sub {
85 5 ▶ my ($code, $env) = $_[0]->_build_code_and_env;
Subroutine _build_sub: called CC=1 SCAR=0.0
86 5 eval_closure(
87 source => $code,
88 environment => $env,
89 description => 'template prelude',
90 );
91 },
92 2 );
93  
94 sub print {
95 0 ▶ my $self = shift;
Subroutine print: not called CC=3 SCAR=24.8
Pod documented
96 0 ▶ my $fh = FileHandle->check($_[0]) ? shift() : undef;
Branch: 1 branch
BranchTrueFalseTotal
&FileHandle()->check($_[0]) ? : 0 0 0
97
98 0 ▶ defined($fh)
Branch: 1 branch
BranchTrueFalseTotal
defined $fh ? : 0 0 0
99 ? $fh->print( $self->render(@_) )
100 : CORE::print( $self->render(@_) );
101 }
102  
103 sub render {
104 6 ▶ my $self = shift;
Subroutine render: called CC=1 SCAR=0.0
Pod documented
105 6 $self->sub->(@_);
106 }
107  
108 sub _build_code_and_env {
109 5 ▶ my $self = shift;
Subroutine _build_code_and_env: called CC=29 SCAR=34.7
110
111 5 my @code;
112 my %env;
113
114 5 push @code, 'sub {';
115 5 push @code, sprintf('use %s;', $self->utils_package);
116
117 5 push @code, 'sub _ ($);';
118 5 push @code, '*_ = sub ($) { goto $_ESCAPE };';
119 5 ▶ $env{'$_ESCAPE'} = \ do { $self->escape or sub { $_[0] } };
Subroutine __ANON__: not called CC=1 SCAR=0.1
Branch: 1 branch
BranchTrueFalseTotal
$self->escape or sub { $_[0]; } 4 1 5
120
121 5 push @code, 'local %_;';
122 5 push @code, 'my ($OUT, $INDENT) = (q(), q());';
123 5 push @code, 'our $_OUT_REF = \\$OUT;';
124
125 5 if (PERL_NATIVE_ALIASES) {
126 5 push @code, 'use feature qw( refaliasing );';
127 5 push @code, 'no warnings qw( experimental::refaliasing );';
128 }
129
130 5 ▶ if ($self->has_signature) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_signature) { } 5 0 5
131 5 push @code, '%_ = %{ $_SIGNATURE->(@_) };';
132 5 $env{'$_SIGNATURE'} = \ do { compile_named(@{ $self->signature }) };
133 }
134 else {
135 0 push @code, '%_ = (@_==1 and ref($_[0]) eq "HASH") ? %{$_[0]} : @_%2 ? Carp::croak("Expected even-sized list of arguments") : @_;';
136 }
137
138 5 ▶ if ($self->has_signature) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_signature) 5 0 5
139 5 my @sig = @{ $self->signature };
140 5 ▶ while (@sig) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sig) 7 5 12
141 shift @sig
142 7 ▶ while HashRef->check($sig[0]) && !$sig[0]{slurpy};
Branch: 1 branch
BranchTrueFalseTotal
&HashRef()->check($sig[0]) and not $sig[0]{"slurpy"} and do { shift @sig } 0 7 7
Condition33%1/3
&HashRef()->check($sig[0]) and not $sig[0]{"slurpy"}
!ll&&!rl&&r
700
MC/DC0%0/2
&HashRef()->check($sig[0]) and not $sig[0]{"slurpy"}
&HashRef()->check($sig[0]) not $sig[0]{"slurpy"}
Truth table33%1/3
A and not(B)
A= &HashRef()->check($sig[0])B= not $sig[0]{"slurpy"}
ABresult
0X0
100
111
143
144 7 my $name = shift @sig;
145 7 my $type = shift @sig;
146
147 7 ▶ next unless $name =~ /\A[A-Z][A-Z0-9_]*\z/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /\A[A-Z][A-Z0-9_]*\z/i 0 7 7
148
149 7 ▶ if (Bool->check($type)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Bool()->check($type)) 0 7 7
150 0 ▶ $type = $type ? Any : Optional[Any];
Branch: 1 branch
BranchTrueFalseTotal
$type ? : 0 0 0
151 }
152
153 7 unless (PERL_NATIVE_ALIASES) {
154 require Data::Alias;
155 }
156
157 7 push @code, PERL_NATIVE_ALIASES
158 ? "\\my \$$name = \\\$_{$name};"
159 : "Data::Alias::alias( my \$$name = \$_{$name} );";
160
161 7 ▶ if ($type->is_a_type_of(HashRef)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type->is_a_type_of(&HashRef())) 0 7 7
162 0 push @code, PERL_NATIVE_ALIASES
163 ? "\\my \%$name = \$$name;"
164 : "Data::Alias::alias( my \%$name = \%{ \$$name } );";
165 }
166
167 7 ▶ if ($type->is_a_type_of(ArrayRef)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type->is_a_type_of(&ArrayRef())) 1 6 7
168 1 push @code, PERL_NATIVE_ALIASES
169 ? "\\my \@$name = \$$name;"
170 : "Data::Alias::alias( my \@$name = \@{ \$$name } );";
171 }
172 }
173 }
174
175 5 push @code, "#line 1 \"template\"\n";
176
177 5 my $template = $self->template;
178 5 ▶ if ($self->trim) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->trim) 2 3 5
179 2 $template =~ s/(?:\A\s*)|(?:\s*\z)//gsm;
180 }
181
182 5 ▶ if (my $outdent = $self->outdent) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $outdent = $self->outdent) 1 4 5
183 1 ▶ $outdent > 0
Branch: 1 branch
BranchTrueFalseTotal
$outdent > 0 ? : 1 0 1
184 ? ($template =~ s/^\s{0,$outdent}//gsm)
185 : ($template =~ s/^\s+//gsm);
186 }
187
188 5 my @delims = @{ $self->delimiters };
189 5 my $regexp = join('|', map quotemeta($_), @delims);
190 5 my @parts = split /($regexp)/, $template;
191
192 5 my $mode = 'text';
193 5 ▶ while (@parts) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts) 31 5 36
194
195 31 my $next = shift @parts;
196
197 31 ▶ if ($next eq $delims[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next eq $delims[0]) 7 24 31
198 7 ▶ $mode = ($mode eq 'text') ? 'code' : confess("Impossible state");
Branch: 1 branch
BranchTrueFalseTotal
$mode eq 'text' ? : 7 0 7
199 7 next;
200 }
201
202 24 ▶ if ($next eq $delims[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next eq $delims[1]) 7 17 24
203 7 ▶ $mode = ($mode eq 'code') ? 'text' : confess("Impossible state");
Branch: 1 branch
BranchTrueFalseTotal
$mode eq 'code' ? : 7 0 7
204 7 next;
205 }
206  
207 17 ▶ my $terminator = $delims[ ($mode eq 'text') ? 0 : 1 ];
Branch: 1 branch
BranchTrueFalseTotal
$mode eq 'text' ? : 10 7 17
208 17 ▶ while (@parts and $parts[0] ne $terminator) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts and $parts[0] ne $terminator) 3 17 20
Condition100%3/3
@parts and $parts[0] ne $terminator
!ll&&!rl&&r
3143
MC/DC100%2/2
@parts and $parts[0] ne $terminator
@parts $parts[0] ne $terminator
Truth table100%3/3
A and B
A= @partsB= $parts[0] ne $terminator
ABresult
0X0
100
111
209 3 $next .= shift(@parts);
210 }
211  
212 17 ▶ if ($mode eq 'text') {
Branch: 2 branches
BranchTrueFalseTotal
if ($mode eq 'text') { } 10 7 17
elsif ($next =~ /\A=/) { } 6 1 7
213 10 $code[-1] .= sprintf('$OUT .= %s;', perlstring($next));
214 10 ▶ if ($next =~ /\n/sm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next =~ /\n/ms) 6 4 10
215 6 my $count = $next;
216 6 $count = ($count =~ y/\n//);
217 6 $code[-1] .= "\n" x $count;
218 }
219 }
220 elsif ($next =~ /\A=/) {
221 6 my ($indent) = map /\A(\s*)/, grep /\S/, split /\n/, substr($next, 1);
222 6 ▶ $code[-1] .= sprintf(
Branch: 1 branch
BranchTrueFalseTotal
$self->has_escape ? : 2 4 6
223 $self->has_escape ? '$OUT .= $_ESCAPE->(do { %s %s });' : '$OUT .= do { %s %s };',
224 sprintf("\$INDENT = %s;", perlstring($indent)),
225 substr($next, 1),
226 );
227 }
228 else {
229 1 my ($indent) = map /\A(\s*)/, grep /\S/, split /\n/, $next;
230 1 $code[-1] .= sprintf("\$INDENT = %s; %s;", perlstring($indent), $next);
231 }
232 }
233
234 5 ▶ if ($self->trim) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->trim) 2 3 5
235 2 push @code, '$OUT =~ s/(?:\A\s*)|(?:\s*\z)//gsm;';
236 }
237
238 5 ▶ if ($self->has_post_process) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_post_process) { } 0 5 5
239 0 push @code, 'do { local *_ = \$OUT; $_POST->($OUT) };';
240 0 $env{'$_POST'} = \ do { $self->post_process };
241 }
242 else {
243 5 push @code, '$OUT;';
244 }
245
246 5 push @code, '}';
247
248 #warn join "\n", @code, "";
249 5 return (\@code, \%env);
250 }
251  
252 sub _lookup_escaping {
253 1 ▶ my $style = shift;
Subroutine _lookup_escaping: called CC=4 SCAR=17.9
254
255 1 ▶ return $style if CodeRef->check($style);
Branch: 1 branch
BranchTrueFalseTotal
if &CodeRef()->check($style) 0 1 1
256
257 1 $style = lc $style;
258
259 1 ▶ if ($style eq 'html') {
Branch: 1 branch
BranchTrueFalseTotal
if ($style eq "html") 1 0 1
260 1 require HTML::Entities;
261 1 return \&HTML::Entities::encode_entities;
262 };
263
264 0 ▶ if ($style eq 'xml') {
Branch: 1 branch
BranchTrueFalseTotal
if ($style eq "xml") 0 0 0
265 0 require HTML::Entities;
266 0 return \&HTML::Entities::encode_entities_numeric;
267 };
268
269 0 croak "Unsupported escaping style '$style'";
270 }
271  
272 2 1;
273  
274 __END__
↑ Summary Compiled/Utils.pm »