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