Perl
  Home arrow Perl arrow Page 7 - 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 
IBM Rational Software Development Conference
 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

    Dell PowerEdge Servers

    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-ToasterPrice: 123.78ID: 2Name: FGH Serrated Carving KnifePrice: 34.99ID: 3Name: Dual-Tub Washing MachinePrice: 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 arrayforeach($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

    - 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 3 hosted by Hostway