Perl
  Home arrow Perl arrow Page 7 - 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 - Different Strokes
    ( Page 7 of 8 )

    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:

    ID: 1 Name: XYZ Slicer-Dicer-Toaster Price: 123.78 ID: 2 Name: FGH Serrated Carving Knife Price: 34.99 ID: 3 Name: Dual-Tub Washing Machine Price: 455.99
    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 array foreach($data as $item) { ?> <tr> <td><?=$item['id']?></td> <td><?=$item['name']?></td> <td><?=$item['price']?></td> </tr> <? } ?> </table> </body> </html>
    Here's what it looks like



    Now that's what I call platform-independence!

     
     
    >>> 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 5 Hosted by Hostway
    Stay green...Green IT