Using Perl With WDDX - Boyz 'N The Hood (Page 4 of 8 )
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);
Here's the output:
<wddxPacket version='1.0'>
<header/><data><string>Robin Hood</string></data></wddxPacket>
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.
<wddxPacket version='1.0'>
<header/><data><struct><var name='hippo'><string>Harry</string></var><var name='parrot'><string>Polly</string></var><var name='iguana'><string>Ian</string></var></struct></data></wddxPacket>
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:
#!/usr/bin/perl
# include moduleuse WDDX;# create WDDX objectmy $wddx = new WDDX;# create WDDX recordset$obj = $wddx->recordset( ["ID", "NAME"], ["number", "string"], [ [1, "John"], [2, "Joe"], [3, "Mary"] ]);# serialize and print objectprint $wddx->serialize($obj);
You can create binary data packets with the binary() method, and timestamps with the datetime() method - I'll leave these to you to experiment with.
Next: All Mixed Up >>
More Perl Articles
More By icarus, (c) Melonfire