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

Flying Toasters And Dancing Knives - 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

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

+----+----------------------------+--------+ | id | name | price | +----+----------------------------+--------+ | 1 | XYZ Slicer-Dicer-Toaster | 123.78 | | 2 | FGH Serrated Carving Knife | 34.99 | | 3 | Dual-Tub Washing Machine | 455.99 | +----+----------------------------+--------+
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:

<wddxPacket version='1.0'><header/><data><array length='3'><struct><var name='name'><string>XYZ Slicer-Dicer-Toaster</string></var><varname='price'><number>123.78</number></var><varname='id'><number>1</number></var></struct><struct><varname='name'><string>FGH Serrated Carving Knife</string></var><varname='price'><number>34.99</number></var><varname='id'><number>2</number></var></struct><struct><varname='name'><string>Dual-Tub Washing Machine</string></var><varname='price'><number>455.99</number></var><varname='id'><number>3</number></var></struct></array></data></wddxPacket>
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.

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PERL PROGRAMMING ARTICLES

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