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.
Now that you've understood the fundamentals, let's look at a simple application of WDDX.
One of the most popular uses of WDDX involves using it to retrieve frequently-updated content from a content repository - news headlines, stock prices and the like. Since WDDX is designed expressly for transferring data in a standard format, it excels at this type of task - in fact, the entire process can be accomplished via two very simple scripts, one running on the server and the other running on the client.
Let's look at the server component first. We'll begin with the assumption that the content to be distributed (products and their corresponding prices, in this case) are stored in a single database table, which is updated on a regular basis. The table in which this data is stored looks a lot like this:
Now, we need to write a script which will reside on the server, connect to this table, retrieve a list of items in the catalog, and encode them as a WDDX packet
#!/usr/bin/perl
# load modulesuse DBI;use WDDX;# create WDDX object$wddx = new WDDX;# connectmy $dbh = DBI->connect("DBI:mysql:database=mydb;host=localhost", "root","gd63hrd", {'RaiseError' => 1});# execute querymy $sth = $dbh->prepare("SELECT * FROM catalog"); $sth->execute();# countermy $count = 0;my @data;# iterate through resultset# append record to @data array as WDDX data objects# each element of this array is a hash with# keys corresponding to the column nameswhile(my $ref = $sth->fetchrow_hashref()){ $data[$count] = $wddx->struct({ "id" =>$wddx->number($ref->{'id'}), "name" => $wddx->string($ref->{'name'}),"price" => $wddx->number($ref->{'price'}) }); $count++;}# clean up$dbh->disconnect();# create a WDDX array data object from @data$obj = $wddx->array(\@data);print "Content-type: text/html\r\n\r\n";# serialize and print the packatprint $wddx->serialize($obj);
This is a pretty simple script, especially if you know a little bit about Perl. First, I've included both the WDDX module and the additional DBI module (which I need in order to retrieve data from the database). I've then used DBI's standard constructs to connect to the database and retrieve all the records from the "catalog" table.
Once a resultset has been retrieved, a "while" loop is used to iterate through it. Each record is retrieved as a Perl hash, and the various elements (columns) of each record are then restructured into a Perl hash containing three keys - "id", "name" and "price" - and WDDX-encoded values. This hash is then converted into a WDDX <struct> via the struct() method, and added to the @data Perl array.
Once all the records in the resultset have been processed, the @data Perl array (which now contains all the data from the table as a series of WDDX <struct>s) is converted into a WDDX array via the array() method, serialized via the serialize() method, and printed to the standard output.
Here's a sample WDDX packet generated by the server above:
This script should be placed in your Web server's "cgi-bin" directory and given appropriate permissions, so that a user accessing it via a Web browser sees a WDDX packet like the one above. Every time a user accesses the script, a new WDDX packet, containing the latest data from the database, is generated and printed.