Perl
  Home arrow Perl arrow Page 5 - 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 - 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 module use WDDX; # create WDDX object $wddx = new WDDX; # simulate a WDDX packet $packet = "<wddxPacket version='1.0'> <header/> <data> <array length='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 elements for ($i=0; $i<$length; $i++) { print "$$arrayref[$i]\n"; }
    Here's the output:

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

    #!/usr/bin/perl # include module use WDDX; # create WDDX object my $wddx = new WDDX; # create an array my @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 object print $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 module use WDDX; # create WDDX object $wddx = new WDDX; # simulate a WDDX packet $packet = " <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>"; # 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 values foreach $k (@k) { print "$k --> " . $obj->get($k)->type . "\n"; }
    Here's what you should see:

    num --> number str --> string arr --> array


     
     
    >>> 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