Home arrow Perl Programming arrow Page 4 - Using Perl With WDDX

Boyz 'N The Hood - Perl

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.

  1. Using Perl With WDDX
  2. The Big Picture
  3. Packet Sniffer
  4. Boyz 'N The Hood
  5. All Mixed Up
  6. Flying Toasters And Dancing Knives
  7. Different Strokes
  8. This Way Out
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 2
August 07, 2002

print this article


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.

>>> More Perl Programming Articles          >>> More By icarus, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates


Dev Shed Tutorial Topics: