1RPC::XML(3) User Contributed Perl Documentation RPC::XML(3)
2
3
4
6 RPC::XML - A set of classes for core data, message and XML handling
7
9 use RPC::XML;
10
11 $req = RPC::XML::request->new('fetch_prime_factors',
12 RPC::XML::int->new(985120528));
13 ...
14 $resp = RPC::XML::Parser->new()->parse(STREAM);
15 if (ref($resp))
16 {
17 return $resp->value->value;
18 }
19 else
20 {
21 die $resp;
22 }
23
25 The RPC::XML package is an implementation of the XML-RPC standard.
26
27 The package provides a set of classes for creating values to pass to
28 the constructors for requests and responses. These are lightweight
29 objects, most of which are implemented as tied scalars so as to
30 associate specific type information with the value. Classes are also
31 provided for requests, responses, faults (errors) and a parser based on
32 the XML::Parser package from CPAN.
33
34 This module does not actually provide any transport implementation or
35 server basis. For these, see RPC::XML::Client and RPC::XML::Server,
36 respectively.
37
39 At present, three simple functions are available for import. They must
40 be explicitly imported as part of the "use" statement, or with a direct
41 call to "import":
42
43 time2iso8601([$time])
44 Convert the integer time value in $time (which defaults to calling
45 the built-in "time" if not present) to a ISO 8601 string in the UTC
46 time zone. This is a convenience function for occassions when the
47 return value needs to be of the dateTime.iso8601 type, but the
48 value on hand is the return from the "time" built-in.
49
50 smart_encode(@args)
51 Converts the passed-in arguments to datatype objects. Any that are
52 already encoded as such are passed through unchanged. The routine
53 is called recursively on hash and array references. Note that this
54 routine can only deduce a certain degree of detail about the values
55 passed. Boolean values will be wrongly encoded as integers. Pretty
56 much anything not specifically recognizable will get encoded as a
57 string object. Thus, for types such as "fault", the ISO time value,
58 base-64 data, etc., the program must still explicitly encode it.
59 However, this routine will hopefully simplify things a little bit
60 for a majority of the usage cases.
61
62 In addition to these three, the following "helper" functions are also
63 available. They may be imported explicitly, or via a tag of ":types":
64
65 RPC_BOOLEAN RPC_INT RPC_I4 RPC_I8 RPC_DOUBLE
66 RPC_DATETIME_ISO8601 RPC_BASE64 RPC_STRING RPC_NIL
67
68 Each creates a data object of the appropriate type from a single value
69 (or, in the case of RPC_NIL, from no value). They are merely short-
70 hand for calling the constructors of the data classes directly.
71
72 All of the above (helpers and the first three functions) may be
73 imported via the tag ":all".
74
76 The classes provided by this module are broken into two groups:
77 datatype classes and message classes.
78
79 Data Classes
80 The following data classes are provided by this library. Each of these
81 provide at least the set of methods below. Note that these classes are
82 designed to create throw-away objects. There is currently no mechanism
83 for changing the value stored within one of these object after the
84 constructor returns. It is assumed that a new object would be created,
85 instead.
86
87 The common methods to all data classes are:
88
89 new($value)
90 Constructor. The value passed in is the value to be encapsulated in
91 the new object.
92
93 value
94 Returns the value kept in the object. Processes recursively for
95 "array" and "struct" objects.
96
97 as_string
98 Returns the value as a XML-RPC fragment, with the proper tags, etc.
99
100 serialize($filehandle)
101 Send the stringified rendition of the data to the given file
102 handle. This allows messages with arbitrarily-large Base-64 data
103 within them to be sent without having to hold the entire message
104 within process memory.
105
106 length
107 Returns the length, in bytes, of the object when serialized into
108 XML. This is used by the client and server classes to calculate
109 message length.
110
111 type
112 Returns the type of data being stored in an object. The type
113 matches the XML-RPC specification, so the normalized form
114 "datetime_iso8601" comes back as "dateTime.iso8601".
115
116 is_fault
117 All types except the fault class return false for this. This is to
118 allow consistent testing of return values for fault status, without
119 checking for a hash reference with specific keys defined.
120
121 The classes themselves are:
122
123 RPC::XML::int
124 Creates an integer value. Constructor expects the integer value as
125 an argument.
126
127 RPC::XML::i4
128 This is like the "int" class. Note that services written in
129 strictly-typed languages such as C, C++ or Java may consider the
130 "i4" and "int" types as distinct and different.
131
132 RPC::XML::i8
133 This represents an 8-byte integer, and is not officially supported
134 by the XML-RPC specification. This has been added to accommodate
135 services already in use that have chosen to add this extension.
136
137 RPC::XML::double
138 Creates a floating-point value.
139
140 RPC::XML::string
141 Creates an arbitrary string. No special encoding is done to the
142 string (aside from XML document encoding, covered later) with the
143 exception of the "<", ">" and "&" characters, which are XML-escaped
144 during object creation, and then reverted when the "value" method
145 is called.
146
147 RPC::XML::boolean
148 Creates a boolean value. The value returned will always be either
149 of 1 or 0, for true or false, respectively. When calling the
150 constructor, the program may specify any of: 0, "no", "false", 1,
151 "yes", "true".
152
153 RPC::XML::datetime_iso8601
154 Creates an instance of the XML-RPC "dateTime.iso8601" type. The
155 specification for ISO 8601 may be found elsewhere. No processing is
156 done to the data.
157
158 RPC::XML::nil
159 Creates a "nil" value. The value returned will always be undef. No
160 value should be passed when calling the constructor.
161
162 Note that nil is an extension to XML-RPC, which is not supported by
163 all implementations. $RPC::XML::ALLOW_NIL must be set to a non-
164 false value before objects of this type can be constructed. See
165 "The nil Datatype".
166
167 RPC::XML::base64
168 Creates an object that encapsulates a chunk of data that will be
169 treated as base-64 for transport purposes. The value may be passed
170 in as either a string or as a scalar reference. Additionally, a
171 second (optional) parameter may be passed, that if true identifies
172 the data as already base-64 encoded. If so, the data is decoded
173 before storage. The "value" method returns decoded data, and the
174 "as_string" method encodes it before stringification.
175
176 Alternately, the constructor may be given an open filehandle
177 argument instead of direct data. When this is the case, the data is
178 never read into memory in its entirety, unless the "value" or
179 "as_string" methods are called. This allows the manipulation of
180 arbitrarily-large Base-64-encoded data chunks. In these cases, the
181 flag (optional second argument) is still relevant, but the data is
182 not pre-decoded if it currently exists in an encoded form. It is
183 only decoded as needed. Note that the filehandle passed must be
184 open for reading, at least. It will not be written to, but it will
185 be read from. The position within the file will be preserved
186 between operations.
187
188 Because of this, this class supports a special method called
189 "to_file", that takes one argument. The argument may be either an
190 open, writable filehandle or a string. If it is a string, "to_file"
191 will attempt to open it as a file and write the decoded data to it.
192 If the argument is a an open filehandle, the data will be written
193 to it without any pre- or post-adjustment of the handle position
194 (nor will it be closed upon completion). This differs from the
195 "serialize" method in that it always writes the decoded data (where
196 the other always writes encoded data), and in that the XML opening
197 and closing tags are not written. The return value of "to_file" is
198 the size of the data written in bytes.
199
200 RPC::XML::array
201 Creates an array object. The constructor takes zero or more data-
202 type instances as arguments, which are inserted into the array in
203 the order specified. "value" returns an array reference of native
204 Perl types. If a non-null value is passed as an argument to
205 "value()", then the array reference will contain datatype objects
206 (a shallow rather than deep copy).
207
208 RPC::XML::struct
209 Creates a struct object, the analogy of a hash table in Perl. The
210 keys are ordinary strings, and the values must all be data-type
211 objects. The "value" method returns a hash table reference, with
212 native Perl types in the values. Key order is not preserved. Key
213 strings are now encoded for special XML characters, so the use of
214 such ("<", ">", etc.) should be transparent to the user. If a non-
215 null value is passed as an argument to "value()", then the hash
216 reference will contain the datatype objects rather than native Perl
217 data (a shallow vs. deep copy, as with the array type above).
218
219 When creating RPC::XML::struct objects, there are two ways to pass
220 the content in for the new object: Either an existing hash
221 reference may be passed, or a series of key/value pairs may be
222 passed. If a reference is passed, the existing data is copied (the
223 reference is not re-blessed), with the values encoded into new
224 objects as needed.
225
226 RPC::XML::fault
227 A fault object is a special case of the struct object that checks
228 to ensure that there are two keys, "faultCode" and "faultString".
229
230 As a matter of convenience, since the contents of a RPC::XML::fault
231 structure are specifically defined, the constructor may be called
232 with exactly two arguments, the first of which will be taken as the
233 code, and the second as the string. They will be converted to
234 RPC::XML types automatically and stored by the pre-defined key
235 names.
236
237 Also as a matter of convenience, the fault class provides the
238 following accessor methods for directly retrieving the integer code
239 and error string from a fault object:
240
241 code
242 string
243
244 Both names should be self-explanatory. The values returned are Perl
245 values, not RPC::XML class instances.
246
247 Message Classes
248 The message classes are used both for constructing messages for
249 outgoing communication as well as representing the parsed contents of a
250 received message. Both implement the following methods:
251
252 new This is the constructor method for the two message classes. The
253 response class may have only a single value (as a response is
254 currently limited to a single return value), and requests may have
255 as many arguments as appropriate. In both cases, the arguments are
256 passed to the exported "smart_encode" routine described earlier.
257
258 as_string
259 Returns the message object expressed as an XML document. The
260 document will be lacking in linebreaks and indention, as it is not
261 targeted for human reading.
262
263 serialize($filehandle)
264 Serialize the message to the given file-handle. This avoids
265 creating the entire XML message within memory, which may be
266 relevant if there is especially-large Base-64 data within the
267 message.
268
269 length
270 Returns the total size of the message in bytes, used by the client
271 and server classes to set the Content-Length header.
272
273 The two message-object classes are:
274
275 RPC::XML::request
276 This creates a request object. A request object expects the first
277 argument to be the name of the remote routine being called, and all
278 remaining arguments are the arguments to that routine. Request
279 objects have the following methods (besides "new" and "as_string"):
280
281 name
282 The name of the remote routine that the request will call.
283
284 args
285 Returns a list reference with the arguments that will be
286 passed. No arguments will result in a reference to an empty
287 list.
288
289 RPC::XML::response
290 The response object is much like the request object in most ways.
291 It may take only one argument, as that is all the specification
292 allows for in a response. Responses have the following methods (in
293 addition to "new" and "as_string"):
294
295 value
296 The value the response is returning. It will be a RPC::XML
297 data-type.
298
299 is_fault
300 A boolean test whether or not the response is signalling a
301 fault. This is the same as taking the "value" method return
302 value and testing it, but is provided for clarity and
303 simplicity.
304
306 All constructors (in all data classes) return "undef" upon failure,
307 with the error message available in the package-global variable
308 $RPC::XML::ERROR.
309
311 The following global variables may be changed to control certain
312 behavior of the library. All variables listed below may be imported
313 into the application namespace when you "use" RPC::XML:
314
315 $ENCODING
316 This variable controls the character-set encoding reported in
317 outgoing XML messages. It defaults to "us-ascii", but may be set to
318 any value recognized by XML parsers.
319
320 $FORCE_STRING_ENCODING
321 By default, "smart_encode" uses heuristics to determine what
322 encoding is required for a data type. For example, 123 would be
323 encoded as "int", where 3.14 would be encoded as "double". In some
324 situations it may be handy to turn off all these heuristics, and
325 force encoding of "string" on all data types encountered during
326 encoding. Setting this flag to "true" will do just that.
327
328 Defaults to "false".
329
330 $ALLOW_NIL
331 By default, the XML-RPC "nil" extension is not supported. Set this
332 to a non-false value to allow use of nil values. Data objects that
333 are "nil" are represented as undef by Perl. See "The nil Datatype".
334
336 Starting with release 0.64 of this package, some small extensions to
337 the core XML-RPC standard have been supported. These are summarized
338 here, with additional caveats as appropriate.
339
340 XML Document Encoding
341 The i8 Datatype
342 The nil Datatype
344 This began as a reference implementation in which clarity of process
345 and readability of the code took precedence over general efficiency. It
346 is now being maintained as production code, but may still have parts
347 that could be written more efficiently.
348
350 Please report any bugs or feature requests to "bug-rpc-xml at
351 rt.cpan.org", or through the web interface at
352 http://rt.cpan.org/NoAuth/ReportBug.html?Queue=RPC-XML
353 <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=RPC-XML>. I will be
354 notified, and then you'll automatically be notified of progress on your
355 bug as I make changes.
356
358 · RT: CPAN's request tracker
359
360 http://rt.cpan.org/NoAuth/Bugs.html?Dist=RPC-XML
361 <http://rt.cpan.org/NoAuth/Bugs.html?Dist=RPC-XML>
362
363 · AnnoCPAN: Annotated CPAN documentation
364
365 http://annocpan.org/dist/RPC-XML <http://annocpan.org/dist/RPC-XML>
366
367 · CPAN Ratings
368
369 http://cpanratings.perl.org/d/RPC-XML
370 <http://cpanratings.perl.org/d/RPC-XML>
371
372 · Search CPAN
373
374 http://search.cpan.org/dist/RPC-XML
375 <http://search.cpan.org/dist/RPC-XML>
376
377 · Source code on GitHub
378
379 http://github.com/rjray/rpc-xml/tree/master
380 <http://github.com/rjray/rpc-xml/tree/master>
381
383 This file and the code within are copyright (c) 2009 by Randy J. Ray.
384
385 Copying and distribution are permitted under the terms of the Artistic
386 License 2.0
387 (http://www.opensource.org/licenses/artistic-license-2.0.php
388 <http://www.opensource.org/licenses/artistic-license-2.0.php>) or the
389 GNU LGPL 2.1 (http://www.opensource.org/licenses/lgpl-2.1.php
390 <http://www.opensource.org/licenses/lgpl-2.1.php>).
391
393 The XML-RPC standard is Copyright (c) 1998-2001, UserLand Software,
394 Inc. See <http://www.xmlrpc.com> for more information about the XML-
395 RPC specification.
396
398 RPC::XML::Client, RPC::XML::Server, RPC::XML::Parser, XML::Parser
399
401 Randy J. Ray <rjray@blackperl.com>
402
403
404
405perl v5.12.0 2009-09-03 RPC::XML(3)