PHP::Serialization::XS - simple flexible means of converting the output of PHP's serialize() into the equivalent Perl memory structure, and vice versa - XS version.


    use PHP::Serialization:XS qw(serialize unserialize);
    my $encoded = serialize({ a => 1, b => 2 });
    my $hashref = unserialize($encoded);

    my $psx = PHP::Serialization::XS->new(prefer_hash => 1);
    my $hash = $psx->decode("a:0:{}");
    my $psy = PHP::Serialization::XS->new(prefer_array => 1);
    my $array = $psy->decode("a:0:{}");

Also see PHP::Serialization.


This module provides the same interface as PHP::Serialization, but uses XS during deserialization, for speed enhancement.

If you have code written for PHP::Serialization, you should be able to replace all references to PHP::Serialization with PHP::Serialization::XS and notice no change except for an increase in speed of deserialization.

Node that serialization is still provided by PHP::Serialization, and its speed should therefore not be affected. This is why PHP::Serialization::XS requires PHP::Serialization to be installed.


PHP "arrays" are all associative ; some of them just happen to have all numeric keys. PHP::Serialization tries to Do What You Mean by converting PHP arrays with gapless numeric indices from 0..n into a Perl array instead of a hash. This may be convenient, but by itself it is wrong, because it is not predictable. The special case of an empty array stands out : if there are no keys, should the resulting structure be an array or a hash ? Neither answer works universally, so the code that uses the Perl structure has to check for both cases on every access.

For this reason, PHP::Serialization::XS accepts additional options to its new() constructor : prefer_hash, prefer_array, or prefer_undef (use only one at a time). prefer_undef allows autovivification. Currently, prefer_array is the default, for backward-compatibility reasons, but if you wish your code to act consistently, you should always use the OO interface and specify the behavior you want (this configurability is not available through the procedural interface).



Prior to version 0.09, there were significant memory leaks (thanks for the bug report goes to Rune Hylleberg).


More tests.




Darren Kulp, <>

Tests stolen shamelessly from Tomas Doran's PHP::Serialization package.


Copyright (C) 2009-2017 by Darren Kulp

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.10.0 or, at your option, any later version of Perl 5 you may have available.