blib/lib/Text/Xslate/Util.pm

filter: Statementstmt 94.9% 149 / 157 Branchbran 84.1% 69 / 82 Conditioncond 80.6% 25 / 31 MC/DCmcdc 55.5% 10 / 18 Subroutinesub 100% 20 / 20 Podpod 33.3% 4 / 12 totaltotal 86.5% 277 / 320 CC 50 SCAR 18.1
CC
50
Cov
90%
import_from
12.2
make_error
10.2
read_around
10.0
CRAP 6.1
« blib/lib/Text/Xslate.pm ↑ Summary blib/lib/Text/Xslate/Parser.pm »
1 package Text::Xslate::Util;
2 384 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 384 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 384 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 380 ▶use parent qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 378 our @EXPORT_OK = qw(
9 mark_raw unmark_raw
10 html_escape escaped_string
11 uri_escape
12 p dump
13 html_builder
14 hash_with_default
15  
16 literal_to_value value_to_literal
17 import_from
18 neat
19 is_int any_in
20 read_around
21 make_error
22 $DEBUG
23 $STRING $NUMBER
24 );
25  
26 378 our $DEBUG;
27 378 ▶defined($DEBUG) or $DEBUG = $ENV{XSLATE} || '';
Branch: 1 branch
BranchTrueFalseTotal
defined $Text::Xslate::Util::DEBUG or $Text::Xslate::Util::DEBUG = $ENV{"XSLATE"} || "" 0 378 378
Condition100%2/2
$ENV{'XSLATE'} || ''
l!l
194184
MC/DC100%1/1
$ENV{'XSLATE'} || ''
$ENV{'XSLATE'}
Truth table100%2/2
A
A= $ENV{'XSLATE'}
Aresult
11
00
28  
29 378 our $DisplayWidth = 76;
30 378 ▶if($DEBUG =~ /display_width=(\d+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Text::Xslate::Util::DEBUG =~ /display_width=(\d+)/) 0 378 378
31 0 $DisplayWidth = $1;
32 }
33  
34 # cf. http://swtch.com/~rsc/regexp/regexp1.html
35 378 my $dquoted = qr/" [^"\\]* (?: \\. [^"\\]* )* "/xms;
36 378 my $squoted = qr/' [^'\\]* (?: \\. [^'\\]* )* '/xms;
37 378 our $STRING = qr/(?: $dquoted | $squoted )/xms;
38  
39 378 our $NUMBER = qr/ (?:
40 (?: [0-9][0-9_]* (?: \. [0-9_]+)? \b) # decimal
41 |
42 (?: 0 (?:
43 (?: [0-7_]+ ) # octal
44 |
45 (?: x [0-9a-fA-F_]+) # hex
46 |
47 (?: b [01_]+ ) # binary
48 )?)
49 )/xms;
50  
51 378 require Text::Xslate; # load XS stuff
52  
53 sub mark_raw; # XS
54 sub unmark_raw; # XS
55 sub html_escape; # XS
56 sub uri_escape; # XS
57 378 sub escaped_string; *escaped_string = \&mark_raw;
58 sub merge_hash; # XS
59  
60 12 ▶sub html_builder (&){
Subroutine html_builder: called CC=1 SCAR=0.0
Pod documented
61 12 my($code_ref) = @_;
62 12 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
63 12 my $ret = $code_ref->(@_);
64 return ref($ret) eq 'CODE'
65 12 ▶ ? html_builder(\&{$ret})
Branch: 1 branch
BranchTrueFalseTotal
ref $ret eq 'CODE' ? : 0 10 10
66 : mark_raw($ret);
67 12 };
68 }
69  
70 12 ▶sub hash_with_default {
Subroutine hash_with_default: called CC=2 SCAR=7.0
Pod documented
71 12 my($hash_ref, $default) = @_;
72 12 ▶ ref($hash_ref) eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
ref $hash_ref eq "HASH" or Carp::croak("Usage: hash_with_default(\\%vars, \$default)") 10 0 10
73 or Carp::croak('Usage: hash_with_default(\%vars, $default)');
74 12 require 'Text/Xslate/HashWithDefault.pm';
75 12 my %vars;
76 12 tie %vars, 'Text::Xslate::HashWithDefault', $hash_ref, $default;
77 12 return \%vars;
78 }
79  
80  
81 # for internals
82  
83 56 ▶sub neat {
Subroutine neat: called CC=4 SCAR=13.9
Pod undocumented
84 56 my($s) = @_;
85 56 ▶ if ( defined $s ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $s) { } 50 4 54
86 52 ▶ if ( ref($s) || Scalar::Util::looks_like_number($s) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $s or &Scalar::Util::looks_like_number($s)) { } 5 45 50
Condition100%3/3
ref $s or &Scalar::Util::looks_like_number($s)
l!l&&r!l&&!r
2345
MC/DC100%2/2
ref $s or &Scalar::Util::looks_like_number($s)
ref $s &Scalar::Util::looks_like_number($s)
Truth table100%3/3
A or B
A= ref $sB= &Scalar::Util::looks_like_number($s)
ABresult
1X1
011
000
87 7 return $s;
88 }
89 else {
90 47 return "'$s'";
91 }
92 }
93 else {
94 6 return 'nil';
95 }
96 }
97  
98 39069 ▶sub is_int {
Subroutine is_int: called CC=4 SCAR=13.9
Pod undocumented
99 39069 my($s) = @_;
100 # XXX: '+1', '1.0', '00', must NOT be interpreted as an integer
101 39069 ▶ return defined($s)
Condition88%8/9
defined $s && $s =~ /\A -? [0-9]+ \z/msx && int $s eq $s && abs int $s < 2147483647
!ll&&!rl&&r
37118381911
defined $s and $s =~ /\A -? [0-9]+ \z/msx and int $s eq $s
!ll&&!rl&&r
37096221949
defined $s and $s =~ /\A -? [0-9]+ \z/msx
!ll&&!rl&&r
0370961971
MC/DC75%3/4
defined $s && $s =~ /\A -? [0-9]+ \z/msx && int $s eq $s && abs int $s < 2147483647
defined $s $s =~ /\A -? [0-9]+ \z/msx int $s eq $s abs int $s < 2147483647
Truth table80%4/5
A and B and C && D
A= defined $sB= $s =~ /\A -? [0-9]+ \z/msxC= int $s eq $sD= abs int $s < 2147483647
ABCDresult
0XXX0
10XX0
110X0
11100
11111
102 && $s =~ /\A -? [0-9]+ \z/xms
103 && int($s) eq $s
104 && abs(int($s)) < 0x7FFF_FFFF; # fits int32_t
105 }
106  
107 6654 ▶sub any_in {
Subroutine any_in: called CC=3 SCAR=12.3
Pod undocumented
108 6654 my $value = shift;
109 6654 ▶ if(defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 6652 0 6652
110 6652 ▶ return scalar grep { defined($_) && $value eq $_ } @_;
Condition66%2/3
defined $_ && $value eq $_
!ll&&!rl&&r
0120821478
MC/DC50%1/2
defined $_ && $value eq $_
defined $_ $value eq $_
Truth table66%2/3
A && B
A= defined $_B= $value eq $_
ABresult
0X0
100
111
111 }
112 else {
113 0 return scalar grep { not defined($_) } @_;
114 }
115 }
116  
117 378 my %esc2char = (
118 't' => "\t",
119 'n' => "\n",
120 'r' => "\r",
121 );
122  
123 35725 ▶sub literal_to_value {
Subroutine literal_to_value: called CC=8 SCAR=20.8
Pod undocumented
124 35723 my($value) = @_;
125 35723 ▶ return $value if not defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 35723 35723
126  
127 35723 ▶ if($value =~ s/\A "(.*)" \z/$1/xms){
Branch: 3 branches
BranchTrueFalseTotal
if ($value =~ s/\A "(.*)" \z/$1/msx) { } 33028 2695 35723
elsif ($value =~ s/\A '(.*)' \z/$1/msx) { } 272 2423 2695
elsif ($value =~ /\A [+-]? $Text::Xslate::Util::NUMBER \z/mosx) { } 2289 134 2423
128 33028 ▶ $value =~ s/\\(.)/$esc2char{$1} || $1/xmseg;
Condition66%2/3
$esc2char{$1} || $1
l!l&&r!l&&!r
170132940
MC/DC0%0/2
$esc2char{$1} || $1
$esc2char{$1} $1
Truth table66%2/3
A || B
A= $esc2char{$1}B= $1
ABresult
1X1
011
000
129 }
130 elsif($value =~ s/\A '(.*)' \z/$1/xms) {
131 272 $value =~ s/\\(['\\])/$1/xmsg; # ' for poor editors
132 }
133 elsif($value =~ /\A [+-]? $NUMBER \z/xmso) {
134 2289 ▶ if($value =~ s/\A ([+-]?) (?= 0[0-7xb])//xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ s/\A ([+-]?) (?= 0[0-7xb])//msx) { } 74 2215 2289
135 74 ▶ $value = ($1 eq '-' ? -1 : +1)
Branch: 1 branch
BranchTrueFalseTotal
$1 eq '-' ? : 6 68 74
136 * oct($value); # also grok hex and binary
137 }
138 else {
139 2215 $value =~ s/_//xmsg;
140 }
141 }
142  
143 35723 return $value;
144 }
145  
146 378 my %char2esc = (
147 "\\" => '\\\\',
148 "\n" => '\\n',
149 "\r" => '\\r',
150 '"' => '\\"',
151 '$' => '\\$',
152 '@' => '\\@',
153 );
154 378 my $value_chars = join '|', map { quotemeta } keys %char2esc;
155  
156 108 ▶sub value_to_literal {
Subroutine value_to_literal: called CC=3 SCAR=11.0
Pod undocumented
157 108 my($value) = @_;
158 108 ▶ return 'undef' if not defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 108 108
159  
160 108 ▶ if(is_int($value)){
Branch: 1 branch
BranchTrueFalseTotal
if (is_int($value)) { } 32 76 108
161 32 return $value;
162 }
163 else {
164 76 $value =~ s/($value_chars)/$char2esc{$1}/xmsgeo;
165 76 return qq{"$value"};
166 }
167 }
168  
169 36 ▶sub import_from {
Subroutine import_from: called CC=12 SCAR=25.0
Pod undocumented
170 36 my $code = "# Text::Xslate::Util::import_from()\n"
171 . "package " . "Text::Xslate::Util::_import;\n"
172 . "use warnings FATAL => 'all';\n"
173 . 'my @args;' . "\n";
174  
175 36 ▶ for(my $i = 0; $i < @_; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @_) 48 34 82
176 48 my $module = $_[$i];
177  
178 48 ▶ if($module =~ /[^a-zA-Z0-9_:]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($module =~ /[^a-zA-Z0-9_:]/) 2 46 48
179 2 Carp::confess("Xslate: Invalid module name: $module");
180 }
181  
182 46 my $commands;
183 46 ▶ if(ref $_[$i+1]){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[$i + 1]) 26 20 46
184 26 require 'Data/Dumper.pm';
185 26 my @args = ($_[++$i]);
186 26 my @protos = ('*data');
187 26 $commands = Data::Dumper->new(\@args, \@protos)->Terse(1)->Dump();
188 }
189  
190 46 ▶ $code .= "use $module ();\n" if !$module->can('export_into_xslate');
Branch: 1 branch
BranchTrueFalseTotal
unless $module->can("export_into_xslate") 38 8 46
191  
192 46 ▶ if(!defined($commands) or $commands ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $commands or $commands ne "") 46 0 46
Condition66%2/3
not defined $commands or $commands ne ""
l!l&&r!l&&!r
20260
MC/DC0%0/2
not defined $commands or $commands ne ""
not defined $commands $commands ne ""
Truth table66%2/3
not(A) or B
A= not defined $commandsB= $commands ne ""
ABresult
1X1
011
000
193 46 ▶ $code .= sprintf <<'END_IMPORT', $module, $commands || '()';
Condition100%2/2
$commands || '()'
l!l
2620
MC/DC100%1/1
$commands || '()'
$commands
Truth table100%2/2
A
A= $commands
Aresult
11
00
194 @args = %2$s;
195 %1$s->can('export_into_xslate')
196 ? %1$s->export_into_xslate(\@funcs, @args) # bridge modules
197 : %1$s->import(@args); # function-based modules
198 END_IMPORT
199 }
200 }
201  
202 34 local $Text::Xslate::Util::{'_import::'};
203 #print STDERR $code;
204 34 my @funcs;
205 34 my $e = do {
206 34 local $@;
207 34 ▶ eval qq{package}
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
208 . qq{ Text::Xslate::Util::_import;\n}
209 . $code;
210 34 $@;
211 };
212 34 ▶ Carp::confess("Xslate: Failed to import:\n" . $e) if $e;
Branch: 1 branch
BranchTrueFalseTotal
if $e 2 32 34
213 push @funcs, map {
214 32 my $entity_ref = \$Text::Xslate::Util::_import::{$_};
215 98 my $c;
216 98 ▶ if(ref($entity_ref) eq 'GLOB') { # normal symbols
Branch: 2 branches
BranchTrueFalseTotal
if (ref $entity_ref eq 'GLOB') { } 98 0 98
elsif (ref $entity_ref eq 'REF') { } 0 0 0
217 98 $c = *{$entity_ref}{CODE};
218 }
219 elsif(ref($entity_ref) eq 'REF') { # special constants
220 380 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
221 0 $c = \&{ 'Text::Xslate::Util::_import::' . $_ };
222 }
223 98 ▶ defined($c) ? ($_ => $c) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $c ? : 62 36 98
224 } keys %Text::Xslate::Util::_import::;
225  
226 32 return {@funcs};
227 }
228  
229 779 ▶sub make_error {
Subroutine make_error: called CC=10 SCAR=23.3
Pod undocumented
230 779 my($self, $message, $file, $line, @extra) = @_;
231 779 ▶ if(ref $message eq 'SCALAR') { # re-thrown form virtual machines
Branch: 1 branch
BranchTrueFalseTotal
if (ref $message eq "SCALAR") 0 779 779
232 0 return ${$message};
233 }
234  
235 779 my $lines = read_around($file, $line, 1, $self->input_layer);
236 779 ▶ if($lines) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lines) 736 43 779
237 736 ▶ $lines .= "\n" if $lines !~ /\n\z/xms;
Branch: 1 branch
BranchTrueFalseTotal
unless $lines =~ /\n\z/msx 158 578 736
238 736 $lines = '-' x $DisplayWidth . "\n"
239 . $lines
240 . '-' x $DisplayWidth . "\n";
241 }
242  
243 779 local $Carp::CarpLevel = $Carp::CarpLevel + 1;
244 779 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
77900
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
245 779 ▶ $message =~ s/\A \Q$class: \E//xms and $message .= "\t...";
Branch: 1 branch
BranchTrueFalseTotal
$message =~ s/\A \Q$class: \E//msx and $message .= "\t..." 459 320 779
246  
247 779 ▶ if(defined $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file) 759 20 779
248 759 ▶ if(defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line) 753 6 759
249 753 unshift @extra, $line;
250 }
251 759 ▶ unshift @extra, ref($file) ? '<string>' : $file;
Branch: 1 branch
BranchTrueFalseTotal
ref $file ? : 307 452 759
252 }
253  
254 779 ▶ if(@extra) {
Branch: 1 branch
BranchTrueFalseTotal
if (@extra) { } 759 20 779
255 759 $message = Carp::shortmess(sprintf '%s (%s)',
256 $message, join(':', @extra));
257 }
258 else {
259 20 $message = Carp::shortmess($message);
260 }
261 779 return sprintf "%s: %s%s",
262 $class, $message, $lines;
263 }
264  
265 789 ▶sub read_around { # for error messages
Subroutine read_around: called CC=10 SCAR=23.0
Pod undocumented
266 789 my($file, $line, $around, $input_layer) = @_;
267  
268 789 ▶ defined($file) && defined($line) or return '';
Branch: 1 branch
BranchTrueFalseTotal
defined $file and defined $line or return "" 761 28 789
Condition100%3/3
defined $file and defined $line
!ll&&!rl&&r
226761
MC/DC100%2/2
defined $file and defined $line
defined $file defined $line
Truth table100%3/3
A and B
A= defined $fileB= defined $line
ABresult
0X0
100
111
269  
270 761 ▶ if (ref $file) { # if $file is a scalar ref, it must contain text strings
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file) 301 460 761
271 301 my $content = $$file;
272 301 utf8::encode($content);
273 301 $file = \$content;
274 }
275  
276 761 ▶ $around = 1 if not defined $around;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $around 6 755 761
277 761 ▶ $input_layer = '' if not defined $input_layer;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input_layer 8 753 761
278  
279 761 ▶ open my $in, '<' . $input_layer, $file or return '';
Branch: 1 branch
BranchTrueFalseTotal
open my $in, '<' . $input_layer, $file or return "" 742 19 761
280 742 local $/ = "\n";
281 742 local $. = 0;
282  
283 742 my $s = '';
284 742 ▶ while(defined(my $l = <$in>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $l = readline $in)) 1612 660 2272
285 1612 ▶ if($. >= ($line - $around)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($. >= $line - $around) 1332 280 1612
286 1332 $s .= $l;
287 }
288 1612 ▶ if($. >= ($line + $around)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($. >= $line + $around) 82 1530 1612
289 82 last;
290 }
291 }
292 742 return $s;
293 }
294  
295 148 ▶sub p { # for debugging, the guts of dump()
Subroutine p: called CC=2 SCAR=7.1
Pod documented
296 148 require 'Data/Dumper.pm'; # we don't want to create its namespace
297 148 my $dd = Data::Dumper->new(\@_);
298 148 $dd->Indent(1);
299 148 $dd->Sortkeys(1);
300 148 $dd->Quotekeys(0);
301 148 $dd->Terse(1);
302 148 ▶ return $dd->Dump() if defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 148 0 148
303 0 print $dd->Dump();
304 }
305  
306 6 ▶sub dump :method { goto &p }
Subroutine dump: called CC=1 SCAR=0.0
Pod documented
307  
308 378 1;
309 __END__
« blib/lib/Text/Xslate.pm ↑ Summary blib/lib/Text/Xslate/Parser.pm »