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

Different Strokes - 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


So that takes care of the server - now how about the client?

The function of the client should be fairly obvious - connect to the server described above via HTTP, read the WDDX packet generated, deserialize it into a native representation, and display the output to the user. Here's the script to accomplish this:

#!/usr/bin/perl # module to read HTTP response use LWP::UserAgent; # WDDX module use WDDX; # create an HTTP client $client = LWP::UserAgent->new; my $request = HTTP::Request->new(GET => "http://my.wddx.server/cgi-bin/server.cgi"); # store the response in an object my $result = $client->request($request); # get a good response if ($result->is_success) { # deserialize resulting packet as array and get reference my $wddx = new WDDX; $packet = $wddx->deserialize($result->content); $arrayref = $packet->as_arrayref; $length = $packet->length(); # iterate through array for($x=0; $x<$length; $x++) { # get hash reference and print values $item = $$arrayref[$x]; print "ID: " . $item->{'id'} . "\nName: " . $item->{'name'} . "\nPrice: " . $item->{'price'} . "\n\n"; } } # response is bad... else { print "Could not read response"; }
Here, I've used the LWP Perl module to instantiate a simple HTTP client, which I've then pointed at the URL of the WDDX server. This client connects to the specified URL, retrieves the dynamically-generated WDDX packet, and stores it in an object.

This object is then passed to the WDDX module's deserialize() method, and is converted into its native form, a Perl array. A "for" loop is used to iterate through the array, access each individual hash within the array, and retrieve the data for each item in the catalog. This data is then printed to the standard output device.

And, when you run this script, you'll see something like this:

ID: 1 Name: XYZ Slicer-Dicer-ToasterPrice: 123.78ID: 2Name: FGH Serrated Carving KnifePrice: 34.99ID: 3Name: Dual-Tub Washing MachinePrice: 455.99
It's important to note that this client need not be on the same physical machine as the server - so long as it can connect to the server via HTTP, it can retrieve the WDDX packet, decode it and display the results. Heck, it doesn't even need to be in Perl - here's another version of the same client, this time in PHP.

<? // url of Web page$url = "http://my.wddx.server/cgi-bin/server.cgi";// read WDDX packet into string$packet = join ('', file($url));// deserialize$data = wddx_deserialize($packet);?><html><head><basefont face="Verdana"></head><body><table border="1" cellspacing="0" cellpadding="5"><tr><td>Item ID</td><td>Name</td><td>Price</td></tr><?// iterate through arrayforeach($data as $item){?><tr><td><?=$item['id']?></td><td><?=$item['name']?></td><td><?=$item['price']?></td></tr><?}?></table></body></html>
Here's what it looks like

Now that's what I call platform-independence!

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