blib/lib/Jasonify.pm

filter: Statementstmt 88.9% 177 / 199 Branchbran 64.4% 98 / 152 Conditioncond 85.7% 18 / 21 MC/DCmcdc 70.0% 7 / 10 Subroutinesub 79.3% 46 / 58 Podpod 76.9% 10 / 13 totaltotal 78.5% 356 / 453 SCAR 50.2
CC
84
Cov
79%
objectify
50.2
negate
30.0
_scalarify
24.4
CRAP 151.6
↑ Summary
1 2 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Jasonify v0.20.052;
5 # ABSTRACT: Just Another Serialized Object Notation library.
6  
7  
8 2 ▶use Carp (); #qw( carp );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Datify v0.20.052 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Scalar::Util (); #qw( blessed looks_like_number reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use String::Tools qw( subst ); #qw( );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use parent 'Datify';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16  
17  
18 ### Accessor ###
19  
20  
21  
22  
23 ### Setter ###
24  
25  
26  
27  
28 2 __PACKAGE__->set(
29 # Varify/Encode options
30 #name => '',
31 #assign => undef,
32 #list => undef,
33 list_sep => ', ',
34 beautify => undef,
35 );
36  
37  
38  
39 2 __PACKAGE__->set(
40 # Undefify options
41 null => 'null',
42 );
43  
44  
45  
46 2 __PACKAGE__->set(
47 # Booleanify options
48 false => 'false',
49 true => 'true',
50 );
51  
52  
53  
54 __PACKAGE__->set(
55 # Stringify options
56 quote => '"',
57 #quote1 => undef,
58 quote2 => '"',
59 #q1 => undef,
60 #q2 => undef,
61 #sigils => undef,
62 longstr => -1,
63 #encode1 => undef,
64 encode2 => {
65 134 map( { $_ => sprintf( '\\u%04x', $_ ) }
66 0x00 .. 0x1f, 0x7f, # Control characters (C0)
67 0x80 .. 0x9f, # Control characters (C1)
68 0x2028, 0x2029, # Characters not allowed by Javascript
69 ),
70 # Special cases
71 2 map( { ord( eval qq!"$_"! ) => $_ } qw( \b \t \n \r \" \\\\ ) ),
72 utf => 16,
73 byte => '\\u00%02x',
74 wide => '\\u%04x',
75 },
76 #qpairs => undef,
77 #qquotes => undef,
78 );
79  
80  
81  
82 2 __PACKAGE__->set(
83 # Numify options
84 infinite => '"Infinity"',
85 -infinite => '"-Infinity"',
86 nonnumber => '"NaN"',
87 #num_sep => undef,
88 );
89  
90  
91  
92 2 __PACKAGE__->set(
93 # Lvalueify options
94 lvalue => '$lvalue',
95 );
96  
97  
98  
99 2 __PACKAGE__->set(
100 # Vstringify options
101 vformat => '"\\u%0*v4x"',
102 vsep => '\\u',
103 );
104  
105  
106 #=option Regexpify options
107 #
108 #=over
109 #
110 #=item ...
111 #
112 #=back
113 #
114 #=cut
115 #
116 #__PACKAGE__->set(
117 # # Regexpify options
118 # #quote3 => undef,
119 # #q3 => undef,
120 # #encode3 => undef,
121 #);
122  
123  
124  
125 2 __PACKAGE__->set(
126 # Arrayify options
127 array_ref => '[$_]',
128 );
129  
130  
131  
132 2 __PACKAGE__->set(
133 # Hashify options
134 hash_ref => '{$_}',
135 pair => '$key : $value',
136 keysort => \&Datify::keysort,
137 keyfilter => undef,
138 keyfilterdefault => 1,
139 #keywords => undef,
140 );
141  
142  
143  
144 2 __PACKAGE__->set(
145 # Objectify options
146 json_method => 'TO_JSON',
147 object => '$data',
148 #object => '{$class_str : $data}',
149 overloads => [qw( "" 0+ )],
150 tag => undef,
151 #tag => '($class_str)$data',
152 tag_method => 'FREEZE',
153 );
154  
155  
156  
157 2 __PACKAGE__->set(
158 # Ioify options
159 io => 'null',
160 );
161  
162  
163  
164 2 __PACKAGE__->set(
165 # Codeify options
166 code => 'null',
167 #codename => undef,
168 #body => undef,
169 );
170  
171  
172  
173 2 __PACKAGE__->set(
174 # Refify options
175 reference => '$_',
176 dereference => '$referent$place',
177 #nested => undef,
178 );
179  
180  
181  
182 2 __PACKAGE__->set(
183 # Formatify options
184 format => 'null',
185 );
186  
187  
188  
189 # Override Datify::booleanify() for SCALAR refs
190 sub booleanify {
191 72 ▶ my $self = &Datify::self;
Subroutine booleanify: called CC=5 SCAR=16.1
Pod documented
192 72 ▶ local $_ = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 72 0 72
193 72 ▶ return $self->undefify unless defined;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 9 63 72
194 63 ▶ return $self->booleanify($$_) if 'SCALAR' eq ref;
Branch: 1 branch
BranchTrueFalseTotal
if "SCALAR" eq ref $_ 4 59 63
195 59 ▶ return $_ ? $Jasonify::Boolean::true : $Jasonify::Boolean::false;
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 40 19 59
196 }
197  
198  
199  
200 # Override Datify::keyify() to appropriately stringify all keys
201 sub keyify {
202 138 ▶ my $self = &Datify::self;
Subroutine keyify: called CC=6 SCAR=18.1
Pod documented
203 138 ▶ local $_ = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 138 0 138
204  
205 138 ▶ return Scalar::Util::looks_like_number($_)
Branch: 4 branches
BranchTrueFalseTotal
&Scalar::Util::looks_like_number($_) ? : 91 47 138
defined($_ <=> 0) ? : 87 4 91
$_ == 9**9**9 ? : 4 83 87
$_ == -9**9**9 ? : 0 83 83
206 ? (
207 defined( $_ <=> 0 )
208 ? (
209 $_ == "Infinity" ? $Jasonify::Number::inf
210 : $_ == "-Infinity" ? $Jasonify::Number::ninf
211 : $self->stringify($_)
212 )
213 : $Jasonify::Number::nan
214 )
215 : $self->stringify($_);
216 }
217  
218 sub _objectify_via {
219 4 ▶ my $self = shift;
Subroutine _objectify_via: called CC=2 SCAR=6.9
220 4 my $object = shift;
221  
222 4 ▶ if ( my $method_name = shift ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $method_name = shift()) 2 2 4
223 2 return $object->can($method_name);
224 }
225 2 return;
226 }
227 sub _objectify_via_tag {
228 2 ▶ my $self = shift;
Subroutine _objectify_via_tag: called CC=2 SCAR=7.4
229 2 my $object = shift;
230  
231 2 ▶ my $tag_method = $self->get('tag') && $self->get('tag_method');
Condition33%1/3
$self->get('tag') && $self->get('tag_method')
!ll&&!rl&&r
200
MC/DC0%0/2
$self->get('tag') && $self->get('tag_method')
$self->get('tag') $self->get('tag_method')
Truth table33%1/3
A && B
A= $self->get('tag')B= $self->get('tag_method')
ABresult
0X0
100
111
232 2 return $self->_objectify_via( $object => $tag_method );
233 }
234 sub _objectify_via_json {
235 2 ▶ my $self = shift;
Subroutine _objectify_via_json: called CC=1 SCAR=0.0
236 2 my $object = shift;
237  
238 2 return $self->_objectify_via( $object => $self->get('json_method') );
239 }
240  
241  
242  
243 # Override Datify::objectify() to appropriately stringify objects
244 sub objectify {
245 55 ▶ my $self = &Datify::self;
Subroutine objectify: called CC=16 SCAR=39.2
Pod documented
246 55 my $object = shift;
247  
248 55 ▶ return $self->scalarify($object)
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $class = builtin::blessed($object)) 0 55 55
249 unless defined( my $class = Scalar::Util::blessed($object) );
250  
251 55 my $object_str = $self->get('object');
252  
253 55 my $data;
254 55 ▶ if (0) {
Branch: 5 branches
BranchTrueFalseTotal
if (do { my $code = $self->_find_handler($class) }) { } 53 2 55
elsif (my $tag = $self->_objectify_via_tag($object)) { } 0 2 2
elsif (my $to_json = $self->_objectify_via_json($object)) { } 0 2 2
elsif (my $method = $self->overloaded($object)) { } 0 2 2
elsif (my $attrkeyvals = $object->can('_attrkeyvals')) { } 0 2 2
255 } elsif ( my $code = $self->_find_handler($class) ) {
256 53 return $self->$code($object);
257 } elsif ( my $tag = $self->_objectify_via_tag($object) ) {
258 0 $object_str = $self->get('tag');
259 0 $data = $self->arrayify( $object->$tag('JSON') );
260 } elsif ( my $to_json = $self->_objectify_via_json($object) ) {
261 0 $data = $self->scalarify( $object->$to_json() );
262 } elsif ( my $method = $self->overloaded($object) ) {
263 0 $data = $self->scalarify( $object->$method() );
264 } elsif ( my $attrkeyvals = $object->can('_attrkeyvals') ) {
265 # TODO: Look this up via meta-objects and such.
266 0 $data = $self->hashify( $object->$attrkeyvals() );
267 } else {
268 2 $data = Scalar::Util::reftype $object;
269  
270 2 ▶ $data
Branch: 9 branches
BranchTrueFalseTotal
$data eq 'ARRAY' ? : 0 2 2
$data eq 'CODE' ? : 0 2 2
$data eq 'FORMAT' ? : 0 2 2
$data eq 'GLOB' ? : 0 2 2
$data eq 'HASH' ? : 2 0 2
$data eq 'IO' ? : 0 0 0
$data eq 'REF' ? : 0 0 0
$data eq 'REGEXP' ? : 0 0 0
$data eq 'SCALAR' ? : 0 0 0
271 = $data eq 'ARRAY' ? $self->arrayify( @$object )
272 : $data eq 'CODE' ? $self->codeify( $object )
273 : $data eq 'FORMAT' ? $self->formatify( $object )
274 : $data eq 'GLOB' ? $self->globify( $object )
275 : $data eq 'HASH' ? $self->hashify( $object )
276 : $data eq 'IO' ? $self->ioify( $object )
277 : $data eq 'REF' ? $self->scalarify( $$object )
278 : $data eq 'REGEXP' ? $self->regexpify( $object )
279 : $data eq 'SCALAR' ? $self->scalarify( $$object )
280 : $self->undefify;
281 }
282  
283 2 return subst(
284 $object_str,
285 class_str => $self->stringify($class),
286 class => $class,
287 data => $data,
288 );
289 }
290  
291  
292  
293 # Override Datify::regexpify() to appropriately stringify regular expressions
294 sub regexpify {
295 2 ▶ my $self = &Datify::self;
Subroutine regexpify: called CC=2 SCAR=7.1
Pod documented
296 2 ▶ local $_ = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 2 0 2
297  
298 2 return $self->stringify($_);
299 }
300  
301 # Override Datify::varify so that it throws an error
302 sub varify;
303  
304  
305  
306 # Override Datify::vstringify so that it encodes a vstring as appropriate
307 sub vstringify {
308 2 ▶ my $self = &Datify::self;
Subroutine vstringify: called CC=3 SCAR=11.7
Pod documented
309 2 ▶ local $_ = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 2 0 2
310  
311 # Encode as a vstring if vformat has been specified
312 # or as a regular string if vformat has not been specified
313 2 ▶ return $self->get('vformat')
Branch: 1 branch
BranchTrueFalseTotal
$self->get('vformat') ? : 2 0 2
314 ? $self->SUPER::vstringify($_)
315 : $self->stringify($_);
316 }
317  
318  
319  
320 # Override Datify::scalarify to properly handle all of the various types
321 sub _scalarify {
322 347 ▶ my $self = &Datify::self;
Subroutine _scalarify: called CC=22 SCAR=31.9
323 347 ▶ local $_ = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 347 0 347
324  
325 347 ▶ return $self->undefify unless defined $_;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 11 336 347
326  
327 336 ▶ if ( defined( my $blessed = Scalar::Util::blessed($_) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $blessed = builtin::blessed($_))) 57 279 336
328 return
329 57 ▶ $blessed eq 'Regexp' ? $self->regexpify($_)
Branch: 1 branch
BranchTrueFalseTotal
$blessed eq 'Regexp' ? : 2 55 57
330 : $self->objectify($_);
331 }
332  
333 279 my $ref = Scalar::Util::reftype $_;
334 279 ▶ if ( not $ref ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ref) 164 115 279
335 # Handle GLOB, LVALUE, and VSTRING
336 164 my $ref2 = ref \$_;
337 return
338 164 ▶ $ref2 eq 'GLOB' ? $self->globify($_)
Branch: 4 branches
BranchTrueFalseTotal
$ref2 eq 'GLOB' ? : 0 164 164
$ref2 eq 'LVALUE' ? : 0 164 164
$ref2 eq 'VSTRING' ? : 2 162 164
$ref2 eq 'SCALAR' && &Scalar::Util::looks_like_number($_) ? : 55 107 162
Condition66%2/3
$ref2 eq "SCALAR" and &Scalar::Util::looks_like_number($_)
!ll&&!rl&&r
010755
MC/DC50%1/2
$ref2 eq "SCALAR" and &Scalar::Util::looks_like_number($_)
$ref2 eq "SCALAR" &Scalar::Util::looks_like_number($_)
Truth table66%2/3
A and B
A= $ref2 eq "SCALAR"B= &Scalar::Util::looks_like_number($_)
ABresult
0X0
100
111
339 : $ref2 eq 'LVALUE' ? $self->lvalueify($_)
340 : $ref2 eq 'VSTRING' ? $self->vstringify($_)
341 : $ref2 eq 'SCALAR' && Scalar::Util::looks_like_number($_)
342 ? $self->numify($_)
343 : $self->stringify($_)
344 ;
345 }
346  
347 return
348 115 ▶ $ref eq 'ARRAY' ? $self->arrayify(@$_)
Branch: 11 branches
BranchTrueFalseTotal
$ref eq 'ARRAY' ? : 29 86 115
$ref eq 'CODE' ? : 3 83 86
$ref eq 'FORMAT' ? : 0 83 83
$ref eq 'GLOB' ? : 0 83 83
$ref eq 'HASH' ? : 11 72 83
$ref eq 'IO' ? : 0 72 72
$ref eq 'LVALUE' ? : 1 71 72
$ref eq 'REF' ? : 12 59 71
$ref eq 'REGEXP' ? : 0 59 59
$ref eq 'SCALAR' ? : 57 2 59
$ref eq 'VSTRING' ? : 2 0 2
349 : $ref eq 'CODE' ? $self->codeify($_)
350 : $ref eq 'FORMAT' ? $self->formatify($_)
351 : $ref eq 'GLOB' ? $self->globify($$_)
352 : $ref eq 'HASH' ? $self->hashify($_)
353 : $ref eq 'IO' ? $self->ioify($_)
354 : $ref eq 'LVALUE' ? $self->booleanify($$_)
355 : $ref eq 'REF' ? $self->refify($$_)
356 : $ref eq 'REGEXP' ? $self->regexpify($_) # ???
357 : $ref eq 'SCALAR' ? $self->booleanify($$_)
358 : $ref eq 'VSTRING' ? $self->booleanify($$_)
359 : $self->objectify($_) # ???
360 ;
361 }
362  
363  
364  
365 # TODO
366 sub decode;
367  
368  
369  
370 sub encode {
371 53 ▶ my $self = &Datify::self;
Subroutine encode: called CC=3 SCAR=11.2
Pod documented
372 53 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 53 53
373  
374 53 my @return = map { $self->scalarify($_) } @_;
375  
376 51 $self->_cache_reset();
377  
378 51 ▶ return @_ == 1 ? $return[0] : @return;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 51 0 51
379 }
380  
381  
382  
383 sub boolean {
384 11 ▶ &Datify::class;
Subroutine boolean: called CC=2 SCAR=6.9
Pod documented
385 11 ▶ return @_ ? Jasonify::Boolean::bool( $_[-1] ) : 'Jasonify::Boolean';
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 10 1 11
386 }
387 2 *bool = \&boolean;
388  
389  
390  
391 sub literal {
392 10 ▶ &Datify::class;
Subroutine literal: called CC=2 SCAR=6.9
Pod documented
393 10 ▶ return @_ ? Jasonify::Literal->new( $_[-1] ) : 'Jasonify::Literal';
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 9 1 10
394 }
395  
396  
397  
398 sub number {
399 13 ▶ &Datify::class;
Subroutine number: called CC=3 SCAR=11.0
Pod documented
400 13 my $count = scalar @_;
401 return
402 13 ▶ $count >= 2 ? Jasonify::Number->formatted(@_)
Branch: 2 branches
BranchTrueFalseTotal
$count >= 2 ? : 8 5 13
$count == 1 ? : 4 1 5
403 : $count == 1 ? Jasonify::Number->number(shift)
404 : 'Jasonify::Number'
405 ;
406 }
407  
408  
409  
410 sub string {
411 5 ▶ &Datify::class;
Subroutine string: called CC=2 SCAR=6.9
Pod documented
412 5 ▶ return @_ ? Jasonify::Literal->string( $_[-1] ) : 'Jasonify::Literal';
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 4 1 5
413 }
414  
415 ### Private Methods & Settings ###
416 ### Do not use these methods & settings outside of this package,
417 ### they are subject to change or disappear at any time.
418 3050 ▶sub _settings() { \state %SETTINGS }
Subroutine _settings: called CC=1 SCAR=0.0
419  
420 2 __PACKAGE__->set(
421 _cache_hit => 1, # Sets the caching to use the final representation
422 # or die if that doesn't exist
423 );
424  
425  
426 package
427 Jasonify::Literal;
428  
429 2 ▶use Scalar::Util (); #qw( looks_like_number );
Subroutine BEGIN: called CC=1 SCAR=0.0
430  
431 use overload
432 2 'bool' => 'bool',
433 '""' => 'as_string',
434 2 ▶ ;
Subroutine BEGIN: called CC=1 SCAR=0.0
435  
436 2 our $null = bless \do { my $null = Jasonify->get('null') }, __PACKAGE__;
437 2 our $false = bless \do { my $false = Jasonify->get('false') }, __PACKAGE__;
438 2 our $true = bless \do { my $true = Jasonify->get('true') }, __PACKAGE__;
439  
440 13 ▶sub Jasonify::jasonify_literalify { $_[1]->as_string }
Subroutine jasonify_literalify: called CC=1 SCAR=0.0
Pod undocumented
441 # OR
442 #Jasonify->add_handler( sub { $_[1]->as_string } );
443  
444 0 ▶sub null() { $null }
Subroutine null: not called CC=1 SCAR=6.9
445 0 ▶sub false() { $false }
Subroutine false: not called CC=1 SCAR=6.9
446 0 ▶sub true() { $true }
Subroutine true: not called CC=1 SCAR=6.9
447  
448 sub new {
449 26 ▶ my $class = &Datify::class;
Subroutine new: called CC=3 SCAR=11.3
450 26 my $literal = shift;
451 26 ▶ return $null unless defined($literal);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $literal 0 26 26
452 26 ▶ return $false unless length( $literal);
Branch: 1 branch
BranchTrueFalseTotal
unless length $literal 0 26 26
453 26 return bless \$literal, $class;
454 }
455 sub string {
456 4 ▶ @_ = ( shift, Jasonify->stringify(@_) );
Subroutine string: called CC=1 SCAR=0.0
457 4 goto &new;
458 }
459 #sub comment {
460 # $_[0]->new(
461 # "# " . join( "\n# ", map { split /\n/ } @_[ 1 .. $#_ ] ) . "\n" );
462 #}
463  
464 180 ▶sub as_string { ${ $_[0] } }
Subroutine as_string: called CC=1 SCAR=0.0
465 sub bool {
466 25 ▶ my $literal = ${ $_[0] };
Subroutine bool: called CC=6 SCAR=17.9
467 return
468 25 ▶ $literal ne $$null
Condition100%15/15
$literal ne $$Jasonify::Literal::null && $literal ne $$Jasonify::Literal::false && $literal ne '""' && $literal ne '"0"' && !(&Scalar::Util::looks_like_number($literal) && $literal == 0)
!ll&&!rl&&r
6415
$literal ne $$Jasonify::Literal::null and $literal ne $$Jasonify::Literal::false and $literal ne "\"\"" and $literal ne "\"0\""
!ll&&!rl&&r
4219
$literal ne $$Jasonify::Literal::null and $literal ne $$Jasonify::Literal::false and $literal ne "\"\""
!ll&&!rl&&r
2221
$literal ne $$Jasonify::Literal::null and $literal ne $$Jasonify::Literal::false
!ll&&!rl&&r
1123
&Scalar::Util::looks_like_number($literal) && $literal == 0
!ll&&!rl&&r
5104
MC/DC100%6/6
$literal ne $$Jasonify::Literal::null && $literal ne $$Jasonify::Literal::false && $literal ne '""' && $literal ne '"0"' && !(&Scalar::Util::looks_like_number($literal) && $literal == 0)
$literal ne $$Jasonify::Literal::null $literal ne $$Jasonify::Literal::false $literal ne "\"\"" $literal ne "\"0\"" &Scalar::Util::looks_like_number($literal) $literal == 0
Truth table100%7/7
A and B and C and D && not(E && F)
A= $literal ne $$Jasonify::Literal::nullB= $literal ne $$Jasonify::Literal::falseC= $literal ne "\"\""D= $literal ne "\"0\""E= &Scalar::Util::looks_like_number($literal)F= $literal == 0
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
1111110
11110X1
1111101
469 && $literal ne $$false
470 && $literal ne '""'
471 && $literal ne '"0"'
472 && !( Scalar::Util::looks_like_number($literal) && $literal == 0 );
473 }
474  
475 package
476 Jasonify::Number;
477  
478 2 ▶use Scalar::Util (); #qw( looks_like_number );
Subroutine BEGIN: called CC=1 SCAR=0.0
479  
480 use overload
481 2 '0+' => 'as_num',
482 'neg' => 'negate',
483  
484 '<=>' => 'comparen',
485 'cmp' => 'compares',
486 2 ▶ ;
Subroutine BEGIN: called CC=1 SCAR=0.0
487 2 ▶use parent -norequire => 'Jasonify::Literal';
Subroutine BEGIN: called CC=1 SCAR=0.0
488  
489 2 our $nan = bless \do { my $nan = Jasonify->get('nonnumber') }, __PACKAGE__;
490 2 our $inf = bless \do { my $inf = Jasonify->get( 'infinite') }, __PACKAGE__;
491 2 our $ninf = bless \do { my $ninf = Jasonify->get('-infinite') }, __PACKAGE__;
492  
493 20 ▶sub Jasonify::jasonify_numberify { $_[1]->as_string }
Subroutine jasonify_numberify: called CC=1 SCAR=0.0
Pod undocumented
494 # OR
495 #Jasonify->add_handler( sub { $_[1]->as_string } );
496  
497 0 ▶sub nan() { $nan }
Subroutine nan: not called CC=1 SCAR=6.9
498 0 ▶sub inf() { $inf }
Subroutine inf: not called CC=1 SCAR=6.9
499 0 ▶sub ninf() { $ninf }
Subroutine ninf: not called CC=1 SCAR=6.9
500  
501 2 my $number_regex = do {
502 2 my $digit09 = '[0123456789]';
503 2 my $digit19 = '[123456789]';
504 2 my $integer = "(?:0|$digit19+$digit09*)";
505 2 my $decimal = "(?:\.$digit09+)";
506 2 qr/-?$integer$decimal?(?:[Ee][+-]?$integer)?/;
507 };
508  
509 0 ▶sub comparen { ( $_[2] ? -1 : +1 ) * ( $_[0]->as_num <=> $_[1] ) }
Subroutine comparen: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 0 0
510 0 ▶sub compares { ( $_[2] ? -1 : +1 ) * ( ${ $_[0] } cmp $_[1] ) }
Subroutine compares: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 0 0
511 0 ▶sub as_num { eval ${ $_[0] } }
Subroutine as_num: not called CC=1 SCAR=6.9
512 sub negate {
513 0 ▶ my $num = ${ $_[0] };
Subroutine negate: not called CC=5 SCAR=34.0
514 return
515 0 ▶ $num eq $$nan ? $nan
Branch: 3 branches
BranchTrueFalseTotal
$num eq $$Jasonify::Number::nan ? : 0 0 0
$num eq $$Jasonify::Number::inf ? : 0 0 0
$num eq $$Jasonify::Number::ninf ? : 0 0 0
516 : $num eq $$inf ? $ninf
517 : $num eq $$ninf ? $inf
518 0 ▶ : $_[0]->number( $num =~ s/\A(-?)/$1 ? '' : '-'/er )
Branch: 1 branch
BranchTrueFalseTotal
$1 ? : 0 0 0
519 ;
520 }
521 sub number {
522 13 ▶ my $class = &Datify::class;
Subroutine number: called CC=6 SCAR=20.3
523 13 my $num = shift;
524 13 ▶ Carp::croak( "Not a number ", $num )
Branch: 1 branch
BranchTrueFalseTotal
unless &Scalar::Util::looks_like_number($num) 0 13 13
525 unless ( Scalar::Util::looks_like_number($num) );
526  
527 return
528 13 ▶ not( defined( $num <=> 0 ) ) ? $nan
Branch: 4 branches
BranchTrueFalseTotal
!defined($num <=> 0) ? : 0 13 13
$num == 9**9**9 ? : 0 13 13
$num == -9**9**9 ? : 0 13 13
$num =~ /\A$number_regex\z/u ? : 13 0 13
529 : $num == 'Infinity' ? $inf
530 : $num == '-Infinity' ? $ninf
531 : $num =~ /\A$number_regex\z/ ? $class->new($num)
532 : Carp::croak( "Malformed number ", $num );
533 }
534  
535 9 ▶sub formatted { return shift()->number( sprintf( shift(), @_ ) ) }
Subroutine formatted: called CC=1 SCAR=0.0
536 0 ▶sub integer { return shift()->formatted( '%d', shift() ) }
Subroutine integer: not called CC=1 SCAR=6.9
537 0 ▶sub float { return shift()->formatted( '%f', shift() ) }
Subroutine float: not called CC=1 SCAR=6.9
538  
539 package
540 Jasonify::Boolean;
541  
542 2 ▶use Scalar::Util (); #qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
543  
544 use overload
545 2 'bool' => 'value',
546 '0+' => 'value',
547 '""' => 'as_string',
548  
549 '<=>' => 'compare',
550 'cmp' => 'compare',
551  
552 '!' => 'negate',
553 2 ▶ ;
Subroutine BEGIN: called CC=1 SCAR=0.0
554  
555 2 our $false = bless \do { my $false = 0 }, __PACKAGE__;
556 2 our $true = bless \do { my $true = 1 }, __PACKAGE__;
557  
558 20 ▶sub Jasonify::jasonify_booleanify { $_[1]->as_string }
Subroutine jasonify_booleanify: called CC=1 SCAR=0.0
Pod undocumented
559 # OR
560 #Jasonify->add_handler( sub { $_[1]->as_string } );
561  
562 2 ▶sub false() { $false }
Subroutine false: called CC=1 SCAR=0.0
563 2 ▶sub true() { $true }
Subroutine true: called CC=1 SCAR=0.0
564  
565 34 ▶sub value { ${ $_[0] } }
Subroutine value: called CC=1 SCAR=0.0
566 sub as_string {
567 93 ▶ ${ $_[0] } ? $Jasonify::Literal::true : $Jasonify::Literal::false;
Subroutine as_string: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
${$_[0];} ? : 56 37 93
568 }
569  
570 44 ▶sub compare { ( $_[2] ? -1 : +1 ) * ( ${ $_[0] } <=> ${ bool( $_[1] ) } ) }
Subroutine compare: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 44 44
571  
572 2 ▶sub negate { bool($_[0]) ? $false : $true }
Subroutine negate: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
&bool($_[0]) ? : 1 1 2
573  
574 sub bool($) {
575 is_bool( $_[0] )
576 ? $_[0]
577 : ref( $_[0] ) eq 'SCALAR'
578 56 ▶ ? ${ $_[0] } ? $true : $false
Subroutine bool: called CC=5 SCAR=16.1
Branch: 4 branches
BranchTrueFalseTotal
&is_bool($_[0]) ? : 10 46 56
ref $_[0] eq 'SCALAR' ? : 21 25 46
${$_[0];} ? : 10 11 21
$_[0] ? : 10 15 25
579 : $_[0] ? $true : $false
580 ;
581 }
582 56 ▶sub is_bool($) { Scalar::Util::blessed( $_[0] ) && $_[0]->isa(__PACKAGE__) }
Subroutine is_bool: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__) 10 46 56
583  
584 2 1;
585  
586 __END__
↑ Summary