<HTML ><HEAD ><TITLE >Perl $B$G$N(B XML-RPC $B$N;H$$J}(B</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.54"><LINK REL="HOME" TITLE="XML-RPC HOWTO" HREF="index.html"><LINK REL="PREVIOUS" TITLE="API $B$N;vNc(B - sumAndDifference" HREF="xmlrpc-howto-api.html"><LINK REL="NEXT" TITLE="Python $B$G$N(B XML-RPC $B$N;H$$J}(B" HREF="xmlrpc-howto-python.html"></HEAD ><BODY CLASS="SECTION" BGCOLOR="#FFFFFF" TEXT="#000000" ><DIV CLASS="NAVHEADER" ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >XML-RPC HOWTO</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="xmlrpc-howto-api.html" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="xmlrpc-howto-python.html" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="SECTION" ><H1 CLASS="SECTION" ><A NAME="XMLRPC-HOWTO-PERL" >6. Perl $B$G$N(B XML-RPC $B$N;H$$J}(B</A ></H1 ><P >Ken MacLeod $B$O(B Perl $BMQ(B XML-RPC $B$r<BAu$7$F$$$^$9!#H`$N(B Frontier::RPC $B%b%8%e!<%k$OH`$N(B<A HREF="http://bitsko.slc.ut.us/~ken/xml-rpc/" TARGET="_top" >$B%&%'%V%5%$%H(B</A >$B$b$7$/$O(B <A HREF="http://www.cpan.org/" TARGET="_top" >CPAN</A > $B$rDL$8$FF~<j$G$-$^$9!#(B </P ><P >Frontier::RPC $B$r%$%s%9%H!<%k$9$k$K$O!"I8=`E*$J$d$jJ}$G%Q%C%1!<(B $B%8$r%@%&%s%m!<%I$7$F%3%s%Q%$%k$7$^$9(B -</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><FONT COLOR="#000000" ><PRE CLASS="PROGRAMLISTING" >bash$ gunzip -c Frontier-RPC-0.07b1.tar.gz | tar xvf - bash$ cd Frontier-RPC-0.07b1 bash$ perl Makefile.PL bash$ make bash$ make test bash$ su -c 'make install'</PRE ></FONT ></TD ></TR ></TABLE ><P >($B%&%#%s%I%&4D6-$d%k!<%H8"8B$NL5$$>l9g!"$o$:$+$K0c$C$?<j=g$H$J(B $B$k$G$7$g$&!#>\:Y$O(B Perl $B$NJ8=q$r;29M$K$7$F$/$@$5$$!#(B)</P ><DIV CLASS="SECTION" ><H2 CLASS="SECTION" ><A NAME="XMLRPC-HOWTO-PERL-CLIENT" >6.1. Perl $B%/%i%$%"%s%H(B</A ></H2 ><P >$B<!$N%W%m%0%i%`$O(B Perl $B$+$i(B XML-RPC $B%5!<%P$N%3!<%k$NJ}K!$r<((B $B$7$^$9!#(B</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><FONT COLOR="#000000" ><PRE CLASS="PROGRAMLISTING" >use Frontier::Client; # Make an object to represent the XML-RPC server. $server_url = 'http://xmlrpc-c.sourceforge.net/api/sample.php'; $server = Frontier::Client->new(url => $server_url); # Call the remote server and get our result. $result = $server->call('sample.sumAndDifference', 5, 3); $sum = $result->{'sum'}; $difference = $result->{'difference'}; print "Sum: $sum, Difference: $difference\n";</PRE ></FONT ></TD ></TR ></TABLE ></DIV ><DIV CLASS="SECTION" ><H2 CLASS="SECTION" ><A NAME="XMLRPC-HOWTO-PERL-SERVER" >6.2. $BFHN)7?(B Perl $B%5!<%P(B</A ></H2 ><P >The following program shows how to write an XML-RPC server in Perl:</P ><P >$B<!$N%W%m%0%i%`$O(B Perl $B$G$N(B XML-RPC $B%5!<%P$N=q$-J}$r<($7$^$9!#(B </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><FONT COLOR="#000000" ><PRE CLASS="PROGRAMLISTING" >use Frontier::Daemon; sub sumAndDifference { my ($x, $y) = @_; return {'sum' => $x + $y, 'difference' => $x - $y}; } # Call me as http://localhost:8080/RPC2 $methods = {'sample.sumAndDifference' => \&sumAndDifference}; Frontier::Daemon->new(LocalPort => 8080, methods => $methods) or die "Couldn't start HTTP server: $!";</PRE ></FONT ></TD ></TR ></TABLE ></DIV ><DIV CLASS="SECTION" ><H2 CLASS="SECTION" ><A NAME="XMLRPC-HOWTO-PERL-CGI" >6.3. CGI $B%Y!<%9$N(B Perl $B%5!<%P(B</A ></H2 ><P ><TT CLASS="LITERAL" >Frontier::RPC2</TT > $B$O(B CGI $B%Y!<%9%5!<%PMQ$N%5(B $B%]!<%H$OK\MhHw$($i$l$F$$$^$;$s!#$7$+$7(B<EM >$BBg>fIW(B</EM >$B!"(B $BI,MW$J$[$H$s$I$N5!G=$OMQ0U$5$l$F$$$^$9!#(B</P ><P >$B<!$N%W%m%0%i%`%3!<%I$r(B <TT CLASS="FILENAME" >sumAndDifference.cgi</TT > $B$H$7$F!"%&%'%V%5!<%P$N(B <TT CLASS="FILENAME" >cgi-bin</TT > $B%G%#%l%/%H%j$KJ]B8$7$F$/$@$5$$(B (Unix $B%7%9%F%`>e$G!"(B<TT CLASS="LITERAL" >chmod +x sumAndDifference.cgi</TT > $B$H(B $BF~NO$7!"<B9TB0@-$rM?$($kI,MW$,$"$k$G$7$g$&(B)$B!#(B</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><FONT COLOR="#000000" ><PRE CLASS="PROGRAMLISTING" >#!/usr/bin/perl -w use strict; use Frontier::RPC2; sub sumAndDifference { my ($x, $y) = @_; return {'sum' => $x + $y, 'difference' => $x - $y}; } process_cgi_call({'sample.sumAndDifference' => \&sumAndDifference}); #========================================================================== # CGI Support #========================================================================== # Simple CGI support for Frontier::RPC2. You can copy this into your CGI # scripts verbatim, or you can package it into a library. # (Based on xmlrpc_cgi.c by Eric Kidd <http://xmlrpc-c.sourceforge.net/>.) # Process a CGI call. sub process_cgi_call ($) { my ($methods) = @_; # Get our CGI request information. my $method = $ENV{'REQUEST_METHOD'}; my $type = $ENV{'CONTENT_TYPE'}; my $length = $ENV{'CONTENT_LENGTH'}; # Perform some sanity checks. http_error(405, "Method Not Allowed") unless $method eq "POST"; http_error(400, "Bad Request") unless $type eq "text/xml"; http_error(411, "Length Required") unless $length > 0; # Fetch our body. my $body; my $count = read STDIN, $body, $length; http_error(400, "Bad Request") unless $count == $length; # Serve our request. my $coder = Frontier::RPC2->new; send_xml($coder->serve($body, $methods)); } # Send an HTTP error and exit. sub http_error ($$) { my ($code, $message) = @_; print <<"EOD"; Status: $code $message Content-type: text/html <title>$code $message</title> <h1>$code $message</h1> <p>Unexpected error processing XML-RPC request.</p> EOD exit 0; } # Send an XML document (but don't exit). sub send_xml ($) { my ($xml_string) = @_; my $length = length($xml_string); print <<"EOD"; Status: 200 OK Content-type: text/xml EOD # We want precise control over whitespace here. print $xml_string; }</PRE ></FONT ></TD ></TR ></TABLE ><P >$B$"$J$?<+?H$N(B CGI $B%9%/%j%W%H$K%f!<%F%#%j%F%#%k!<%A%s$r%3%T!<(B $B$7$F$b$+$^$$$^$;$s!#(B</P ></DIV ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="xmlrpc-howto-api.html" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="xmlrpc-howto-python.html" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >API $B$N;vNc(B - <TT CLASS="FUNCTION" >sumAndDifference</TT ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" > </TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Python $B$G$N(B XML-RPC $B$N;H$$J}(B</TD ></TR ></TABLE ></DIV ></BODY ></HTML >