Perl
  Home arrow Perl arrow Page 5 - 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

    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 - All Mixed Up
    (Page 5 of 8 )

    The flip side of whatever you just read is, of course, deserialization. Perl's WDDX.pm module accomplishes this via its deserialize() method, which can be used to convert WDDX-based language-independent data representations into native data types.

    Consider the following example, which demonstrates how a WDDX packet containing a string value is deserialized into a Perl scalar variable:

    #!/usr/bin/perl # use WDDX module use WDDX; # create WDDX object $wddx = new WDDX; # simulate a WDDX packet $packet = "<wddxPacket version='1.0'><header/><data><string>Robin Hood</string></data></wddxPacket>"; # deserialize packet into WDDX string object $obj = $wddx->deserialize($packet); # check object type if ($obj->type eq "string") { # and print as scalar print $obj->as_scalar; }
    In this case, the WDDX packet is first deserialized into a WDDX string object, and then the string object's as_scalar() method is used to convert the string object into a native Perl scalar. Note that the deserialized object exposes a type() method, which can be used to identify the data type and process it appropriately.

    Here's the output:

    Robin Hood
    This deserialization works with arrays too - as the following example demonstrates:

    #!/usr/bin/perl # use WDDX moduleuse WDDX;# create WDDX object$wddx = new WDDX;# simulate a WDDX packet$packet = "<wddxPacket version='1.0'> <header/> <data> <arraylength='3'> <string>red</string> <string>blue</string><string>green</string> </array> </data> </wddxPacket>";# deserialize packet into WDDX array object$obj = $wddx->deserialize($packet);# get number of elements in array$length = $obj->length();# get reference to native Perl array$arrayref = $obj->as_arrayref();# iterate through array and print elementsfor ($i=0; $i<$length; $i++){ print "$$arrayref[$i]\n";}
    Here's the output:

    red bluegreen
    Wanna really cause some heartburn? Try serializing an array of arrays,

    #!/usr/bin/perl # include moduleuse WDDX;# create WDDX objectmy $wddx = new WDDX;# create an arraymy @arr = ( $wddx->string("huey"), $wddx->string("dewey"),$wddx->boolean(1) );# create a WDDX hash$obj = $wddx->struct( { "str" => $wddx->string("Abracadabra"), "num" => $wddx->number(23), "arr" => $wddx->array(\@arr) });# serialize and print objectprint $wddx->serialize($obj);
    and see what you get:

    <wddxPacket version='1.0'> <header/><data><struct><var name='num'><number>23</number></var><var name='str'><string>Abracadabra</string></var><var name='arr'><array length='3'><string>huey</string><string>dewey</string><boolean value='true'/></array></var></struct></data></wddxPacket>
    This is a hash with three keys, one containing a string, the second a number, and the third an array. Now, try deserializing the WDDX packet generated from the code above.

    #!/usr/bin/perl # use WDDX moduleuse WDDX;# create WDDX object$wddx = new WDDX;# simulate a WDDX packet$packet = " <wddxPacket version='1.0'><header/><data><struct><varname='num'><number>23</number></var><varname='str'><string>Abracadabra</string></var><var name='arr'><arraylength='3'><string>huey</string><string>dewey</string><booleanvalue='true'/></array></var></struct></data></wddxPacket>";# deserialize packet into WDDX array object$obj = $wddx->deserialize($packet);# get reference to native Perl hash$hashref = $obj->as_hashref();# get keys@k = $obj->keys();# print keys and type of corresponding valuesforeach $k (@k){ print "$k --> " . $obj->get($k)->type . "\n";}
    Here's what you should see:

    num --> number str --> stringarr --> array

    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