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

All Mixed Up - 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


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);
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 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";}
Here's what you should see:

num --> number str --> stringarr --> array

>>> 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: