++ed by:

2 PAUSE users

Randy J Ray


RPC::XML - A set of classes for core data, message and XML handling


    use RPC::XML;

    $req = new RPC::XML::request ('fetch_prime_factors',
    $resp = RPC::XML::Parser->new()->parse(STREAM);
    if (ref($resp))
        return $resp->value->value;
        die $resp;


The RPC::XML package is a reference implementation of the XML-RPC standard. As a reference implementation, it is geared more towards clarity and readability than efficiency.

The package provides a set of classes for creating values to pass to the constructors for requests and responses. These are lightweight objects, most of which are implemented as tied scalars so as to associate specific type information with the value. Classes are also provided for requests, responses, faults (errors) and a parser based on the XML::Parser package from CPAN.

This module does not actually provide any transport implementation or server basis. For these, see RPC::XML::Client and RPC::XML::Server, respectively.


At present, only two functions are available for import. They must be explicitly imported as part of the use statement, or with a direct call to import:


Convert the integer time value in $time to a ISO 8601 string in the UTC time zone. This is a convenience function for occassions when the return value needs to be of the dateTime.iso8601 type, but the value on hand is the return from the time built-in.


Converts the passed-in arguments to datatype objects. Any that are already encoded as such are passed through unchanged. The routine is called recursively on hash and array references. Note that this routine can only deduce a certain degree of detail about the values passed. Boolean values will be wrongly encoded as integers. Pretty much anything not specifically recognizable will get encoded as a string object. Thus, for types such as fault, the ISO time value, base-64 data, etc., the program must still explicitly encode it. However, this routine will hopefully simplify things a little bit for a majority of the usage cases.


The classes provided by this module are broken into two groups: datatype classes and message classes.

Data Classes

The following data classes are provided by this library. Each of these provide at least new, value and as_string methods. Note that these classes are designed to create throw-away objects. There is currently no mechanism for changing the value stored within one of these object after the constructor returns. It is assumed that a new object would be created, instead.

The new methods are constructors, value returns the value stored within the object (processed recursively for arrays and structs), and as_string stringifies the object as a chunk of XML with relative indention for clarity. The as_string method takes as its first argument a numeric indention level, which is applied as a base indention for output. Other arguments are specified with the classes.


Creates an integer value. Constructor expects the integer value as an argument.


This is like the int class.


Creates a floating-point value.


Creates an arbitrary string. No special encoding is done to the string (aside from XML document encoding, covered later) with the exception of the <, > and & characters, which are XML-escaped during object creation, and then reverted when the value method is called.


Creates a boolean value. The value returned will always be either of 1 or 0, for true or false, respectively. When calling the constructor, the program may specify any of: 0, no, false, 1, yes, true.


Creates an instance of the XML-RPC dateTime.iso8601 type. The specification for ISO 8601 may be found elsewhere. No processing is done to the data.


Creates an object that encapsulates a chunk of data that will be treated as base-64 for transport purposes. The value may be passed in as either a string or as a scalar reference. Additionally, a second (optional) parameter may be passed, that if true identifies the data as already base-64 encoded. If so, the data is decoded before storage. The value method returns decoded data, and the as_string method encodes it before stringification.


Creates an array object. The constructor takes zero or more data-type instances as arguments, which are inserted into the array in the order specified. value returns an array reference of native Perl types. If a non-null value is passed as an argument to value(), then the array reference will contain the datatype objects (a shallow copy rather than a deep one).


Creates a struct object, the analogy of a hash table in Perl. The keys are ordinary strings, and the values must all be data-type objects. The value method returns a hash table reference, with native Perl types in the values. Key order is not preserved. Key strings are not encoded for special XML characters, so the use of such (<, >, etc.) is discouraged. If a non-null value is passed as an argument to value(), then the hash reference will contain the datatype objects (a shallow copy rather than a deep one).


A fault object is a special case of the struct object that checks to ensure that there are two keys, faultCode and faultString.

As a matter of convenience, since the contents of a RPC::XML::fault structure are specifically defined, the constructor may be called with exactly two arguments, the first of which will be taken as the code, and the second as the string. They will be converted to RPC::XML types automatically and stored by the pre-defined key names.

Message Classes

The message classes are used both for constructing messages for outgoing communication as well as representing the parsed contents of a received message. Both implement the following methods:


This is the constructor method for the two message classes. The response class may have only a single value (as a response is currently limited to a single return value), and requests may have as many arguments as appropriate. In both cases, the arguments are passed to the exported smart_encode routine described earlier.


Returns the message object expressed as an XML document. The optional indent parameter causes the body to be indented that many steps (each step is two spaces).

The two message-object classes are:


This creates a request object. A request object expects the first argument to be the name of the remote routine being called, and all remaining arguments are the arguments to that routine. Request objects have the following methods (besides new and as_string):


The name of the remote routine that the request will call.


Returns a list reference with the arguments that will be passed. No arguments will result in a reference to an empty list.


The response object is much like the request object in most ways. They may take only one argument, as that is all the specification allows for in a response. Responses have the following methods (in addition to new and as_string):


The name of the remote method that was called. This will not be set in compatibility mode.


The value the response is returning. It will be a RPC::XML data-type.


A boolean test whether or not the response is signalling a fault. This is the same as taking the value method return value and testing it, but is provided for clarity and simplicity.


All constructors return undef upon failure, with the error message available in the package-global variable $RPC::XML::ERROR.


As was stated at the beginning, this is a reference implementation in which clarity of process and readability of the code took precedence over general efficiency. Much, if not all, of this can be written more compactly and/or efficiently. However, if done that will be done in a separate module so that this remains legible to the casual programmer.


The XML-RPC standard is Copyright (c) 1998-2001, UserLand Software, Inc. See <http://www.xmlrpc.com> for more information about the XML-RPC specification.


This module is licensed under the terms of the Artistic License that covers Perl itself. See <http://language.perl.com/misc/Artistic.html> for the license itself.


RPC::XML::Client, RPC::XML::Server, RPC::XML::Parser, XML::Parser


Randy J. Ray <rjray@blackperl.com>