Sign In/My Account | View Cart  
advertisement
-->




   Print.Print
Email.Email article link
The Perl Cookbook, 2nd Edition (cover)

Perl Recipe of the Day

The following recipe is from Perl Cookbook, 2nd Edition, by Tom Christiansen and Nathan Torkington. All links in this recipe point to the online version of the book on the Safari Bookshelf.

Buy it now, or read it online on the Safari Bookshelf.


18.12. Writing an XML-RPC Client

18.12.3. Discussion

A single XML-RPC server may run many services, differentiated by their method name: ClassName.handler corresponds to ClassName->handler on the server side; A.B.method corresponds to A::B->method; and a call to handler corresponds to main->handler.

The proxy is the actual URL of the server. If you're using a CGI server, the proxy method looks something like this:

$server->proxy("http://server.example.com/path/to/server.cgi")

There are three ways to invoke remote methods. The first way is to use the call method on your XMLRPC::Lite object. The first argument to call is the remote method name, and the remaining arguments are parameters for the remote method:

$returned = $server
         -> call("getRecordByNumber", 12, { format => "CSV" })
         -> result;

The second way to invoke a remote method is to call that method on the XMLRPC::Lite object. This works only when the remote method name isn't the same as a method provided by the XMLRPC::Lite object. For example:

$returned = $server
         -> getRecordByNumber(12, { format => "CSV" })
         -> result;

The last way to invoke a remote method is with autodispatch, turning unrequired function calls and method invocations in your Perl program into XML-RPC requests. Enable autodispatch with:

use XMLRPC::Lite +autodispatch =>
  proxy => "http://server.example.com/path";

$returned = getRecordByNumber(12, { format => "CSV" });

A critical difference between autodispatch and the other styles is that autodispatch automatically decodes the result into a Perl value for you. When you use an XMLRPC::Lite object, you must explicitly invoke the result method to decode the XML-RPC response into a Perl value.


View the past week's recipes: Today | Yesterday | 3 days ago | 4 days ago | 5 days ago | 6 days ago | A week ago

Sponsored By: