=head1 NAME WDDX.pm - a module for reading and writing WDDX packets =head1 SYNOPSIS use WDDX; my $wddx = new WDDX; ########################## ## Serialization example my $wddx_hash = $wddx->hash( { str => $wddx->string( "Welcome to WDDX!\n" ), num => $wddx->number( -12.456 ), date => $wddx->datetime( date ), bool => $wddx->boolean( 1 ), arr => $wddx->array( [ $wddx->boolean( 0 ), $wddx->number( 10 ), $wddx->string( "third element" ), ] ), rec => $wddx->recordset( [ "NAME", "AGE" ], [ "string", "number" ], [ [ "John Doe", 34 ], [ "Jane Doe", 25 ], [ "Fred Doe", 90 ], ] ), obj => $wddx->hash( { str => $wddx->string( "a string" ), num => $wddx->number( 3.14159 ), } ), bin => $wddx->binary( $img_data ), null => $wddx->null(), } ); print $wddx->header; print $wddx->serialize( $wddx_hash ); ########################## ## Deserialization example my $wddx_request = $wddx->deserialize( $packet ); # Assume that our code expects an array $wddx_request->type eq "array" or die "Invalid request"; my $array_ref = $wddx_request->as_arrayref; =head1 DESCRIPTION =head2 About WDDX This is from the WDDX.org web site: "The Web Distributed Data Exchange, or WDDX, is a free, open XML-based technology that allows Web applications created with any platform to easily exchange data with one another over the Web." For more information about WDDX, visit http://www.wddx.org/. For information about using Perl with WDDX (including examples) you can also visit http://www.scripted.com/wddx/. =head2 WDDX and Perl WDDX defines basic data types that mirror the data types available in other common programming languages. Many of these data types don't have corresponding data types in Perl. To Perl, strings, numbers, booleans, and dates are just scalars. However, in order to communicate effectively with other languages (and this is the point of WDDX), you do have to learn the basic WDDX data types. Here is a table that maps the WDDX data type to Perl, along with the intermediate object WDDX.pm represents it as: WDDX Type WDDX.pm Data Object Perl Type --------- ------------------- --------- String WDDX::String Scalar Number WDDX::Number Scalar Boolean WDDX::Boolean Scalar (1 or "") Datetime WDDX::Datetime Scalar (seconds since epoch) Null WDDX::Null Scalar (undef) Binary WDDX::Binary Scalar Array WDDX::Array Array Struct WDDX::Struct Hash Recordset WDDX::Recordset WDDX::Recordset In languages that have data types similar to the WDDX data types, the WDDX modules allow you to convert directly from a variable to a WDDX packet and vice versa. This Perl implementation is different; here you must always go through an intermediate stage where the data is represented by an object with a corresponding data type. These objects can be converted to a WDDX packet, converted to a basic Perl type, or converted to JavaScript code (which will recreate the data for you in JavaScript). We will refer to these objects as I throughout this documentation. =head1 Requirements This module requires L and L, which are both available on CPAN at http://www.cpan.org/. Windows users note: These modules use compiled code, but I have been told that they are both included with recent distributions of ActiveState Perl. =head1 METHODS =over =item new This creates a new WDDX object. You need one of these to do pretty much anything else. It doesn't take any arguments. =item $wddx->deserialize( $string_or_filehandle ) This method deserializes a WDDX packet and returns a data object. Note that you can pass either a string or a reference to an open filehandle containing a packet (XML::Parser is flexible this way): $wddx_obj = $wddx->deserialize( $packet ); # OR $wddx_obj = $wddx->deserialize( \*HANDLE ); If WDDX.pm or the underlying L finds any errors with the structure of the WDDX packet, then it will C with an error message that identifies the problem. If you don't want this to terminate your script, you will have to place this call within an C block to trap the C. =item $wddx->serialize( $wddx_obj ) This accepts a data object as an argument and returns a WDDX packet. This method calls the as_packet() method on the data object it receives. However, this method does provide one feature that as_packet() does not. If C<$WDDX::INDENT> is set to a defined value, then the generated WDDX packet is indented using C<$WDDX::INDENT> as the unit of indentation. Otherwise packets are generated without extra whitespace. Note that the generated packet is not a valid XML document without the header, see below. =item $wddx->header This returns a header that should accompany every serialized packet you send. =back =head1 WDDX DATA OBJECTS =head2 Common Methods All of the WDDX data objects share the following common methods: =over =item $wddx_obj->type This returns the data type of the object. It is lowercase and maps to the package name without the WDDX prefix. For example, type will return "string" for WDDX::String objects, "datetime" for WDDX::Datetime objects, etc. =item $wddx_obj->as_packet This returns a WDDX packet for the object. You can also do this by passing the object to the C<$wddx->serialize> method. See the warning in C<$wddx->header>. =item $wddx_obj->as_javascript( $js_varname ) This method takes the name of a JavaScript variable and returns the actual JavaScript code to assign this data object to the given JavaScript variable. No temporary variables are created to avoid any danger of variable name collisions. Example: $options[0] = $wddx->string( "First Choice" ); $options[1] = $wddx->string( "Second Choice" ); $options[2] = $wddx->string( "Third Choice" ); $w_array = $wddx->array( \@options ); print $w_array->as_javascript( "myArray" ); This prints the text (new lines added for readability): myArray=new Array(); myArray[0]="First Choice"; myArray[1]="Second Choice"; myArray[2]="Third Choice"; All data types are supported, and arrays and hashes (structs) can nest to any level. Recordset and binary objects require the JavaScript WddxRecordset and WddxBinary constructors. The easiest way to include these is to add a reference to the wddx.js file: wddx.js is the WDDX library for JavaScript. It is available as part of the WDDX SDK at http://www.wddx.org/. =back =head2 WDDX::String =over =item $wddx->string( 'Just a bunch of text...' ) This creates a WDDX string object. Strings contain 8 bit characters, can be any length, and should not include embedded nulls. However, control characters and characters that have special meaning for XML (like E, E, and E) are safely encoded for you. =item $w_string->as_scalar This returns the value of the WDDX::String as a Perl scalar. =back =head2 WDDX::Number =over =item $wddx->number( 3.14159 ) This creates a WDDX number object. Numbers are restricted to +/-1.7e308 and if you exceed these bounds this method dies with an error. Floating point numbers are restricted to 15 digits of accuracy past the decimal. If you exceed this then the number is truncated to 15 digits with a warning. If you pass a non-numeric scalar to this, then it is simply treated as a number: Perl will attempt to translate it, will probably use zero, and will issue a warning. =item $w_number->as_scalar This returns the value of the WDDX::Number as a Perl scalar. =back =head2 WDDX::Boolean =over =item $wddx->boolean( 1 ) This creates a WDDX boolean object. It simply tests the argument in a boolean context, so "0" and "" are false and anything else is true. =item $w_boolean->as_scalar This returns the value of the WDDX::Boolean as a Perl scalar. True is represented by 1 and false is represented by an empty string. =back =head2 WDDX::Datetime =over =item $wddx->datetime This creates a WDDX Datetime object. =item $w_datetime->use_timezone_info( 1 ) This sets or reads the flag that says whether to include the timezone info (local hour and minute offset from UTC) in WDDX packets created from this object. By default this is turned on for new objects. You can turn it off by passing a false (but not undef) argument to this method. When a WDDX::Datetime object is deserialized from a packet, this method will indicate whether timezone information was present in that packet. =item $w_datetime->as_scalar This returns the value of the WDDX::Datetime as a Perl scalar. It contains the number of seconds since the epoch localized for the current machine (like Perl's built-in C