PLON.pm

filter: Statementstmt 90.7% 177 / 195 Branchbran 86.9% 113 / 130 Conditioncond 40.0% 2 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 96.9% 32 / 33 Podpod 16.6% 1 / 6 totaltotal 87.6% 326 / 372 CC 69 SCAR 23.9
CC
69
Cov
88%
_encode_basic
22.6
_decode_string
18.4
_decode
12.1
CRAP 10.9
↑ Summary Scalar/Perl.pm »
1 package PLON;
2 12 ▶use 5.008005;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use Scalar::Util qw(blessed reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 ▶use parent qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 12 ▶use B;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 12 our $VERSION = "0.08";
12  
13 12 our @EXPORT = qw(encode_plon decode_pson $_perl);
14  
15 12 our $INDENT;
16  
17 12 my $WS = qr{[ \t]*};
18  
19 1 ▶sub encode_plon { PLON->new->encode(shift) }
Subroutine encode_plon: called CC=1 SCAR=0.0
Pod undocumented
20 1 ▶sub decode_pson { PLON->new->decode(shift) }
Subroutine decode_pson: called CC=1 SCAR=0.0
Pod undocumented
21  
22 48 ▶sub mk_accessor {
Subroutine mk_accessor: called CC=1 SCAR=0.0
Pod undocumented
23 48 my ($pkg, $name) = @_;
24  
25 12 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 48 ▶ *{"${pkg}::${name}"} = sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
27 6 ▶ my $enable = defined($_[1]) ? $_[1] : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 4 2 6
28 6 ▶ if ($enable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($enable) { } 6 0 6
29 6 $_[0]->{$name} = 1;
30 } else {
31 0 $_[0]->{$name} = 0;
32 }
33 6 $_[0];
34 48 };
35 48 ▶ *{"${pkg}::get_${name}"} = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
36 137 ▶ $_[0]->{$name} ? 1 : '';
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{$name} ? : 40 97 137
37 48 };
38 }
39  
40 25 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
41 25 my $class = shift;
42 25 bless {
43 }, $class;
44 }
45  
46 12 mk_accessor(__PACKAGE__, $_) for qw(pretty ascii deparse canonical);
47  
48 30 ▶sub encode {
Subroutine encode: called CC=1 SCAR=0.0
Pod undocumented
49 30 my ($self, $stuff) = @_;
50 30 local $INDENT = -1;
51 30 return $self->_encode($stuff);
52 }
53  
54 73 ▶sub _encode {
Subroutine _encode: called CC=2 SCAR=6.9
55 73 my ($self, $value) = @_;
56 73 local $INDENT = $INDENT + 1;
57  
58 73 my $blessed = blessed($value);
59  
60 73 ▶ if (defined $blessed) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $blessed) { } 3 70 73
61 3 'bless(' . $self->_encode_basic($value, 1) . ',' . $self->_encode_basic($blessed) . ')';
62 } else {
63 70 $self->_encode_basic($value);
64 }
65 }
66  
67 76 ▶sub _encode_basic {
Subroutine _encode_basic: called CC=22 SCAR=31.2
68 76 my ($self, $value, $blessing) = @_;
69  
70 76 ▶ if (not defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 2 74 76
71 2 return 'undef';
72 }
73  
74 74 my $reftype = reftype($value);
75 74 ▶ if (not defined $reftype) {
Branch: 7 branches
BranchTrueFalseTotal
if (not defined $reftype) { } 42 32 74
elsif ($reftype eq 'SCALAR') { } 3 29 32
elsif ($reftype eq 'REF') { } 1 28 29
elsif ($reftype eq 'ARRAY') { } 20 8 28
elsif ($reftype eq 'CODE') { } 2 6 8
elsif ($reftype eq 'HASH') { } 5 1 6
elsif ($reftype eq 'GLOB') { } 1 0 1
76 42 my $flags = B::svref_2object(\$value)->FLAGS;
77 42 ▶ return 0 + $value if $flags & (B::SVp_IOK | B::SVp_NOK) && $value * 0 == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $flags & 12288 and $value * 0 == 0 12 30 42
Condition66%2/3
$flags & 12288 and $value * 0 == 0
!ll&&!rl&&r
30012
MC/DC50%1/2
$flags & 12288 and $value * 0 == 0
$flags & 12288 $value * 0 == 0
Truth table66%2/3
A and B
A= $flags & 12288B= $value * 0 == 0
ABresult
0X0
100
111
78  
79 # string
80 30 ▶ if ($self->{ascii}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'ascii'}) { } 2 28 30
81 2 $value =~ s/"/\\"/g;
82 2 ▶ if (Encode::is_utf8($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Encode::is_utf8($value)) { } 2 0 2
83 2 my $buf = '';
84 2 for (split //, $value) {
85 3 ▶ if ($_ =~ /\G[a-zA-Z0-9_ -]\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ =~ /\G[a-zA-Z0-9_ -]\z/) { } 1 2 3
86 1 $buf .= Encode::encode_utf8($_);
87 } else {
88 2 $buf .= sprintf "\\x{%X}", ord $_;
89 }
90 }
91 2 $value = $buf;
92 } else {
93 0 $value = $value;
94 }
95 2 q{"} . $value . q{"};
96 } else {
97 #
98 # Here is the list of special characters from perlop.pod
99 #
100 # Sequence Note Description
101 # \t tab (HT, TAB)
102 # \n newline (NL)
103 # \r return (CR)
104 # \f form feed (FF)
105 # \b backspace (BS)
106 # \a alarm (bell) (BEL)
107 # \e escape (ESC)
108 # \x{263A} [1,8] hex char (example: SMILEY)
109 # \x1b [2,8] restricted range hex char (example: ESC)
110 # \N{name} [3] named Unicode character or character sequence
111 # \N{U+263D} [4,8] Unicode character (example: FIRST QUARTER MOON)
112 # \c[ [5] control char (example: chr(27))
113 # \o{23072} [6,8] octal char (example: SMILEY)
114 # \033 [7,8] restricted range octal char (example: ESC)
115 #
116 28 my %special_chars = (
117 qq{"} => q{\"},
118 qq{\t} => q{\t},
119 qq{\n} => q{\n},
120 qq{\r} => q{\r},
121 qq{\f} => q{\f},
122 qq{\b} => q{\b},
123 qq{\a} => q{\a},
124 qq{\e} => q{\e},
125 q{$} => q{\$},
126 q{@} => q{\@},
127 q{%} => q{\%},
128 q{\\} => q{\\\\},
129 );
130 28 $value =~ s/(.)/
131 43 ▶ if (exists($special_chars{$1})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $special_chars{$1}) { } 11 32 43
132 11 $special_chars{$1};
133 } else {
134 32 $1
135 }
136 /gexs;
137 28 ▶ $value = Encode::is_utf8($value) ? Encode::encode_utf8($value) : $value;
Branch: 1 branch
BranchTrueFalseTotal
&Encode::is_utf8($value) ? : 1 27 28
138 28 q{"} . $value . q{"};
139 }
140 } elsif ($reftype eq 'SCALAR') {
141 3 ▶ if ($blessing) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blessing) { } 1 2 3
142 1 '\\(do {my $o=' . $self->_encode($$value) . '})';
143 } else {
144 2 '\\(' . $self->_encode($$value) . ')';
145 }
146 } elsif ($reftype eq 'REF') {
147 1 '\\(' . $self->_encode($$value) . ')';
148 } elsif ($reftype eq 'ARRAY') {
149 join('',
150 '[',
151 $self->_nl,
152 20 (map { $self->_indent(1) . $self->_encode($_) . "," . $self->_nl }
153 @$value),
154 $self->_indent,
155 ']',
156 );
157 } elsif ($reftype eq 'CODE') {
158 2 ▶ if ($self->get_deparse) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_deparse) { } 1 1 2
159 1 require B::Deparse;
160 1 ▶ my $code = B::Deparse->new($self->get_pretty ? '' : '-si0')->coderef2text($value);
Branch: 1 branch
BranchTrueFalseTotal
$self->get_pretty ? : 0 1 1
161 1 $code = "sub ${code}";
162 1 ▶ if ($self->get_pretty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_pretty) { } 0 1 1
163 0 my $indent = $self->_indent;
164 0 $code =~ s/^/$indent/gm;
165 0 $code;
166 } else {
167 1 $code =~ s/\n//g;
168 1 $code;
169 }
170 } else {
171 1 'sub { "DUMMY" }'
172 }
173 } elsif ($reftype eq 'HASH') {
174 5 my @keys = keys %$value;
175 5 ▶ if ($self->get_canonical) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_canonical) 1 4 5
176 1 @keys = sort { $a cmp $b } @keys;
177 }
178  
179 join('',
180 '{',
181 $self->_nl,
182 (map {
183 5 $self->_indent(1) . $self->_encode($_)
184 . $self->_before_sp . '=>' . $self->_after_sp
185 7 . $self->_encode($value->{$_})
186 . "," . $self->_nl,
187 } @keys),
188 $self->_indent,
189 '}',
190 );
191 } elsif ($reftype eq 'GLOB') {
192 1 "\\(" . $$value . ")";
193 } else {
194 0 die "Unknown type: ${reftype}";
195 }
196 }
197  
198 57 ▶sub _indent {
Subroutine _indent: called CC=3 SCAR=11.0
199 57 my ($self, $n) = @_;
200 57 ▶ if (not defined $n) { $n = 0 };
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $n) 25 32 57
201 57 ▶ $self->get_pretty ? ' ' x ($INDENT+$n) : ''
Branch: 1 branch
BranchTrueFalseTotal
$self->get_pretty ? : 17 40 57
202 }
203  
204 57 ▶sub _nl {
Subroutine _nl: called CC=2 SCAR=6.9
205 57 my $self = shift;
206 57 ▶ $self->get_pretty ? "\n" : '',
Branch: 1 branch
BranchTrueFalseTotal
$self->get_pretty ? : 17 40 57
207 }
208  
209 7 ▶sub _before_sp {
Subroutine _before_sp: called CC=2 SCAR=6.9
210 7 my $self = shift;
211 7 ▶ $self->get_pretty ? " " : ''
Branch: 1 branch
BranchTrueFalseTotal
$self->get_pretty ? : 2 5 7
212 }
213  
214 7 ▶sub _after_sp {
Subroutine _after_sp: called CC=2 SCAR=6.9
215 7 my $self = shift;
216 7 ▶ $self->get_pretty ? " " : ''
Branch: 1 branch
BranchTrueFalseTotal
$self->get_pretty ? : 2 5 7
217 }
218  
219 31 ▶sub decode {
Subroutine decode: called CC=1 SCAR=0.0
Pod undocumented
220 31 my ($self, $src) = @_;
221 31 local $_ = $src;
222 31 return $self->_decode();
223 }
224  
225 68 ▶sub _decode {
Subroutine _decode: called CC=12 SCAR=25.0
226 68 my ($self) = @_;
227  
228 68 ▶ if (/\G$WS\{/gc) {
Branch: 11 branches
BranchTrueFalseTotal
if (/\G$WS\{/cg) { } 5 63 68
elsif (/\G$WS\[/cg) { } 18 45 63
elsif (/\G$WS"/cg) { } 23 22 45
elsif (/\G${WS}undef/cg) { } 2 20 22
elsif (/\G$WS\\\(/cg) { } 5 15 20
elsif (/\G${WS}sub\s*\{/cg) { } 2 13 15
elsif (/\G$WS"/cg) { } 0 13 13
elsif (/\G$WS([0-9\.]+)/cg) { } 8 5 13
elsif (/\G${WS}bless\(/cg) { } 3 2 5
elsif (/\G${WS}do \{my \$o=/cg) { } 1 1 2
elsif (/\G$WS\*([a-zA-Z0-9_:]+)/cg) { } 1 0 1
229 5 return $self->_decode_hash();
230 } elsif (/\G$WS\[/gc) {
231 18 return $self->_decode_array();
232 } elsif (/\G$WS"/gc) {
233 23 return $self->_decode_string();
234 } elsif (/\G${WS}undef/gc) {
235 2 return undef;
236 } elsif (/\G${WS}\\\(/gc) {
237 5 return $self->_decode_scalarref();
238 } elsif (/\G${WS}sub\s*\{/gc) {
239 2 return $self->_decode_code();
240 } elsif (/\G$WS"/gc) {
241 0 return $self->_decode_string;
242 } elsif (/\G$WS([0-9\.]+)/gc) {
243 8 return 0+$1;
244 } elsif (/\G${WS}bless\(/gc) {
245 3 return $self->_decode_object;
246 } elsif (/\G${WS}do \{my \$o=/gc) {
247 1 return $self->_decode_do;
248 } elsif (/\G$WS\*([a-zA-Z0-9_:]+)/gc) {
249 12 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
250 1 *{$1};
251 } else {
252 0 Carp::confess("Unexpected token: " . substr($_, pos, 9));
253 }
254 }
255  
256 5 ▶sub _decode_hash {
Subroutine _decode_hash: called CC=4 SCAR=13.9
257 5 my ($self) = @_;
258  
259 5 my %ret;
260 5 ▶ until (/\G$WS(,$WS)?\}/gc) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/\G$WS(,$WS)?\}/cg) 5 3 8
261 5 my $k = $self->_decode();
262 5 ▶ /\G$WS=>$WS/gc
Branch: 1 branch
BranchTrueFalseTotal
/\G$WS=>$WS/cg or _exception("Unexpected token in Hash") 5 0 5
263 or _exception("Unexpected token in Hash");
264 5 my $v = $self->_decode();
265  
266 5 $ret{$k} = $v;
267  
268 5 ▶ /\G$WS,/gc
Branch: 1 branch
BranchTrueFalseTotal
/\G$WS,/cg or last 3 2 5
269 or last;
270 }
271 5 return \%ret;
272 }
273  
274 18 ▶sub _decode_array {
Subroutine _decode_array: called CC=2 SCAR=6.9
275 18 my ($self) = @_;
276  
277 18 my @ret;
278 18 ▶ until (/\G$WS,?$WS\]/gc) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/\G$WS,?$WS\]/cg) 15 17 32
279 15 my $term = $self->_decode();
280 14 push @ret, $term;
281 }
282 17 return \@ret;
283 }
284  
285 2 ▶sub _decode_code {
Subroutine _decode_code: called CC=1 SCAR=0.0
286 # We can't decode coderef. Because it makes security issue.
287 # And, we can't detect end of code block.
288 2 Carp::confess("Cannot decode PLON contains CodeRef.");
289 }
290  
291 3 ▶sub _decode_object {
Subroutine _decode_object: called CC=3 SCAR=11.2
292 3 my ($self) = @_;
293 3 my $body = $self->_decode; # class name
294 3 ▶ m!\G${WS},\s*!gc
Branch: 1 branch
BranchTrueFalseTotal
/\G$WS,\s*/cg or _exception("Missing comma after bless") 3 0 3
295 or _exception("Missing comma after bless");
296 3 my $str = $self->_decode; # class name
297 3 ▶ m!\G${WS}\)!gc
Branch: 1 branch
BranchTrueFalseTotal
/\G$WS\)/cg or _exception("Missing closing paren after bless") 3 0 3
298 or _exception("Missing closing paren after bless");
299 3 return bless($body, $str);
300 }
301  
302 5 ▶sub _decode_scalarref {
Subroutine _decode_scalarref: called CC=2 SCAR=7.0
303 5 my $self = shift;
304 5 my $value = $self->_decode();
305 5 ▶ /\G\s*\)/gc
Branch: 1 branch
BranchTrueFalseTotal
/\G\s*\)/cg or _exception("Missing closing paren after scalarref") 5 0 5
306 or _exception("Missing closing paren after scalarref");
307 5 return \$value;
308 }
309  
310 # do {my $o=3}
311 1 ▶sub _decode_do {
Subroutine _decode_do: called CC=2 SCAR=7.0
312 1 my $self = shift;
313 1 my $value = $self->_decode;
314 1 ▶ m!\G\}!gc
Branch: 1 branch
BranchTrueFalseTotal
/\G\}/cg or _exception("Missing closing blace after `do {`") 1 0 1
315 or _exception("Missing closing blace after `do {`");
316 1 return $value;
317 }
318  
319 23 ▶sub _decode_string {
Subroutine _decode_string: called CC=18 SCAR=29.1
320 23 my $self = shift;
321  
322 23 my $ret;
323 23 ▶ until (/\G"/gc) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/\G"/cg) 39 23 62
324 39 ▶ if (/\G\\"/gc) {
Branch: 15 branches
BranchTrueFalseTotal
if (/\G\\"/cg) { } 1 38 39
elsif (/\G\\\$/cg) { } 1 37 38
elsif (/\G\\t/cg) { } 1 36 37
elsif (/\G\\n/cg) { } 1 35 36
elsif (/\G\\r/cg) { } 1 34 35
elsif (/\G\\f/cg) { } 1 33 34
elsif (/\G\\b/cg) { } 1 32 33
elsif (/\G\\a/cg) { } 1 31 32
elsif (/\G\\e/cg) { } 1 30 31
elsif (/\G\\$/cg) { } 0 30 30
elsif (/\G\\@/cg) { } 1 29 30
elsif (/\G\\%/cg) { } 0 29 29
elsif (/\G\\\\/cg) { } 1 28 29
elsif (/\G\\x\{([0-9a-fA-F]+)\}/cg) { } 1 27 28
elsif (/\G([^"\\]+)/cg) { } 27 0 27
325 1 $ret .= q{"};
326 } elsif (/\G\\\$/gc) {
327 1 $ret .= qq{\$};
328 } elsif (/\G\\t/gc) {
329 1 $ret .= qq{\t};
330 } elsif (/\G\\n/gc) {
331 1 $ret .= qq{\n};
332 } elsif (/\G\\r/gc) {
333 1 $ret .= qq{\r};
334 } elsif (/\G\\f/gc) {
335 1 $ret .= qq{\f};
336 } elsif (/\G\\b/gc) {
337 1 $ret .= qq{\b};
338 } elsif (/\G\\a/gc) {
339 1 $ret .= qq{\a};
340 } elsif (/\G\\e/gc) {
341 1 $ret .= qq{\e};
342 } elsif (/\G\\$/gc) {
343 0 $ret .= qq{\$};
344 } elsif (/\G\\@/gc) {
345 1 $ret .= qq{\@};
346 } elsif (/\G\\%/gc) {
347 0 $ret .= qq{\%};
348 } elsif (/\G\\\\/gc) {
349 1 $ret .= qq{\\};
350 } elsif (/\G\\x\{([0-9a-fA-F]+)\}/gc) { # \x{5963}
351 1 $ret .= chr(hex $1);
352 } elsif (/\G([^"\\]+)/gc) {
353 27 $ret .= $1;
354 } else {
355 0 _exception("Unexpected EOF in string");
356 }
357 }
358 # If it's utf-8, it means the PLON encoded by ASCII mode.
359 # The PLON contains "\x{5963}". Then, we shouldn't decode the string.
360 23 ▶ return Encode::is_utf8($ret) ? $ret : Encode::decode_utf8($ret);
Branch: 1 branch
BranchTrueFalseTotal
&Encode::is_utf8($ret) ? : 1 22 23
361 }
362  
363 0 ▶sub _exception {
Subroutine _exception: not called CC=3 SCAR=24.8
364  
365 # Leading whitespace
366 0 m/\G$WS/gc;
367  
368 # Context
369 0 my $context = 'Malformed PLON: ' . shift;
370 0 ▶ if (m/\G\z/gc) { $context .= ' before end of data' }
Branch: 1 branch
BranchTrueFalseTotal
if (/\G\z/cg) { } 0 0 0
371 else {
372 0 my @lines = split "\n", substr($_, 0, pos);
373 0 ▶ $context .= ' at line ' . @lines . ', offset ' . length(pop @lines || '');
Condition0%0/2
pop @lines || ''
l!l
00
MC/DC0%0/1
pop @lines || ''
pop @lines
Truth table0%0/2
A
A= pop @lines
Aresult
11
00
374 }
375  
376 0 die "$context\n";
377 }
378  
379 12 1;
380 __END__
↑ Summary Scalar/Perl.pm »