Think there's no such thing as platform independence? Thinkagain. This article introduces you to WDDX, a platform-neutral way toexchange data structures across the Web, and shows you how you can putit to work using the Perl WDDX module.
The flip side of whatever you just read is, of course, deserialization. Perl's WDDX.pm module accomplishes this via its deserialize() method, which can be used to convert WDDX-based language-independent data representations into native data types.
Consider the following example, which demonstrates how a WDDX packet containing a string value is deserialized into a Perl scalar variable:
#!/usr/bin/perl
# use WDDX module
use WDDX;
# create WDDX object
$wddx = new WDDX;
# simulate a WDDX packet
$packet = "<wddxPacket version='1.0'><header/><data><string>Robin
Hood</string></data></wddxPacket>";
# deserialize packet into WDDX string object
$obj = $wddx->deserialize($packet);
# check object type
if ($obj->type eq "string")
{
# and print as scalar
print $obj->as_scalar;
}
In this case, the WDDX packet is first deserialized into a WDDX string object, and then the string object's as_scalar() method is used to convert the string object into a native Perl scalar. Note that the deserialized object exposes a type() method, which can be used to identify the data type and process it appropriately.
Here's the output:
Robin Hood
This deserialization works with arrays too - as the following example demonstrates:
#!/usr/bin/perl
# use WDDX moduleuse WDDX;# create WDDX object$wddx = new WDDX;# simulate a WDDX packet$packet = "<wddxPacket version='1.0'> <header/> <data> <arraylength='3'> <string>red</string> <string>blue</string><string>green</string> </array> </data> </wddxPacket>";# deserialize packet into WDDX array object$obj = $wddx->deserialize($packet);# get number of elements in array$length = $obj->length();# get reference to native Perl array$arrayref = $obj->as_arrayref();# iterate through array and print elementsfor ($i=0; $i<$length; $i++){ print "$$arrayref[$i]\n";}
Here's the output:
red
bluegreen
Wanna really cause some heartburn? Try serializing an array of arrays,
#!/usr/bin/perl
# include moduleuse WDDX;# create WDDX objectmy $wddx = new WDDX;# create an arraymy @arr = ( $wddx->string("huey"), $wddx->string("dewey"),$wddx->boolean(1) );# create a WDDX hash$obj = $wddx->struct( { "str" => $wddx->string("Abracadabra"), "num" => $wddx->number(23), "arr" => $wddx->array(\@arr) });# serialize and print objectprint $wddx->serialize($obj);
This is a hash with three keys, one containing a string, the second a number, and the third an array. Now, try deserializing the WDDX packet generated from the code above.
#!/usr/bin/perl
# use WDDX moduleuse WDDX;# create WDDX object$wddx = new WDDX;# simulate a WDDX packet$packet = " <wddxPacket version='1.0'><header/><data><struct><varname='num'><number>23</number></var><varname='str'><string>Abracadabra</string></var><var name='arr'><arraylength='3'><string>huey</string><string>dewey</string><booleanvalue='true'/></array></var></struct></data></wddxPacket>";# deserialize packet into WDDX array object$obj = $wddx->deserialize($packet);# get reference to native Perl hash$hashref = $obj->as_hashref();# get keys@k = $obj->keys();# print keys and type of corresponding valuesforeach $k (@k){ print "$k --> " . $obj->get($k)->type . "\n";}