The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


Python::Serialise::Pickle - a file for reading and writing pickled Python files


    use Python::Serialise::Pickle;

    my $pr = Python::Serialise::Pickle->new("file/for/reading");
    while (my $data = $pr->load()) {
          print Dumper $data;

    my $pw = Python::Serialise::Pickle->new(">file/for/writing");
    $pw->dump(['a' 'list']);
    $pw->dump("a string");



Pickling is a method of serialising files in Python (another method, Marshalling, is also available).

This module is an attempt to write a pure Perl implementation of the algorithm.


new <filename>

Open a file for reading or writing. Can take any arguments that IO::File can.


Returns the next data structure from the pickle file or undef.

dump <data structure>

Takes a ref to an array or a hash or a number or string and pickles it.

Structures may be nested.


Closes the current file.


Almost certainly lots and lots.

Serialised objects

At the moment we don't deal with serialised objects very well. Should probably just take or return a Python::Serialise::Pickle::Object object.

The 'None' object

Similar to Perl's undef but an object. At the moment we deal with it badly because if we returned undef then that would signify the end of the Pickle file.

Should probably be returned as a special object or something.


There's no testing for longs



Some nested dictionaries

Dictionaries are the Python equivalent of hashes. This module can deal with most nested dictionaries but, for some reason, this one :

        a={'a':['two',{'goof':'foo', 'a':[1,2,3]}]}

causes it to fail.

Chnaging it slightly starts it working again.

Bad reading of specs

This is entirely my fault


You could always dump the data structure out as YAML in Python and then read it back in with YAML in Perl.


Simon Wistow <>


(c) 2003 Simon Wistow

Distributed under the same terms as Perl itself.

This software is under no warranty and will probably ruin your life, kill your friends, burn your house and bring about the apocalypse.

SEE ALSO, YAML, IO::File and the RESOURCES file in this distribution.