XML technologies / XML / Perl & XML / RSS, SOAP and other XML applications / SOAP::Lite
(Excerpt from “Perl & XML” by Erik T. Ray & Jason McIntosh)
Finally, we reach Perl/XML software abstracting its XML foundation so far that it floats almost absurdly above this book’s subject. It still deserves a closer look. These modules and extensions resemble helper modules in the style of XML::RSS. Both focus on a particular topic or document format rather than general XML solutions. The difference is how far the programmer’s view is detached from raw data flow. Add enough abstraction layers and XML disappears from sight.
This is legitimate and sensible: when writing software based on SOAP or XML-RPC, XML itself is no longer our concern. It is a black box: if it works, we are satisfied. A good module still provides some access to raw XML, just in case.
Simple Object Access Protocol (SOAP) gives access to the full power of object-oriented web services.
Note: Despite the name, web services need not concern the World Wide Web. A service is essentially software behind a URI, waiting for requests and responding more or less usefully. Usually it uses an ordinary HTTP or web server. More recently, attention has turned to services exposing persistent objects and their methods, letting programmers incorporate remote software without that fact being apparent.
We construct and use objects whose classes reside at the far end of a URI. The remote language can be anything: the protocol automatically transforms object methods into a standardized XML API. With proper documentation, a class can be treated as though implemented in a local file. Detailed knowledge of class and instance methods is necessary, so documentation really matters.
At this point, working with XML is hard to miss. At least with RSS, interface method names roughly match the underlying document. We want to go further and leave this dreadful, machine-unreadable document behind entirely.
The name SOAP::Lite refers to the effort required, not weight. Installing it creates a long list of available Perl modules, some supporting different transport protocols.
Note: HTTP is the usual way to transport SOAP objects, but raw TCP, SMTP and Jabber can also be used.
SOAP::Litesupports them all.
Thus mod_perl assists in implementing web services with SOAPy, with abundant documentation and examples. Installation also provides modules with similar interfaces for XML-RPC, SOAP’s non-object-oriented cousin. SOAP::Lite is one of those versatile Perl tools: for web-service programming, it is what CGI.pm is for dynamic web pages.
Let us get to work and dirty our hands by dipping them in soap.
First example: Temperature conversion
Every programming book needs a temperature-conversion example, right? (Europeans find this hard to understand. US confusion over Fahrenheit and Celsius is comparable to mentally adjusting to the euro.) We have not had one yet. In this example borrowed from the SYNOPSIS in the SOAP::Lite documentation, we write a program with a function named f2c.
use SOAP::Lite;
print SOAP::Lite
-> uri('http://www.soaplite.com/Temperatures')
-> proxy('http://services.soaplite.com/temper.cgi')
-> f2c(32)
-> result;Running it as a Perl script produces the correct answer: 0. This assumes that SOAP::Lite is installed on your machine.
Second example: Looking up ISBNs
This more object-oriented example uses a small module on the author’s personal web server. It takes an ISBN and returns an XML document in Dublin Core format listing matching books:
my ($isbn_number) = @ARGV;
use SOAP::Lite +autodispatch=>
uri=>'http://www.jmac.org/ISBN',
proxy=>'http://www.jmac.org/projects/bookdb/isbn/lookup.pl';
my $isbn_obj = ISBN->new;
# Die Methode 'get_dc' ruft Informationen im Format des Dublin Core ab
my $result = $isbn_obj->get_dc($isbn_number); The trick is loading the module ISBN.pm from the server. It is an ordinary Perl module that could equally be loaded locally with use ISBN if a local copy existed. The program above is therefore equivalent to this:
my ($isbn_number) = @ARGV;
use ISBN; # Diese Zeile ersetzt das obige 'use SOAP::Lite'.
my $isbn_obj = ISBN->new;
# Die Methode 'get_dc' ruft Informationen im Format des Dublin Core ab
my $result = $isbn_obj->get_dc($isbn_number); Calling SOAP::Lite with a little magic locating the right server and URI eliminates the local module copy while retaining its API. Even replacing the module could go unnoticed if its interface remained intact. In the language-dependent world of web services, that is all that matters.
What has this to do with XML? Turn a valve and let raw data flow. See what the class constructor of ISBN actually returns when the option outputxml is enabled:
my ($isbn_number) = @ARGV;
use SOAP::Lite +autodispatch=>
uri=>'http://www.jmac.org/ISBN', outputxml=>1,
proxy=>'http://www.jmac.org/projects/bookdb/isbn/lookup.pl';
my $isbn_xml = ISBN->new;
print "$isbn_xml\n";The result should resemble this:
<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENC="http://schemas.
xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/
encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://
www.w3.org/1999/XMLSchema-instance" xmlns:xsd="http://www.w3.org/1999/XMLSchema" xmlns:
namesp1="http://www.jmac.org/ISBN"><SOAP-ENV:Body><namesp2:newResponse xmlns:
namesp2="http://www.jmac.org/ISBN"><ISBN xsi:type="namesp1:ISBN"/></namesp2:newResponse>
</SOAP-ENV:Body></SOAP-ENV:Envelope> The second example needed shortening. We could no longer call the method get_dc because enabling outputxml makes the constructor return a scalar string instead of an object. We printed that XML string with print. This can be circumvented by putting the string into the magical class SOAP::Deserializer, turning XML back into an object:
# Wir setzen das vorige Beispiel fort...
my $deserial = SOAP::Deserializer->new;
my $isbn_obj = $deserial->deserialize($isbn_xml);
# Jetzt könnten wir den fehlenden Rest aus dem ersten Beispiel übernehmen. A little extra effort therefore yields both raw XML and the black box of a SOAP::Lite object. This feature supports applications far beyond this book. Conversely, raw XML access also opens up many possible sources of error.
The Perl module SOAP::Lite is magical in several ways. SOAP itself is not: it is a protocol and document format with unusual namespaces, elements and attributes, as seen in the XML produced by SOAP::Lite. That document conforms to the public SOAP specification (Version 1.2).
Conformance enables clever techniques. A SOAP application might use SOAP::Lite normally, then intervene, modify raw XML using any method from this book, and return it to SOAP::Lite for further processing.
Using SOAP::Lite usually requires no knowledge of XML and Perl. Most applications will be satisfied by its features, but freedom is valuable. Knowledge is power: here, knowledge of Perl and XML.
This concludes the survey of Perl/XML applications. Next come strategies for implementing your own.
Copyright © 2003 O'Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “Perl & XML” is subject to the same conditions as the bound edition: the work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstraße 81, 50670 Köln, kommentar(at)oreilly.de