Perl
  Home arrow Perl arrow Page 6 - Using Perl With WDDX
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
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: 2 stars2 stars2 stars2 stars2 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    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 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 Articles
    More By icarus, (c) Melonfire


     

       

    PERL ARTICLES

    - Perl: A Continuing Look at Hashes and Multid...
    - Perl: Another Round with Hashes
    - Perl Hashes
    - Perl Lists: A Final Look at List::Util
    - Perl Lists: Utilizing List::Util
    - Perl Lists: The Split() Function
    - SQL and CGI with Perl and DBI
    - Perl Lists: More Functions and Operators
    - SELECT Queries and Perl
    - Perl Lists: More on Manipulation
    - Creating a Database with Perl and DBI
    - Perl: Sailing the List(less) Seas
    - Perl and DBI
    - Perl: Concatenating Text and More
    - Perl Text: Quoting Without Quote Marks

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway