For some reason XML-RPC has been taking up a lot of my thoughts lately. I've been thinking of a nice way to create an API to perl subroutines.

I wanted to create something where the the packages within the API itself would have to do little XML RPC related work themselves.

The main guts of the XML RPC would would come in this file

package XMLMethod; use Server; use RPC::XML::Procedure; sub import { my $child = shift; print "Child is $child\n"; return if $child eq 'XMLMethod'; my $export_procedures = *{"$child\::export_procedures"}{CODE}; die "$child does not implement export_procedures method\n" unless +$export_procedures; #run the code my $import_proc = &$export_procedures; my @procedures; while (my ($identifier, $details) = each(%{$import_proc}) ) { my $sub_ref = $details->[0]; my $sig = $details->[1]; my $proc = RPC::XML::Procedure->new({ 'name' => $id +entifier, 'code' => $su +b_ref, 'signature' => + [$sig] }); push(@procedures,$proc); } foreach (@procedures) { $Server::server->add_method($_); } } 1;
This XMLMethod package will look at every package which imports it and will look for the export_procedures sub. This sub essentially returns the code reference, signature and an identifier in a hash ref. I looked into automating this, by using no strict 'refs' and going through the package symbol table, but thought it might be problematic discerning what is actually a sub that is intended to be used in the XML RPC and what is not. So I decided it would be better to explicitly set the details from within each individual package. The following is an example of a package which would be in the API Directory.
package API::Math; use base XMLMethod; sub export_procedures { my %subs = ( 'math.sum' => [\&API::Math::sum, 'int int int'] ); return \%subs; } sub sum{ my $x = shift; my $y = shift; return $y + $x; } 1;

What I like about it the most, is that all you need to do is make sure your export_procedures sub is correct, and then use XMLMethod and then you will have your code available to respond to XML RPC requests without having to worry about anything else.

Does anybody feel there are any large oversights or omissions? Ideas for how it could be done in a 'nicer' way?

Ray

In reply to XML-RPC API layout by kalium

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.