Sidebar


SOAP::Lite

Once you've seen the details of a SOAP request and response, forget them. The protocol is so simple, you could implement a client after sitting down with a good book on XML, a good book on HTTP, and a pot of strong coffee. Luckily, you don't have to roll your own—there are many SOAP implementations to choose from (see the "Online Resources" for a sampling). Perl developers will be happy to learn that SOAP::Lite is one such implementation of their chosen language.

You can download Paul Kulchenko's SOAP::Lite from the Web. Because SOAP::Lite is a Perl module, you'll need to have the Perl interpreter installed on your machine. Paul's Web site provides detailed instructions on the necessary prerequisites for SOAP::Lite, and how to install it. ActivePerl 5.6 for Windows also includes a version of SOAP::Lite.

Initializing

As with all Perl modules, you need to import SOAP::Lite into your Perl programs before you can use it. The statement use SOAP::Lite will do the trick.

To create a SOAP client in your program, you need to call at least two methods, uri and proxy. Although its name suggests otherwise, the uri method doesn't specify the URI of the Web service you want to use. Instead, it's an identifier. Using tempuri.org as a label is acceptable. The proxy method, on the other hand, needs the real URI of the service. For example, you'd need to pass localhost/bjepson/dbdemo.asmx to it.

If you're acting as a client of a .Net Web service, you need to toss in a call to on_action. The on_action method sets up a callback that helps create the SOAPAction header, as illustrated on line 4 of Listing 3. The on_action method call shown in Listing 5 uses an empty string to make sure that the delimiter between the URI and the method you want to call is a slash (/) character, and nothing else.

Invoking

Once you've imported SOAP::Lite and invoked uri, proxy, and on_action to create a SOAP client, you can invoke a method using the following syntax:

$soap->METHODNAME->result

The result method returns an array reference containing all the results of your method call. In Listing 5, you can see that the Perl script invokes the enumerateChoices method and stores the result in $result. In the last line of the program, I dereference the array as @$result, and turn it into a comma-delimited list using the Perl function join.

By downloading and installing the dbdemo.asmx code from the source code page, you can run Listing 5 and view the result of a service query. The result sent to the Perl client is "Jepstone, AS220, Web Techniques", which was pulled from the ADO data source using the Web service from dbdemo.asmx.

As usual, a few lines of Perl code can work magic. Of course, it wouldn't be so simple without the SOAP::Lite module. —BJ