Bill
Bill

Reputation: 83

Perl code to serialize and deserialize variable like python pickle

I was wondering if someone could provide the Perl equivalent of the following python code that pickles and unpickles a variable to file.

data = [( "somestring.data", (178346464,1234568) )]
serialized_data = cPickle.dumps(data, protocol=-1)
length_prefix = struct.pack("!L", len(serialized_data))
message = length_prefix + serialized_data

Thanks.

Upvotes: 2

Views: 1668

Answers (3)

Eric Leschinski
Eric Leschinski

Reputation: 153943

Serialize a perl variable to file:

$thing = "Something the one true Morty might say";
use Python::Serialise::Pickle qw( );
$file_location = "/home/some/file.pckl";
open my $file, '>', $file_location or die $!; 
my $pickle = bless({ _fh => $file }, 'Python::Serialise::Pickle'); 
$pickle_out = $pickle->dump($thing); 
print $file $pickle_out; 
$pickle->close(); 
close $file; 

Contents of the file:

.S'Something\040the\040one\040true\040Morty\040might\040say' 
p0 
. 

Deserialize a perl variable from file back to a variable:

use Data::Dumper; 
use Python::Serialize::Pickle::InlinePython;
$file_location = "/home/some/file.pckl";
my $pic = Python::Serialize::Pickle::InlinePython->new($file_location); 
my $recovered_variable = $pic->load(); 
print "\$recovered_variable: '" . $recovered_variable . "'\n";

Prints:

$recovered_variable: 'Something the one true Morty might say'

Upvotes: 0

ikegami
ikegami

Reputation: 385897

Untested.

use Python::Serialise::Pickle qw( );

# Work around P::S::Pickle 0.01's extremely limiting interface.
sub pickle_dumps {
   open(my $fh, '>', \my $s) or die $!;
   my $pickle = bless({ _fh => $fh }, 'Python::Serialise::Pickle');
   $pickle->dump($_[0]);
   $pickle->close();
   return $s;
}

my $data = [ "somestring.data", [ 178346464, 1234568 ] ];
my $message = pack("N/a*", pickle_dumps($data));

Upvotes: 4

MattH
MattH

Reputation: 38247

It's been a long time (over a decade) since I looked at perl in earnest.

So I'll describe instead:

  • data is a small arbitrary data structure of a string and an array of integers in an array
  • data is serialized using a binary language-specific packing scheme (pickle) that can pack arbitrary data and code
  • the length of the serialized data is calculated and converted into binary format big-endian 4 byte.
  • the binary representation of length and the serialized data are concatenated

Looks like the basis of a line protocol. The receiving code would read 4 bytes, unpack payload length, read payload-length bytes. Unpack payload.

Upvotes: 1

Related Questions