Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

This is the XML-RPC client.

#!/usr/bin/perl use strict; use warnings; use Frontier::Client; my $number; { print "Enter a number\n"; $number = <STDIN>; chomp($number); redo unless $number =~ /^\d+$/; } my $server_url = 'http://localhost/square/index.cgi/RPC2'; my $server = Frontier::Client->new(url => $server_url); my $result = $server->call('sample.square', $number); my $square = $result->{'square'}; my $difference = $result->{'difference'}; print "The square of $number is $square.\n";

This is the CGI::Application subclass which implements the functionality of the server.

package Square; use base 'CGI::Application'; sub setup { my ($self) = @_; $ENV{'PATH'} = ''; my @pi = split(m{/}, $self->query->path_info()); $self->start_mode(($pi[1] eq 'RPC2') ? 'RPC2' : 'view'); $self->run_modes( 'view' => 'view', 'RPC2' => 'rpc2', ); } sub view { my ($self) = @_; my $number = $self->query->param('number'); my $template = $self->load_tmpl('view'); $template->param(NUMBER => $number, SQUARE => square($number)->{square}, ); return $template->output; } sub rpc2 { my ($self) = @_; require Frontier::RPC2; my $rpc = Frontier::RPC2->new(); my $response = $rpc->serve($self->query->param('POSTDATA'), { 'sample.square' => \&square, }, ); $self->header_props( -type => 'text/xml', charset => 'UTF-8', ); return $response; } sub square { my ($x) = @_; return {square => ($x ** 2)}; } 1;

Here is the driver script that uses the above module:

#!/usr/bin/perl -T use warnings; use strict; use lib '.'; use Square; my $square = Square->new(); $square->run();

Here is the HTML::Template used in the view run mode.

<html> <head> <title>Square Two Numbers</title> </head> <body> <h1>Square Two Numbers</h1> <form action="http://localhost/square/index.cgi/view" method="POST +"> Enter a number: <input type="text" name="number"> <input type="submit"> </form> <!-- TMPL_IF NAME="number" --> <p>The square of <!-- TMPL_VAR NAME="number" --> is <!-- TMPL_V +AR NAME="square" -->.</p> <!-- /TMPL_IF --> </body> </html>

In reply to XML-RPC in a CGI::Application by jaldhar

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2022-06-25 10:42 GMT
Find Nodes?
    Voting Booth?
    My most frequent journeys are powered by:

    Results (81 votes). Check out past polls.