Perl
  Home arrow Perl arrow Page 6 - Using Perl With WDDX
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PERL

Using Perl With WDDX
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    2002-08-07


    Table of Contents:
  • Using Perl With WDDX
  • The Big Picture
  • Packet Sniffer
  • Boyz 'N The Hood
  • All Mixed Up
  • Flying Toasters And Dancing Knives
  • Different Strokes
  • This Way Out

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Using Perl With WDDX - Flying Toasters And Dancing Knives
    ( Page 6 of 8 )

    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 modules use DBI; use WDDX; # create WDDX object $wddx = new WDDX; # connect my $dbh = DBI->connect("DBI:mysql:database=mydb;host=localhost", "root", "gd63hrd", {'RaiseError' => 1}); # execute query my $sth = $dbh->prepare("SELECT * FROM catalog"); $sth->execute(); # counter my $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 names while(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 packat print $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><var name='price'><number>123.78</number></var><var name='id'><number>1</number></var></struct><struct><var name='name'><string>FGH Serrated Carving Knife</string></var><var name='price'><number>34.99</number></var><var name='id'><number>2</number></var></struct><struct><var name='name'><string>Dual-Tub Washing Machine</string></var><var name='price'><number>455.99</number></var><var name='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 Articles          >>> More By icarus, (c) Melonfire
     

       

    PERL ARTICLES

    - More Perl Bits
    - Perl, Bit by Bit
    - Basic Charting with Perl
    - Using Getopt::Long: More Command Line Option...
    - Command Line Options in Perl: Using Getopt::...
    - Web Access with LWP
    - More Templating Tools for Perl
    - Site Layout with Perl Templating Tools
    - Build a Perl RSS Aggregator with Templating ...
    - Looping, Security, and Templating Tools
    - Perl: Bon Voyage Lists and Hashes
    - Templating Tools
    - Perl: Number Crunching
    - Perl Debuggers in Detail
    - Debugging Perl





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    Stay green...Green IT