Convert-PerlRef2String
view release on metacpan or search on metacpan
lib/Convert/PerlRef2String.pm view on Meta::CPAN
package Convert::PerlRef2String;
use 5.008003;
use strict;
use warnings;
require Exporter;
use AutoLoader qw(AUTOLOAD);
use vars qw(@ISA @EXPORT @EXPORT_OK);
our @ISA = qw(Exporter);
# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.
# This allows declaration use Convert::PerlRef2String ':all';
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
# will save memory.
@EXPORT = qw(perlref2string string2perlref string2perlcode);
@EXPORT_OK = qw(perlref2string string2perlref string2perlcode);
our $VERSION = '0.03';
# Preloaded methods go here.
# Autoload methods go after =cut, and are processed by the autosplit program.
use MIME::Base64;
use Compress::Zlib;
use Data::Dumper;
sub perlref2string {
my $perlref = shift;
die "Argument undefined" unless(defined $perlref);
my ($string,$zipped,$encoded);
if(ref $perlref){
eval{$string = Dumper($perlref);};
die $! if($@);
}else{
$string = $perlref;
}
eval{$zipped = Compress::Zlib::memGzip($string);};
die $! if($@);
eval{$encoded = encode_base64($zipped);};
die $! if($@);
return $encoded;
}
sub string2perlref {
my $string = shift;
return unless(defined $string);
my($decoded,$perlref,$VAR1);
eval{$decoded = decode_base64($string);};
die $! if($@);
$perlref = eval($VAR1 = Compress::Zlib::memGunzip($decoded));
die $! if($@);
return $perlref;
}
sub string2perlcode {
my $string = shift;
return unless(defined $string);
my($decoded,$perlcode);
eval{$decoded = decode_base64($string);};
die $! if($@);
eval {$perlcode = Compress::Zlib::memGunzip($decoded);};
die $! if($@);
return $perlcode;
}
1;
__END__
=head1 NAME
Convert::PerlRef2String - Converting PERL references to compressed string and vice versa
=head1 SYNOPSIS
The following script
use Convert::PerlRef2String;
#Sender's action:
use Data::Dumper;
my $perl = {
'Order' => {
'BookName' => 'Programming Web Serivices with Perl',
'Id' => '0-596-00206-8',
'Quantity' => '500'
},
'Payment' => {
'CardType' => 'VISA',
'ValidDate' => '12-10-2006',
'CardNo' => '1234-5678-9012-3456',
'Bearer' => 'Kai Li'
}
};
my $string = perlref2string($perl);
print $string,"\n";
#sending the string over the Internet...
#Receiver's action:
my $perlref = string2perlref($string);
print Dumper($perlref);
produces this output:
H4sIAAAAAAAAA32RzarCQAxG9z5FFheycSCttiqi4M9GFK1XqetoBx20rYyjUqTvbtVeuYJtlplz
vknIj9/7taADtwq8C2c6kBqh0/1ofxT243g/5VA+MfR0vNUchirawkquYSG1uqiNPMFVmR14Uh+w
Wpg1Cl4pJJyWK4hsckWzhJ+fOTLKJC/LIcLvaPo/Aj1OQhmZ8r0ybsA6WCbHfDN/tOgVj5LhPh9U
MGST85YtLBI2kVtqPT6Zxn9KrS4ct9EULcr0Wt0pd/uSdX4fHLOCicIiOn0/pO3KHXfoF8XsAQAA
$VAR1 = {
'Order' => {
'BookName' => 'Programming Web Serivices with Perl',
'Id' => '0-596-00206-8',
'Quantity' => '500'
},
'Payment' => {
'CardType' => 'VISA',
'ValidDate' => '12-10-2006',
'CardNo' => '1234-5678-9012-3456',
'Bearer' => 'Kai Li'
}
( run in 1.585 second using v1.01-cache-2.11-cpan-524268b4103 )