2009-04-15 12:55:41 +09:00
|
|
|
package Data::MessagePack;
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
2009-07-01 17:45:18 +09:00
|
|
|
use 5.008001;
|
2009-04-15 12:55:41 +09:00
|
|
|
|
2010-09-10 09:35:39 +09:00
|
|
|
our $VERSION = '0.20';
|
2009-07-03 01:49:37 +09:00
|
|
|
our $PreferInteger = 0;
|
2009-04-15 12:55:41 +09:00
|
|
|
|
2009-07-30 16:22:00 +09:00
|
|
|
our $true = do { bless \(my $dummy = 1), "Data::MessagePack::Boolean" };
|
|
|
|
our $false = do { bless \(my $dummy = 0), "Data::MessagePack::Boolean" };
|
|
|
|
sub true () { $true }
|
|
|
|
sub false () { $false }
|
|
|
|
|
2010-09-01 11:59:01 +09:00
|
|
|
if ( !__PACKAGE__->can('pack') ) { # this idea comes from Text::Xslate
|
2010-09-01 16:04:25 +09:00
|
|
|
my $backend = $ENV{ PERL_DATA_MESSAGEPACK } || '';
|
|
|
|
if ( $backend !~ /\b pp \b/xms ) {
|
2010-09-01 11:59:01 +09:00
|
|
|
eval {
|
|
|
|
require XSLoader;
|
|
|
|
XSLoader::load(__PACKAGE__, $VERSION);
|
|
|
|
};
|
2010-09-01 16:04:25 +09:00
|
|
|
die $@ if $@ && $backend =~ /\b xs \b/xms; # force XS
|
2010-09-01 11:59:01 +09:00
|
|
|
}
|
|
|
|
if ( !__PACKAGE__->can('pack') ) {
|
|
|
|
require 'Data/MessagePack/PP.pm';
|
|
|
|
}
|
|
|
|
}
|
2009-04-15 12:55:41 +09:00
|
|
|
|
|
|
|
1;
|
|
|
|
__END__
|
|
|
|
|
|
|
|
=head1 NAME
|
|
|
|
|
2010-09-06 14:19:31 +09:00
|
|
|
Data::MessagePack - MessagePack serialising/deserialising
|
2009-04-15 12:55:41 +09:00
|
|
|
|
|
|
|
=head1 SYNOPSIS
|
|
|
|
|
|
|
|
my $packed = Data::MessagePack->pack($dat);
|
|
|
|
my $unpacked = Data::MessagePack->unpack($dat);
|
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
|
2010-09-06 14:19:31 +09:00
|
|
|
This module converts Perl data structures to MessagePack and vice versa.
|
|
|
|
|
2010-09-10 20:38:37 +09:00
|
|
|
=head1 ABOUT MESSAGEPACK FORMAT
|
|
|
|
|
2010-09-06 14:19:31 +09:00
|
|
|
MessagePack is a binary-based efficient object serialization format.
|
|
|
|
It enables to exchange structured objects between many languages like JSON. But unlike JSON, it is very fast and small.
|
2009-04-15 12:55:41 +09:00
|
|
|
|
2010-09-10 20:38:37 +09:00
|
|
|
=head2 ADVANTAGES
|
|
|
|
|
|
|
|
=over 4
|
|
|
|
|
|
|
|
=item PORTABILITY
|
|
|
|
|
|
|
|
Messagepack is language independent binary serialize format.
|
|
|
|
|
|
|
|
=item SMALL SIZE
|
|
|
|
|
|
|
|
say length(JSON::XS::encode_json({a=>1, b=>2})); # => 13
|
|
|
|
say length(Storable::nfreeze({a=>1, b=>2})); # => 21
|
|
|
|
say length(Data::MessagePack->pack({a=>1, b=>2})); # => 7
|
|
|
|
|
|
|
|
MessagePack format saves memory than JSON and Storable format.
|
|
|
|
|
|
|
|
=item STREAMING DESERIALIZER
|
|
|
|
|
|
|
|
MessagePack supports streaming deserializer. It is useful for networking such as RPC.
|
|
|
|
|
|
|
|
=back
|
|
|
|
|
|
|
|
If you want to get more informations about messagepack format, please visit to L<http://msgpack.org/>.
|
|
|
|
|
2010-05-03 01:09:21 +09:00
|
|
|
=head1 METHODS
|
|
|
|
|
|
|
|
=over 4
|
|
|
|
|
2010-09-10 21:25:46 +09:00
|
|
|
=item my $packed = Data::MessagePack->pack($data[, $max_depth]);
|
2010-05-03 01:09:21 +09:00
|
|
|
|
2010-09-10 21:25:46 +09:00
|
|
|
Pack the $data to messagepack format string.
|
|
|
|
|
|
|
|
This method throws exception when nesting perl structure more than $max_depth(default: 512) for detecting circular reference.
|
|
|
|
|
|
|
|
Data::MessagePack->pack() throws exception when encountered blessed object. Because MessagePack is language independent format.
|
2010-05-03 01:09:21 +09:00
|
|
|
|
|
|
|
=item my $unpacked = Data::MessagePack->unpack($msgpackstr);
|
|
|
|
|
|
|
|
unpack the $msgpackstr to messagepack format string.
|
|
|
|
|
|
|
|
=back
|
|
|
|
|
2009-07-02 14:25:48 +09:00
|
|
|
=head1 Configuration Variables
|
|
|
|
|
|
|
|
=over 4
|
|
|
|
|
2009-07-02 14:29:49 +09:00
|
|
|
=item $Data::MessagePack::PreferInteger
|
2009-07-02 14:25:48 +09:00
|
|
|
|
|
|
|
Pack the string as int when the value looks like int(EXPERIMENTAL).
|
|
|
|
|
|
|
|
=back
|
|
|
|
|
2010-09-10 20:38:37 +09:00
|
|
|
=head1 SPEED
|
|
|
|
|
|
|
|
This is result of benchmark/serialize.pl and benchmark/deserialize.pl on my SC440(Linux 2.6.32-23-server #37-Ubuntu SMP).
|
|
|
|
|
|
|
|
-- serialize
|
|
|
|
JSON::XS: 2.3
|
|
|
|
Data::MessagePack: 0.20
|
|
|
|
Storable: 2.21
|
|
|
|
Benchmark: timing 1000000 iterations of json, mp, storable...
|
|
|
|
json: 5 wallclock secs ( 3.95 usr + 0.00 sys = 3.95 CPU) @ 253164.56/s (n=1000000)
|
|
|
|
mp: 3 wallclock secs ( 2.69 usr + 0.00 sys = 2.69 CPU) @ 371747.21/s (n=1000000)
|
|
|
|
storable: 26 wallclock secs (27.21 usr + 0.00 sys = 27.21 CPU) @ 36751.19/s (n=1000000)
|
|
|
|
|
|
|
|
-- deserialize
|
|
|
|
JSON::XS: 2.3
|
|
|
|
Data::MessagePack: 0.20
|
|
|
|
Storable: 2.21
|
|
|
|
Benchmark: timing 1000000 iterations of json, mp, storable...
|
|
|
|
json: 4 wallclock secs ( 4.45 usr + 0.00 sys = 4.45 CPU) @ 224719.10/s (n=1000000)
|
|
|
|
mp: 6 wallclock secs ( 5.45 usr + 0.00 sys = 5.45 CPU) @ 183486.24/s (n=1000000)
|
|
|
|
storable: 7 wallclock secs ( 7.77 usr + 0.00 sys = 7.77 CPU) @ 128700.13/s (n=1000000)
|
|
|
|
|
2009-04-15 12:55:41 +09:00
|
|
|
=head1 AUTHORS
|
|
|
|
|
|
|
|
Tokuhiro Matsuno
|
|
|
|
|
2010-09-06 14:19:31 +09:00
|
|
|
Makamaka Hannyaharamitu
|
|
|
|
|
2010-01-04 12:10:46 +09:00
|
|
|
=head1 THANKS TO
|
|
|
|
|
|
|
|
Jun Kuriyama
|
|
|
|
|
2010-09-06 14:19:31 +09:00
|
|
|
Dan Kogai
|
|
|
|
|
|
|
|
FURUHASHI Sadayuki
|
|
|
|
|
2009-10-22 14:32:39 +09:00
|
|
|
=head1 LICENSE
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify
|
|
|
|
it under the same terms as Perl itself.
|
|
|
|
|
|
|
|
|
2009-04-15 12:55:41 +09:00
|
|
|
=head1 SEE ALSO
|
|
|
|
|
2010-09-10 20:38:37 +09:00
|
|
|
L<http://msgpack.org/> is official web site for MessagePack format.
|
2009-04-15 12:55:41 +09:00
|
|
|
|