| 1 | package Python::Serialise::Marshal; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 9 | ▶ | use IO::File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 9 | ▶ | use File::Binary; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 9 | ▶ | use Math::Complex; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 9 | ▶ | use vars qw($VERSION); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 9 | $VERSION = '0.02'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 9 | my %_set_handlers = ( | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 'NONE' => \&_set_none, | ||||||||||||||||||||||||||||
| 15 | 'INTEGER' => \&_set_int, | ||||||||||||||||||||||||||||
| 16 | 'FLOAT' => \&_set_float, | ||||||||||||||||||||||||||||
| 17 | 'LONG' => \&_set_long, | ||||||||||||||||||||||||||||
| 18 | 'STRING' => \&_set_string, | ||||||||||||||||||||||||||||
| 19 | 'HASH' => \&_set_dict, | ||||||||||||||||||||||||||||
| 20 | 'ARRAY' => \&_set_mylist, | ||||||||||||||||||||||||||||
| 21 | 'COMPLEX' => \&_set_complex, | ||||||||||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 9 | my %_get_handlers = ( | |||||||||||||||||||||||||||
| 26 | 'N' => \&_get_none, | ||||||||||||||||||||||||||||
| 27 | 'i' => \&_get_int, | ||||||||||||||||||||||||||||
| 28 | # 'l' => \&_get_long, # long support is broken | ||||||||||||||||||||||||||||
| 29 | 'f' => \&_get_float, | ||||||||||||||||||||||||||||
| 30 | 's' => \&_get_string, | ||||||||||||||||||||||||||||
| 31 | '(' => \&_get_list, | ||||||||||||||||||||||||||||
| 32 | '[' => \&_get_list, | ||||||||||||||||||||||||||||
| 33 | '{' => \&_get_dict, | ||||||||||||||||||||||||||||
| 34 | 'x' => \&_get_complex, | ||||||||||||||||||||||||||||
| 35 | ); | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | =head1 NAME | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | Python::Serialise::Marshal - a module for reading and writing marshalled Python files | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | use Python::Serialise::Marshal; | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | my $pr = Python::Serialise::Marshal->new("file/for/reading"); | ||||||||||||||||||||||||||||
| 48 | while (my $data = $pr->load()) { | ||||||||||||||||||||||||||||
| 49 | print Dumper $data; | ||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | my $pw = Python::Serialise::Marshal->new(">file/for/writing"); | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | $pw->dump(['a' 'list']); | ||||||||||||||||||||||||||||
| 55 | $pw->dump("a string"); | ||||||||||||||||||||||||||||
| 56 | $pw->dump(42); | ||||||||||||||||||||||||||||
| 57 | $pw->dump({'a'=>'hash'}); | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | $pw->close(); | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | Marshalling is a method of serialising files in Python (another method, | ||||||||||||||||||||||||||||
| 64 | Pickling, is also available). It is the method that Mailman uses to store | ||||||||||||||||||||||||||||
| 65 | its config files. | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | This module is an attempt to write a pure Perl implementation of the algorithm. | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =head1 METHODS | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | =head2 new <filename> | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | Open a file for reading or writing. Can take any arguments that C<IO::File> can. | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | =cut | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 27 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||
| 79 | 27 | my $class = shift; | |||||||||||||||||||||||||||
| 80 | 27 | ▶ | my $file = shift || die "You must pass a file\n"; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || die("You must pass a file\n")
MC/DC0%0/1
shift() || die("You must pass a file\n")
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | ## FIXME error here | ||||||||||||||||||||||||||||
| 83 | 27 | ▶ | my $fh = File::Binary->new($file) || die "Couldn't open file\n"; | ||||||||||||||||||||||||||
|
Condition50%1/2
'File::Binary'->new($file) || die("Couldn't open file\n")
MC/DC0%0/1
'File::Binary'->new($file) || die("Couldn't open file\n")
'File::Binary'->new($file)
Truth table50%1/2
A
A= 'File::Binary'->new($file)
| |||||||||||||||||||||||||||||
| 84 | 27 | my $self = { _fh => $fh }; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 27 | $self->{_fh}->set_endian($File::Binary::BIG_ENDIAN); | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 27 | return bless $self, $class; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | =head2 load | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | Returns the next data structure from the marshalled file or undef. | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | =cut | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 150 | ▶ | sub load { | ||||||||||||||||||||||||||
|
Subroutine
load: called CC=4 SCAR=15.0
Pod
documented
| |||||||||||||||||||||||||||||
| 102 | 150 | my $self = shift; | |||||||||||||||||||||||||||
| 103 | 150 | $self->{_cur_id} = 0; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 150 | my $id = $self->_get_char(); | |||||||||||||||||||||||||||
| 107 | 150 | ▶ | return undef if (!defined $id or $id eq ""); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $id or $id eq ""
MC/DC0%0/2
not defined $id or $id eq ""
not defined $id
$id eq ""
Truth table33%1/3
not(A) or B
A= not defined $idB= $id eq ""
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 150 | ▶ | my $sub = $_get_handlers{$id} || die "We have no handler to deal with '$id'\n"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$_get_handlers{$id} || die("We have no handler to deal with '${id}'\n")
MC/DC0%0/1
$_get_handlers{$id} || die("We have no handler to deal with '${id}'\n")
$_get_handlers{$id}
Truth table50%1/2
A
A= $_get_handlers{$id}
| |||||||||||||||||||||||||||||
| 110 | 150 | return $self->$sub(); | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =head2 dump <data structure> | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | Takes a ref to an array or a hash or a number or string and pickles it. | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | Structures may be nested. | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =cut | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 30 | ▶ | sub dump { | ||||||||||||||||||||||||||
|
Subroutine
dump: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 125 | 30 | my $self = shift; | |||||||||||||||||||||||||||
| 126 | 30 | my $val = shift; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 30 | my $type = $self->_type($val); | |||||||||||||||||||||||||||
| 129 | 30 | ▶ | my $sub = $_set_handlers{$type} || die "We have no handler for value '$val' of type $type"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$_set_handlers{$type} || die("We have no handler for value '${val}' of type $type")
MC/DC0%0/1
$_set_handlers{$type} || die("We have no handler for value '${val}' of type $type")
$_set_handlers{$type}
Truth table50%1/2
A
A= $_set_handlers{$type}
| |||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 30 | return $self->$sub($val); | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | #$self->_write($line); | ||||||||||||||||||||||||||||
| 134 | #return $line; | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 0 | ▶ | sub _write { | ||||||||||||||||||||||||||
|
Subroutine
_write: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 139 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 140 | 0 | my $line = shift; | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 0 | $self->{_fh}->put_bytes($line); | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | sub _get_char | ||||||||||||||||||||||||||||
| 147 | 200 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_get_char: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 148 | 200 | my $self = shift; | |||||||||||||||||||||||||||
| 149 | 200 | return $self->{_fh}->get_bytes(1); | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | sub _put_char | ||||||||||||||||||||||||||||
| 154 | 100 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_put_char: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 155 | 100 | my $self = shift; | |||||||||||||||||||||||||||
| 156 | 100 | my $char = shift; | |||||||||||||||||||||||||||
| 157 | 100 | $self->{_fh}->put_bytes($char); | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 72 | ▶ | sub _get_int { | ||||||||||||||||||||||||||
|
Subroutine
_get_int: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 162 | 72 | my $self = shift; | |||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 72 | return $self->{_fh}->get_si32(); | |||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 36 | ▶ | sub _set_int { | ||||||||||||||||||||||||||
|
Subroutine
_set_int: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 170 | 36 | my $self = shift; | |||||||||||||||||||||||||||
| 171 | 36 | my $val = shift; | |||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 36 | $self->_put_char('i'); | |||||||||||||||||||||||||||
| 174 | 36 | $self->{_fh}->put_si32($val); | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 0 | ▶ | sub _get_long { | ||||||||||||||||||||||||||
|
Subroutine
_get_long: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 178 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 0 | return 0; | |||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 8 | ▶ | sub _get_float { | ||||||||||||||||||||||||||
|
Subroutine
_get_float: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 186 | 8 | my $self = shift; | |||||||||||||||||||||||||||
| 187 | 8 | my $size = $self->{_fh}->get_ui8(); | |||||||||||||||||||||||||||
| 188 | 8 | return $self->{_fh}->get_bytes($size); | |||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 4 | ▶ | sub _set_float { | ||||||||||||||||||||||||||
|
Subroutine
_set_float: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 193 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 194 | 4 | my $val = shift; | |||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 4 | $self->_put_char('f'); | |||||||||||||||||||||||||||
| 197 | 4 | $self->{_fh}->put_ui8(length($val)); | |||||||||||||||||||||||||||
| 198 | 4 | $self->{_fh}->put_bytes($val); | |||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 74 | ▶ | sub _get_string { | ||||||||||||||||||||||||||
|
Subroutine
_get_string: called CC=2 SCAR=7.4
| |||||||||||||||||||||||||||||
| 203 | 74 | my $self = shift; | |||||||||||||||||||||||||||
| 204 | 74 | my $size = $self->{_fh}->get_ui32(); | |||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 74 | ▶ | if ($size>0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | 74 | return $self->{_fh}->get_bytes($size); | |||||||||||||||||||||||||||
| 208 | } else { | ||||||||||||||||||||||||||||
| 209 | 0 | return ""; | |||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | 37 | ▶ | sub _set_string { | ||||||||||||||||||||||||||
|
Subroutine
_set_string: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 214 | 37 | my $self = shift; | |||||||||||||||||||||||||||
| 215 | 37 | my $val = shift; | |||||||||||||||||||||||||||
| 216 | 37 | $self->_put_char('s'); | |||||||||||||||||||||||||||
| 217 | 37 | $self->{_fh}->put_ui32(length $val); | |||||||||||||||||||||||||||
| 218 | 37 | $self->{_fh}->put_bytes($val); | |||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | 24 | ▶ | sub _get_list { | ||||||||||||||||||||||||||
|
Subroutine
_get_list: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 222 | 24 | my $self = shift; | |||||||||||||||||||||||||||
| 223 | 24 | my $n = $self->{_fh}->get_ui32(); | |||||||||||||||||||||||||||
| 224 | 24 | my @return; | |||||||||||||||||||||||||||
| 225 | 24 | foreach (1..$n) { | |||||||||||||||||||||||||||
| 226 | 90 | push @return, $self->load(); | |||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | 24 | return \@return; | |||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 12 | ▶ | sub _set_mylist { | ||||||||||||||||||||||||||
|
Subroutine
_set_mylist: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 233 | 12 | my $self = shift; | |||||||||||||||||||||||||||
| 234 | 12 | my $arr = shift; | |||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | 12 | $self->_put_char('['); | |||||||||||||||||||||||||||
| 237 | 12 | $self->{_fh}->put_ui32(scalar @$arr); | |||||||||||||||||||||||||||
| 238 | 12 | foreach my $val (@$arr) { | |||||||||||||||||||||||||||
| 239 | 45 | my $type = $self->_type($val); | |||||||||||||||||||||||||||
| 240 | 45 | ▶ | my $sub = $_set_handlers{$type} || die "We have no handler for value '$val' of type '$type'\n"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$_set_handlers{$type} || die("We have no handler for value '${val}' of type '${type}'\n")
MC/DC0%0/1
$_set_handlers{$type} || die("We have no handler for value '${val}' of type '${type}'\n")
$_set_handlers{$type}
Truth table50%1/2
A
A= $_set_handlers{$type}
| |||||||||||||||||||||||||||||
| 241 | 45 | $self->$sub($val); | |||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||
| 243 | 12 | return 1; | |||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | 10 | ▶ | sub _get_dict { | ||||||||||||||||||||||||||
|
Subroutine
_get_dict: called CC=6 SCAR=18.4
| |||||||||||||||||||||||||||||
| 248 | 10 | my $self = shift; | |||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | 10 | my %hash; | |||||||||||||||||||||||||||
| 251 | 10 | while (1) { | |||||||||||||||||||||||||||
| 252 | 30 | my $id = $self->_get_char(); | |||||||||||||||||||||||||||
| 253 | 30 | ▶ | return undef if (!defined $id or $id eq ""); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $id or $id eq ""
MC/DC0%0/2
not defined $id or $id eq ""
not defined $id
$id eq ""
Truth table33%1/3
not(A) or B
A= not defined $idB= $id eq ""
| |||||||||||||||||||||||||||||
| 254 | 30 | ▶ | last if $id eq '0'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | 20 | ▶ | my $sub = $_get_handlers{$id} || die "We have no handler to deal wwith '$id'\n"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$_get_handlers{$id} || die("We have no handler to deal wwith '${id}'\n")
MC/DC0%0/1
$_get_handlers{$id} || die("We have no handler to deal wwith '${id}'\n")
$_get_handlers{$id}
Truth table50%1/2
A
A= $_get_handlers{$id}
| |||||||||||||||||||||||||||||
| 257 | 20 | my $key = $self->$sub(); | |||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | 20 | $id = $self->_get_char(); | |||||||||||||||||||||||||||
| 260 | 20 | ▶ | $sub = $_get_handlers{$id} || die "We have no handler to deal wwith '$id'\n"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$_get_handlers{$id} || die("We have no handler to deal wwith '${id}'\n")
MC/DC0%0/1
$_get_handlers{$id} || die("We have no handler to deal wwith '${id}'\n")
$_get_handlers{$id}
Truth table50%1/2
A
A= $_get_handlers{$id}
| |||||||||||||||||||||||||||||
| 261 | 20 | my $value = $self->$sub(); | |||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | 20 | $hash{$key} = $value; | |||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | 10 | return \%hash; | |||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | 5 | ▶ | sub _set_dict { | ||||||||||||||||||||||||||
|
Subroutine
_set_dict: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 272 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 273 | 5 | my $hash = shift; | |||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | 5 | $self->_put_char('{'); | |||||||||||||||||||||||||||
| 276 | 5 | foreach my $key (keys %$hash) { | |||||||||||||||||||||||||||
| 277 | 10 | my $ktype = $self->_type($key); | |||||||||||||||||||||||||||
| 278 | 10 | ▶ | my $ksub = $_set_handlers{$ktype} || die "We have no handler for key '$key' of type '$ktype'\n"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$_set_handlers{$ktype} || die("We have no handler for key '${key}' of type '${ktype}'\n")
MC/DC0%0/1
$_set_handlers{$ktype} || die("We have no handler for key '${key}' of type '${ktype}'\n")
$_set_handlers{$ktype}
Truth table50%1/2
A
A= $_set_handlers{$ktype}
| |||||||||||||||||||||||||||||
| 279 | 10 | $self->$ksub($key); | |||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | 10 | my $val = $hash->{$key}; | |||||||||||||||||||||||||||
| 282 | 10 | my $vtype = $self->_type($val); | |||||||||||||||||||||||||||
| 283 | 10 | ▶ | my $vsub = $_set_handlers{$vtype} || die "We have no handler for value '$val' of type '$vtype'\n"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$_set_handlers{$vtype} || die("We have no handler for value '${val}' of type '${vtype}'\n")
MC/DC0%0/1
$_set_handlers{$vtype} || die("We have no handler for value '${val}' of type '${vtype}'\n")
$_set_handlers{$vtype}
Truth table50%1/2
A
A= $_set_handlers{$vtype}
| |||||||||||||||||||||||||||||
| 284 | 10 | $self->$vsub($val); | |||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||
| 287 | 5 | $self->_put_char('0'); | |||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | 0 | ▶ | sub _get_none { | ||||||||||||||||||||||||||
|
Subroutine
_get_none: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 292 | 0 | return Python::Serialise::None->new(); | |||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | 0 | ▶ | sub _set_none { | ||||||||||||||||||||||||||
|
Subroutine
_set_none: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 296 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 297 | 0 | $self->_put_char('N'); | |||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | 2 | ▶ | sub _get_complex { | ||||||||||||||||||||||||||
|
Subroutine
_get_complex: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 301 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | 2 | my $rsize = $self->{_fh}->get_ui8(); | |||||||||||||||||||||||||||
| 304 | 2 | my $real = $self->{_fh}->get_bytes($rsize); | |||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | 2 | my $isize = $self->{_fh}->get_ui8(); | |||||||||||||||||||||||||||
| 307 | 2 | my $imag = $self->{_fh}->get_bytes($isize); | |||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | 2 | my $comp = Math::Complex->new($real, $imag); | |||||||||||||||||||||||||||
| 310 | 2 | $comp->display_format('cartesian'); | |||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | 2 | return $comp; | |||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | 1 | ▶ | sub _set_complex { | ||||||||||||||||||||||||||
|
Subroutine
_set_complex: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 316 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 317 | 1 | my $comp = shift; | |||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | 1 | my $real = Re($comp); | |||||||||||||||||||||||||||
| 320 | 1 | my $imag = Im($comp); | |||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | 1 | $self->_put_char('x'); | |||||||||||||||||||||||||||
| 323 | 1 | $self->{_fh}->put_ui8(length $real); | |||||||||||||||||||||||||||
| 324 | 1 | $self->{_fh}->put_bytes($real); | |||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | 1 | $self->{_fh}->put_ui8(length $imag); | |||||||||||||||||||||||||||
| 327 | 1 | $self->{_fh}->put_bytes($imag); | |||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | =head2 close | ||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | Closes the current file. | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | =cut | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | 27 | ▶ | sub close { | ||||||||||||||||||||||||||
|
Subroutine
close: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 338 | 27 | my $self = shift; | |||||||||||||||||||||||||||
| 339 | 27 | $self->{_fh}->close(); | |||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | 95 | ▶ | sub _type { | ||||||||||||||||||||||||||
|
Subroutine
_type: called CC=10 SCAR=23.7
| |||||||||||||||||||||||||||||
| 344 | 95 | my $self = shift; | |||||||||||||||||||||||||||
| 345 | 95 | my $val = shift; | |||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | 95 | ▶ | return "NONE" unless defined $val; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | 95 | my $ref = ref $val; | |||||||||||||||||||||||||||
| 350 | 95 | ▶ | return "NONE" if defined $ref and UNIVERSAL::isa($val,'Python::Serialise::None'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $ref and UNIVERSAL::isa($val, "Python::Serialise::None")
MC/DC0%0/2
defined $ref and UNIVERSAL::isa($val, "Python::Serialise::None")
defined $ref
UNIVERSAL::isa($val, "Python::Serialise::None")
Truth table33%1/3
A and B
A= defined $refB= UNIVERSAL::isa($val, "Python::Serialise::None")
| |||||||||||||||||||||||||||||
| 351 | 95 | ▶ | return "COMPLEX" if defined $ref and UNIVERSAL::isa($val,'Math::Complex'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $ref and UNIVERSAL::isa($val, "Math::Complex")
MC/DC50%1/2
defined $ref and UNIVERSAL::isa($val, "Math::Complex")
defined $ref
UNIVERSAL::isa($val, "Math::Complex")
Truth table66%2/3
A and B
A= defined $refB= UNIVERSAL::isa($val, "Math::Complex")
| |||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | 94 | ▶ | return $ref if defined $ref && $ref ne ""; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $ref and $ref ne ""
MC/DC50%1/2
defined $ref and $ref ne ""
defined $ref
$ref ne ""
Truth table66%2/3
A and B
A= defined $refB= $ref ne ""
| |||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | 77 | ▶ | return "INTEGER" if ($val =~ /^[+-]?\d+$/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 356 | 41 | ▶ | return "FLOAT" if ($val =~ /^-?(?:\d+(?:\.\d*)?|\.\d+)$/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 357 | 37 | return "STRING"; | |||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | } | ||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | =head1 NOTES | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | =head2 Complex numbers | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | Python has inbuilt support form complex numbers whilst Perl | ||||||||||||||||||||||||||||
| 366 | provides it through the core module C<Math::Complex>. Unserialising | ||||||||||||||||||||||||||||
| 367 | a Python complex number will return a C<Math::Complex> object | ||||||||||||||||||||||||||||
| 368 | and, as you'd expect, serialising something that ISA C<Math::Complex> | ||||||||||||||||||||||||||||
| 369 | will result in a serialised Python complex number. | ||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | =head2 None | ||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | Python has C<None> objects, similar to Perl's C<undef>. | ||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | Because I<load> indictaes "no more objects" by returning C<undef> | ||||||||||||||||||||||||||||
| 376 | we have to return C<Python::Serialise::None> objects. However dump can | ||||||||||||||||||||||||||||
| 377 | take C<undef> and serialise it as a C<None> object. | ||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | =cut | ||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | =head1 BUGS | ||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | Much less than my C<Pickle> module because this is a | ||||||||||||||||||||||||||||
| 384 | I<much> saner file format. | ||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | =over 4 | ||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | =item Tests for None | ||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | I can't think of a nice elegant way of doing tests at the moment. | ||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | I'm sure I will soon. | ||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | =item Longs | ||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||
| 396 | There's no support for longs. I've figured out how to write them in | ||||||||||||||||||||||||||||
| 397 | Python but I just can't seem to extract them properly. | ||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | =item Unicode | ||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | Not an itch that needs scratching at the moment so there's no support. | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | =item Code | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | Ditto | ||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | =back | ||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | =head1 ALTERNATIVES | ||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | You could always dump the data structure out as YAML in Python | ||||||||||||||||||||||||||||
| 412 | and then read it back in with YAML in Perl. | ||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | I also may look into wrapping the Python source code file in XS. | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | Simon Wistow <simon@thegestalt.org> | ||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | (c) 2003 Simon Wistow | ||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||
| 424 | Distributed under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | This software is under no warranty and will probably ruin your life, | ||||||||||||||||||||||||||||
| 427 | kill your friends, burn your house and bring about the apocalypse. | ||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | http://www.python.org, L<YAML>, L<File::Binary>, L<Math::Complex> | ||||||||||||||||||||||||||||
| 432 | and the RESOURCES file in this distribution. | ||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | =cut | ||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | package Python::Serialise::None; | ||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 441 | 0 | my $class = shift; | |||||||||||||||||||||||||||
| 442 | 0 | return bless {}, $class; | |||||||||||||||||||||||||||
| 443 | } | ||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | 9 | 1; | |||||||||||||||||||||||||||