added PP backend switch into Data::MessagePack

This commit is contained in:
makamaka 2010-09-01 11:59:01 +09:00
parent 712b8eec3d
commit a0705a6c67
3 changed files with 34 additions and 20 deletions

View File

@ -1,3 +1,9 @@
0.1x
- added PP version.
(makamaka)
0.15
- better argument validation.

View File

@ -1,7 +1,6 @@
package Data::MessagePack;
use strict;
use warnings;
use XSLoader;
use 5.008001;
our $VERSION = '0.15';
@ -12,7 +11,19 @@ our $false = do { bless \(my $dummy = 0), "Data::MessagePack::Boolean" };
sub true () { $true }
sub false () { $false }
XSLoader::load(__PACKAGE__, $VERSION);
if ( !__PACKAGE__->can('pack') ) { # this idea comes from Text::Xslate
if ( $ENV{ PERL_DATA_MESSAGEPACK } !~ /\b pp \b/xms ) {
eval {
require XSLoader;
XSLoader::load(__PACKAGE__, $VERSION);
};
die $@ if $@ && $ENV{ PERL_DATA_MESSAGEPACK } =~ /\b xs \b/xms; # force XS
}
if ( !__PACKAGE__->can('pack') ) {
print "PP\n";
require 'Data/MessagePack/PP.pm';
}
}
1;
__END__

View File

@ -2,21 +2,9 @@ package Data::MessagePack::PP;
use 5.008000;
use strict;
use B ();
use Scalar::Util qw( blessed );
use Carp ();
our $VERSION = '0.03';
# copied from Data::MessagePack
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 }
our $PreferInteger;
our $VERSION = '0.15';
# See also
# http://redmine.msgpack.org/projects/msgpack/wiki/FormatSpec
@ -24,6 +12,13 @@ our $PreferInteger;
# http://frox25.no-ip.org/~mtve/wiki/MessagePack.html : reference to using CORE::pack, CORE::unpack
package
Data::MessagePack;
use Scalar::Util qw( blessed );
use strict;
use B ();
BEGIN {
# for pack and unpack compatibility
if ( $] < 5.010 ) {
@ -160,11 +155,11 @@ sub _pack {
elsif ( $flags & B::SVf_POK ) { # raw / check needs before dboule
if ( $PreferInteger ) {
if ( $Data::MessagePack::PreferInteger ) {
if ( $value =~ /^-?[0-9]+$/ ) { # ok?
my $value2 = 0 + $value;
if ( 0 + $value != B::svref_2object( \$value2 )->int_value ) {
local $PreferInteger; # avoid for PV => NV
local $Data::MessagePack::PreferInteger; # avoid for PV => NV
return _pack( "$value" );
}
return _pack( $value + 0 );
@ -346,7 +341,8 @@ sub _unpack {
# Data::MessagePack::Unpacker
#
package Data::MessagePack::PP::Unpacker;
package
Data::MessagePack::Unpacker;
use strict;
@ -530,7 +526,7 @@ __END__
=head1 NAME
Data::MessagePack::PP - the pure perl version of Data::MessagePack
Data::MessagePack::PP - Pure Perl version of Data::MessagePack
=head1 LIMITATION
@ -540,9 +536,10 @@ In Perl 5.8.x, it requires L<Data::Float> and cannot unpack int64 and float (pac
=head1 SEE ALSO
L<http://msgpack.sourceforge.jp/>,
L<Data::MessagePack>,
L<Data::Float>,
L<http://frox25.no-ip.org/~mtve/wiki/MessagePack.html>,
L<Data::Float>
=head1 AUTHOR