Using Perl With WDDX - All Mixed Up (
Page 5 of 8 )
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 module
use WDDX;
# create WDDX object
$wddx = new WDDX;
# simulate a WDDX packet
$packet = "<wddxPacket version='1.0'> <header/> <data> <array
length='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 elements
for ($i=0; $i<$length; $i++)
{
print "$$arrayref[$i]\n";
}
Here's the output:
red
blue
green
Wanna really cause some heartburn? Try serializing an array of arrays,
#!/usr/bin/perl
# include module
use WDDX;
# create WDDX object
my $wddx = new WDDX;
# create an array
my @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 object
print $wddx->serialize($obj);
and see what you get:
<wddxPacket version='1.0'>
<header/>
<data>
<struct>
<var name='num'>
<number>23</number>
</var>
<var name='str'>
<string>Abracadabra</string>
</var>
<var name='arr'>
<array length='3'>
<string>huey</string>
<string>dewey</string>
<boolean value='true'/>
</array>
</var>
</struct>
</data>
</wddxPacket>
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 module
use WDDX;
# create WDDX object
$wddx = new WDDX;
# simulate a WDDX packet
$packet = " <wddxPacket version='1.0'><header/><data><struct><var
name='num'><number>23</number></var><var
name='str'><string>Abracadabra</string></var><var name='arr'><array
length='3'><string>huey</string><string>dewey</string><boolean
value='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 values
foreach $k (@k)
{
print "$k --> " . $obj->get($k)->type . "\n";
}
Here's what you should see:
num --> number
str --> string
arr --> array