| 1 | package Data::ENAML; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Carp; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 1 | require Exporter; | |||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | 1 | @ISA = qw(Exporter); | |||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | # Items to export into callers namespace by default. Note: do not export | ||||||||||||||||||||||||||||||||
| 11 | # names by default without a very good reason. Use EXPORT_OK instead. | ||||||||||||||||||||||||||||||||
| 12 | # Do not simply export all your public functions/methods/constants. | ||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | # This allows declaration use Data::ENAML ':all'; | ||||||||||||||||||||||||||||||||
| 15 | # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK | ||||||||||||||||||||||||||||||||
| 16 | # will save memory. | ||||||||||||||||||||||||||||||||
| 17 | 1 | %EXPORT_TAGS = ( 'all' => [ qw( | |||||||||||||||||||||||||||||||
| 18 | serialize deserialize | ||||||||||||||||||||||||||||||||
| 19 | ) ] ); | ||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | 1 | @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | |||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | 1 | @EXPORT = qw( | |||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||||||||||||||
| 26 | 1 | $VERSION = '0.03'; | |||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | # Preloaded methods go here. | ||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||
| 31 | # Autoload methods go after =cut, and are processed by the autosplit program. | ||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||
| 33 | 2 | ▶ | sub serialize { | ||||||||||||||||||||||||||||||
|
Subroutine
serialize: called CC=6 SCAR=22.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 34 | 2 | my @pair; | |||||||||||||||||||||||||||||||
| 35 | 2 | ▶ | if (@_ == 1 && UNIVERSAL::isa($_[0], 'HASH')) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
@_ == 1 and UNIVERSAL::isa($_[0], "HASH")
MC/DC0%0/2
@_ == 1 and UNIVERSAL::isa($_[0], "HASH")
@_ == 1
UNIVERSAL::isa($_[0], "HASH")
Truth table33%1/3
A and B
A= @_ == 1B= UNIVERSAL::isa($_[0], "HASH")
| |||||||||||||||||||||||||||||||||
| 36 | 2 | my %hash = %{shift()}; | |||||||||||||||||||||||||||||||
| 37 | 2 | my @keys = keys %hash; | |||||||||||||||||||||||||||||||
| 38 | 2 | ▶ | if (scalar(@keys) != 1) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 39 | 0 | croak "serialized must be called with a key:value pair"; | |||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||
| 41 | 2 | @pair = %hash; | |||||||||||||||||||||||||||||||
| 42 | } elsif (@_ == 2) { | ||||||||||||||||||||||||||||||||
| 43 | 0 | @pair = @_; | |||||||||||||||||||||||||||||||
| 44 | } else { | ||||||||||||||||||||||||||||||||
| 45 | 0 | croak "serialized must be called with a key:value pair"; | |||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||
| 47 | 2 | ▶ | unless ($pair[0] =~ /[a-z_-]{1,32}/i) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 48 | 0 | croak "$pair[0] is an illegal key name"; | |||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||
| 50 | 2 | "$pair[0]: " . &do_serialize($pair[1], {}) . "\r\n"; | |||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||
| 53 | 8 | ▶ | sub do_serialize { | ||||||||||||||||||||||||||||||
|
Subroutine
do_serialize: called CC=15 SCAR=36.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 54 | 8 | my ($datum, $history) = @_; | |||||||||||||||||||||||||||||||
| 55 | 8 | ▶ | unless (ref($datum)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 56 | 6 | ▶ | return $datum if ($datum =~ /^\d+$/); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 57 | 6 | ▶ | unless ($datum =~ /[\000-\037"%\x7F\xFF]/) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 58 | 6 | return qq!"$datum"!; # TO DO: add UTF8 support | |||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||
| 60 | 0 | my $opt1 = $datum; | |||||||||||||||||||||||||||||||
| 61 | 0 | $opt1 =~ s/([\000-\037"%\x7F\xFF])/sprintf("%%%02X", ord($1))/ge; | |||||||||||||||||||||||||||||||
| 62 | 0 | $opt1 = qq!"$opt1"!; | |||||||||||||||||||||||||||||||
| 63 | 0 | my $opt2 = '%' . join("", map {sprintf("%02X", $_);} | |||||||||||||||||||||||||||||||
| 64 | unpack("C*", $datum)); | ||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | return (length($opt1) <= length($opt2)) ? $opt1 : $opt2; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||
| 67 | 2 | ▶ | croak "Circular referencing detected" if ++$history->{$datum} > 1; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 68 | 2 | ▶ | if (UNIVERSAL::isa($datum, 'HASH')) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 69 | 2 | my $str = "{ "; | |||||||||||||||||||||||||||||||
| 70 | 2 | my $count = 0; | |||||||||||||||||||||||||||||||
| 71 | 2 | ▶ | while (my ($key, $val) = each %$datum) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 72 | 8 | ▶ | unless ($key =~ /[a-z_-]{1,32}/i) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 73 | 0 | croak "$datum is an illegal key name"; | |||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||
| 75 | 8 | ▶ | $str .= " " if (++$count > 1); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 76 | 8 | ▶ | if ($val eq "\000" || !defined($val)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$val eq "\0" or not defined $val
MC/DC50%1/2
$val eq "\0" or not defined $val
$val eq "\0"
not defined $val
Truth table66%2/3
A or not(B)
A= $val eq "\0"B= not defined $val
| |||||||||||||||||||||||||||||||||
| 77 | 2 | $str .= $key; | |||||||||||||||||||||||||||||||
| 78 | 2 | next; | |||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||
| 80 | 6 | $str .= "$key: " . &do_serialize($val); | |||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||
| 82 | 2 | $str .= " }"; | |||||||||||||||||||||||||||||||
| 83 | 2 | return $str; | |||||||||||||||||||||||||||||||
| 84 | } elsif (UNIVERSAL::isa($datum, 'ARRAY')) { | ||||||||||||||||||||||||||||||||
| 85 | 0 | my $str = "[ "; | |||||||||||||||||||||||||||||||
| 86 | 0 | my $count = 0; | |||||||||||||||||||||||||||||||
| 87 | 0 | foreach (@$datum) { | |||||||||||||||||||||||||||||||
| 88 | 0 | ▶ | $str .= " " if (++$count > 1); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 89 | 0 | $str .= &do_serialize($_); | |||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||
| 91 | 0 | $str .= " ]"; | |||||||||||||||||||||||||||||||
| 92 | 0 | return $str; | |||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||
| 94 | 0 | croak "Object type " . ref($datum) . " not supported"; | |||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||
| 97 | 1 | ▶ | sub deserialize { | ||||||||||||||||||||||||||||||
|
Subroutine
deserialize: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 98 | 1 | my $text = shift; | |||||||||||||||||||||||||||||||
| 99 | 1 | $text =~ s/[\r\n]+$//; | |||||||||||||||||||||||||||||||
| 100 | 1 | my ($hash, $rem) = &deserialize_hash("$text }"); | |||||||||||||||||||||||||||||||
| 101 | 1 | ▶ | croak "Ended at $rem" if ($rem); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 102 | 1 | $hash; | |||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||
| 105 | 2 | ▶ | sub deserialize_hash { | ||||||||||||||||||||||||||||||
|
Subroutine
deserialize_hash: called CC=5 SCAR=16.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 106 | 2 | local ($_) = shift; | |||||||||||||||||||||||||||||||
| 107 | 2 | my $struct = {}; | |||||||||||||||||||||||||||||||
| 108 | 2 | ▶ | while ($_) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 109 | 7 | s/^\s+//; | |||||||||||||||||||||||||||||||
| 110 | 7 | ▶ | if (s/^\}//) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 111 | 2 | return ($struct, $_); | |||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||
| 113 | 5 | ▶ | unless (s/^([A-Za-z-_]{1,32})(:?)\s*//) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 114 | 0 | croak "Expected: key, at $_"; | |||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||
| 116 | 5 | my $key = $1; | |||||||||||||||||||||||||||||||
| 117 | 5 | ▶ | unless ($2) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 118 | 1 | $struct->{$key} = undef; | |||||||||||||||||||||||||||||||
| 119 | 1 | next; | |||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||
| 121 | 4 | ($struct->{$key}, $_) = &eat_one($_); | |||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||
| 123 | 0 | croak "expected }"; | |||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||
| 126 | 0 | ▶ | sub deserialize_array { | ||||||||||||||||||||||||||||||
|
Subroutine
deserialize_array: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 127 | 0 | local ($_) = shift; | |||||||||||||||||||||||||||||||
| 128 | 0 | my $array = []; | |||||||||||||||||||||||||||||||
| 129 | 0 | ▶ | while ($_) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 130 | 0 | s/^\s+//; | |||||||||||||||||||||||||||||||
| 131 | 0 | ▶ | if (s/^\]//) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 132 | 0 | return ($array, $_); | |||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||
| 134 | 0 | my ($elem, $rem) = &eat_one($_); | |||||||||||||||||||||||||||||||
| 135 | 0 | $_ = $rem; | |||||||||||||||||||||||||||||||
| 136 | 0 | push(@$array, $elem); | |||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||
| 138 | 0 | croak "expected ]"; | |||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||
| 141 | 4 | ▶ | sub eat_one { | ||||||||||||||||||||||||||||||
|
Subroutine
eat_one: called CC=7 SCAR=27.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 142 | 4 | local ($_) = shift; | |||||||||||||||||||||||||||||||
| 143 | 4 | ▶ | if (s/^\{//) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 144 | 1 | my ($hash, $text) = &deserialize_hash($_); | |||||||||||||||||||||||||||||||
| 145 | 1 | return ($hash, $text); | |||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||
| 147 | 3 | ▶ | if (s/^\[//) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 148 | 0 | my ($ary, $text) = &deserialize_array($_); | |||||||||||||||||||||||||||||||
| 149 | 0 | return ($ary, $text); | |||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||
| 151 | 3 | ▶ | if (s/^(\d+)(?![^}\]\t ])//) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 152 | 0 | return($1, $_); | |||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||
| 154 | 3 | ▶ | if (s/^\"(.*?)\"//) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 155 | 3 | my $text = $1; | |||||||||||||||||||||||||||||||
| 156 | 0 | $text =~ s/%([0-9A-F]{2})/chr(hex($1))/gei; | |||||||||||||||||||||||||||||||
| 157 | 3 | return ($text, $_); | |||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||
| 159 | 0 | ▶ | if (s/^%([0-9A-F]+)//i) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 160 | 0 | my $hex = $1; | |||||||||||||||||||||||||||||||
| 161 | 0 | ▶ | croak "Odd number of hex digits" if (length($hex) % 2); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 162 | 0 | my @tokens = ($hex =~ /(..)/g); | |||||||||||||||||||||||||||||||
| 163 | 0 | my $str = pack("C*", map {hex($_);} @tokens); | |||||||||||||||||||||||||||||||
| 164 | 0 | return ($str, $_); | |||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||
| 166 | 0 | croak "Could not get token at $_"; | |||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||
| 169 | 1 | 1; | |||||||||||||||||||||||||||||||
| 170 | __END__ | ||||||||||||||||||||||||||||||||