| 1 | package TOML; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # ------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 4 | # TOML - Parser for Tom's Obvious, Minimal Language. | ||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||
| 6 | # Copyright (C) 2013 Darren Chamberlain <darren@cpan.org> | ||||||||||||||||||||||||||||
| 7 | # ------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use 5.008005; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 2 | our ($VERSION, @EXPORT, @_NAMESPACE, $PARSER); | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 2 | ▶ | use B; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 2 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 2 | ▶ | use TOML::Parser 0.03; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 2 | $VERSION = "0.96"; | |||||||||||||||||||||||||||
| 21 | 2 | @EXPORT = qw(from_toml to_toml); | |||||||||||||||||||||||||||
| 22 | 0 | ▶ | $PARSER = TOML::Parser->new(inflate_boolean => sub { $_[0] }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | sub to_toml { | ||||||||||||||||||||||||||||
| 25 | 13 | ▶ | my $stuff = shift; | ||||||||||||||||||||||||||
|
Subroutine
to_toml: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 26 | 13 | local @_NAMESPACE = (); | |||||||||||||||||||||||||||
| 27 | 13 | _to_toml($stuff); | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | sub _to_toml { | ||||||||||||||||||||||||||||
| 31 | 25 | ▶ | my ($stuff) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_to_toml: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 25 | ▶ | if (ref $stuff eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 25 | my $res = ''; | |||||||||||||||||||||||||||
| 35 | 25 | my @keys = sort keys %$stuff; | |||||||||||||||||||||||||||
| 36 | 25 | for my $key (grep { ref $stuff->{$_} ne 'HASH' } @keys) { | |||||||||||||||||||||||||||
| 37 | 30 | my $val = $stuff->{$key}; | |||||||||||||||||||||||||||
| 38 | 30 | $res .= "$key = " . _serialize($val) . "\n"; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | 25 | for my $key (grep { ref $stuff->{$_} eq 'HASH' } @keys) { | |||||||||||||||||||||||||||
| 41 | 12 | my $val = $stuff->{$key}; | |||||||||||||||||||||||||||
| 42 | 12 | local @_NAMESPACE = (@_NAMESPACE, $key); | |||||||||||||||||||||||||||
| 43 | 12 | $res .= sprintf("[%s]\n", join(".", @_NAMESPACE)); | |||||||||||||||||||||||||||
| 44 | 12 | $res .= _to_toml($val); | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | 25 | return $res; | |||||||||||||||||||||||||||
| 47 | } else { | ||||||||||||||||||||||||||||
| 48 | 0 | croak("You cannot convert non-HashRef values to TOML"); | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | sub _serialize { | ||||||||||||||||||||||||||||
| 53 | 45 | ▶ | my $value = shift; | ||||||||||||||||||||||||||
|
Subroutine
_serialize: called CC=9 SCAR=22.8
| |||||||||||||||||||||||||||||
| 54 | 45 | my $b_obj = B::svref_2object(\$value); | |||||||||||||||||||||||||||
| 55 | 45 | my $flags = $b_obj->FLAGS; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 45 | ▶ | return $value | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$flags & 12288 and not $flags & 16384
MC/DC50%1/2
$flags & 12288 and not $flags & 16384
$flags & 12288
not $flags & 16384
Truth table66%2/3
A and not(B)
A= $flags & 12288B= not $flags & 16384
| |||||||||||||||||||||||||||||
| 58 | if $flags & ( B::SVp_IOK | B::SVp_NOK ) and !( $flags & B::SVp_POK ); # SvTYPE is IV or NV? | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 39 | my $type = ref($value); | |||||||||||||||||||||||||||
| 61 | 39 | ▶ | if (!$type) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 62 | 31 | return string_to_json($value); | |||||||||||||||||||||||||||
| 63 | } elsif ($type eq 'ARRAY') { | ||||||||||||||||||||||||||||
| 64 | 6 | return sprintf('[%s]', join(", ", map { _serialize($_) } @$value)); | |||||||||||||||||||||||||||
| 65 | } elsif ($type eq 'SCALAR') { | ||||||||||||||||||||||||||||
| 66 | 2 | ▶ | if (defined $$value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 2 | ▶ | if ($$value eq '0') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 68 | 1 | return 'false'; | |||||||||||||||||||||||||||
| 69 | } elsif ($$value eq '1') { | ||||||||||||||||||||||||||||
| 70 | 1 | return 'true'; | |||||||||||||||||||||||||||
| 71 | } else { | ||||||||||||||||||||||||||||
| 72 | 0 | croak("cannot encode reference to scalar"); | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | 0 | croak("cannot encode reference to scalar"); | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | 0 | croak("Bad type in to_toml: $type"); | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 2 | my %esc = ( | |||||||||||||||||||||||||||
| 81 | "\n" => '\n', | ||||||||||||||||||||||||||||
| 82 | "\r" => '\r', | ||||||||||||||||||||||||||||
| 83 | "\t" => '\t', | ||||||||||||||||||||||||||||
| 84 | "\f" => '\f', | ||||||||||||||||||||||||||||
| 85 | "\b" => '\b', | ||||||||||||||||||||||||||||
| 86 | "\"" => '\"', | ||||||||||||||||||||||||||||
| 87 | "\\" => '\\\\', | ||||||||||||||||||||||||||||
| 88 | "\'" => '\\\'', | ||||||||||||||||||||||||||||
| 89 | ); | ||||||||||||||||||||||||||||
| 90 | sub string_to_json { | ||||||||||||||||||||||||||||
| 91 | 31 | ▶ | my ($arg) = @_; | ||||||||||||||||||||||||||
|
Subroutine
string_to_json: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 31 | $arg =~ s/([\x22\x5c\n\r\t\f\b])/$esc{$1}/g; | |||||||||||||||||||||||||||
| 94 | 31 | $arg =~ s/\//\\\//g if 1; | |||||||||||||||||||||||||||
| 95 | 31 | $arg =~ s/([\x00-\x08\x0b\x0e-\x1f])/'\\u00' . unpack('H2', $1)/eg; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 31 | return '"' . $arg . '"'; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | sub from_toml { | ||||||||||||||||||||||||||||
| 101 | 6 | ▶ | my $string = shift; | ||||||||||||||||||||||||||
|
Subroutine
from_toml: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 102 | 6 | local $@; | |||||||||||||||||||||||||||
| 103 | 6 | my $toml = eval { $PARSER->parse($string) }; | |||||||||||||||||||||||||||
| 104 | 6 | ▶ | return wantarray ? ($toml, $@) : $toml; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 2 | 1; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | __END__ | ||||||||||||||||||||||||||||