Scott Hurring » Code » Perl » PHP serialize and unserialize in Perl » v0.92

Download Code
download
serialize_v0.92.tgz
v0.92 stable · Feb 12, 2006
(browse code)

Description

This is a perl implementation of PHP's native serialize() and unserialize() functions.

Taken along with the python serialize implementation, this code will enable you to transfer simple data structures between PHP, Python, and Perl using PHP's data serialization format.

This code aims to be a very lightweight data transport layer between PHP, perl and python using PHP's native serialization format.


Useage

To serialize perl data:
use serialize;
my $string = serialize($data);
# or...
my $string = serialize(\%data);
# or..
my $string = serialize(\@data);

To unserialize a serialized string into perl data:

use serialize;
my $data = unserialize($string);

Examples

%data = ("Hello"=>"World");
print serialize(\%data) . "\n";
# Output: a:1:{s:5:"Hello";s:5:"World";}

@data = [1,2,3,4];
print serialize(\@data) . "\n";
# Output: a:1:{i:0;a:4:{i:0;i:1;i:1;i:2;i:2;i:3;i:3;i:4;}}

@data = ("Array", "With", 4, "Values");
print serialize(\@data) . "\n";
# Output: a:4:{i:0;s:5:"Array";i:1;s:4:"With";i:2;i:4;i:3;s:6:"Values";}

$data = 12.5;
print serialize($data) . "\n";
# Output: d:12.5;

To help test the interoperability between python and php, i've provided some test scripts in the Download section that can be used by piping the output of one into another, like so:
$ ./serialize.pl | ./unserialize.php


PHP's Serialization Format

TypeSerializedExample
NULLN;N;
Integeri:$data;i:123;
Doubled:$data;d:1.23;
Floatd:$data;d:1.23;
Booleanb:$bool_value;b:1;
Strings:$data_length:"$data";s:5:"Hello"
Arraya:$key_count:{$key;$value}a:1:{i:1;i:2}
$value can be any data type

Supported Perl Types:
Serializing:

Unserializing:

*arrays are handled special becuase PHP only has one array type "array()", which is analagous to Perl %hash's. When you try to serialize a perl array, it's automagically converted into a PHP aray with keys numbered from 0 upwards.

Type Translation Table:

Perl => serialize => PHP => unserialize => Perl
PerlPHPPerl
NULL \0NULLNULL \0
intintint
doubledoubledouble
stringstringstring
\%hash_refarray\%hash_ref
\@array_refarray\%hash_ref


README

Perl implementation of PHP's native serialize() and unserialize() functions.

Scott Hurring
scott at hurring dot com
http://hurring.com/
Please be nice and send bugfixes and code improvements to me.

@version v0.91
@author Scott Hurring; scott at hurring dot com
@copyright Copyright (c) 2005 Scott Hurring
@license http://opensource.org/licenses/gpl-license.php GNU Public License

Most recent version can be found at:
http://hurring.com/code/perl/serialize/

=====================================================================

Unlike modules that make use of language-specific binary formats, the
output of serialize() is an ASCII string, meaning you can easily
manipulate it as you would any other string, i.e. sticking it
into a URL, a database, a file, etc...

Taken along with my python serialize implementation, this code will
enable you to transfer data between PHP, Python, and Perl using PHP's
data serialization format.

To serialize:
	# serialize an array into a string
	my $serialized_string = serialize(\@data);
	# or... serialize a hash into a string
	my $serialized_string = serialize(\%data);

To unserialize:
	# unserialize some string into python data
	$hash_ref = unserialize($serialized_string)

PHP Serialization Format:
	NULL		N;
	Boolean		b:1;			b:$data;
	Integer		i:123;			i:$data;
	Double		d:1.23;			d:$data;
	String		s:5:"Hello"		s:$length:"$data";
	Array		a:1:{i:1;i:2}		a:$key_count:{$key;$value}
						$value can be any data type

Supported Perl Types:
	Serializing:
	NULL (\0), int, double, string, hash, array

	Unserializing:
	NULL (\0), int, double, string, hash

*array is unserialized as a hash, becuase PHP only has one array
type "array()", which is analagous to Perl hash's.  When you try to
serialize a perl array, it's automagically converted into a hash
with keys numbered from 0 up.

Type Translation Table:
	(Perl)	(serialize)	(PHP)	    (unserialize)  (Perl)
	NULL 	=>		NULL 			=> NULL
	int 	=>		int 			=> int
	double 	=>		double			=> double
	string 	=>		string			=> string
	hash 	=>		array			=> hash
	array 	=>		array			=> hash

====================================================================

Warning:

This code comes with absolutely NO warranty... it is a quick hack
that i sometimes work on in my spare time.  This code may or may
not melt-down your computer and give you nonsensical output.

Please, do not use this code in a production enviornment until
you've thoroughly tested it.

=====================================================================


Older Versions