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.
Perl implements WDDX via its WDDX.pm module, which you can download from http://www.scripted.com/wddx/. This Perl module includes both a serializer and deserializer for WDDX, and also implements all the base data types defined in the WDDX specification.
The basic procedure for serializing a WDDX packet with WDDX.pm is fairly straightforward: first create an object representation of the value you want to serialize, and then call the module's serialize() method to actually create the packet data. The following example illustrates the process of serializing a string variable with the module's string() method:
#!/usr/bin/perl
# include module
use WDDX;
# create WDDX object
my $wddx = new WDDX;
# create WDDX string object
$obj = $wddx->string("Robin Hood");
# serialize and print object
print $wddx->serialize($obj);
In a similar manner, you can use the number() and boolean() methods to create numeric and Boolean data representations also.
#!/usr/bin/perl
# include moduleuse WDDX;# create WDDX objectmy $wddx = new WDDX;# create WDDX number object$obj = $wddx->number(5);# serialize and print objectprint $wddx->serialize($obj);
You can serialize integer-indexed arrays with the array() method.
#!/usr/bin/perl
# include moduleuse WDDX;# create WDDX objectmy $wddx = new WDDX;# create a Perl array containing WDDX data objectsmy @colors = ( $wddx->string("red"), $wddx->string("blue"), $wddx->string("green"),);# create a WDDX array object# note that the array() method must be passed a reference $obj = $wddx->array(\@colors);# serialize and print objectprint $wddx->serialize($obj);
Note that, when serializing arrays, the array() method must be passed a reference to a Perl array containing WDDX data objects. These objects need not be of the same type.
You can also serialize a Perl hash with the struct() method.
#!/usr/bin/perl
# include moduleuse WDDX;# create WDDX objectmy $wddx = new WDDX;# create WDDX struct from a Perl hash# note that values of the Per,l hash must be WDDX data objects $obj =$wddx->struct( { "parrot" => $wddx->string("Polly"), "hippo" => $wddx->string("Harry"), "iguana" => $wddx->string("Ian") });# serialize and print objectprint $wddx->serialize($obj);
This creates a <struct> containing name-value pairs corresponding to the elements of the hash.
Finally, you can generate a WDDX <recordset> with the recordset() method. This method accepts three arguments: a list of column names, a list of corresponding data types, and a two-dimensional list of values. Take a look at the following example, which might make this clearer: