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

Convert Perl module to Web Service?

by tpederse (Sexton)
on Aug 21, 2010 at 13:01 UTC ( #856444=perlquestion: print w/replies, xml ) Need Help??
tpederse has asked for the wisdom of the Perl Monks concerning the following question:

Greetings all,

I have an existing Perl module that is written in object oriented Perl, and I'd like to make it available as a web service (or some other mechanism that allows for remote use).

My goal is to allow remote users to call the methods that are available in the module in more or less the same way they would use them if they had the module locally installed.

The module is already written and working pretty much as we'd like it to work, so I'm hoping to avoid major amounts of new coding, and I'd also like the "remote" users to be able to use the code in pretty much the same way as local users.

Any suggestions on how to proceed?

Thanks!
Ted

BTW, the following is an example of the code we can run locally now....we'd just like to somehow make it possible for a user to run this same kind of code on their own computer, and have our server do the work and send back the results.

use WordNet::QueryData; my $wn = WordNet::QueryData->new; defined $wn or die "Construction of WordNet::QueryData failed"; use WordNet::Similarity::DepthFinder; my $obj = WordNet::Similarity::DepthFinder->new ($wn); my ($err, $errString) = $obj->getError (); $err and die $errString; my $wps1 = 'car#n#4'; my $wps2 = 'oil#n#1'; my $offset1 = $wn -> offset ($wps1); my $offset2= $wn -> offset ($wps2); my @roots = $obj->getTaxonomies ($offset1, 'n'); my $taxonomy_depth = $obj->getTaxonomyDepth ($roots[0], 'n'); print "The maximum depth of the taxonomy where $wps1 is found is $tax +onomy_depth\n";

Replies are listed 'Best First'.
Re: Convert Perl module to Web Service?
by migmir (Initiate) on Aug 21, 2010 at 19:23 UTC
    Hi, Having spent the last year building SOAP web services with perl tools, I'd like to point a few useful things.
    You should be looking into Markov's excellent XML::Compile::WSDL11

    I've mainly used Catalyst in conjunction with Catalyst::Controller::SOAP which simplify most things.

    If you need to use your module as a catalyst model, use Catalyst::Model::Adaptor (or Catalyst::Model::Factory if you want a brand new object at each request )
    If "wsdl" rings no bell, start here I had quite a hard time dealing with all the info to assimilate at first, pm me if you want more pointers
      I'd second this. With Catalyst::Model::Adaptor or Catalyst::Model::Factory you can use Catalyst pretty much as glue between your existing model (the package you have written already) and the web framework, which provides the views and the controllers. The controllers should be tiny as they only call methods in your model based on input parameters. I use that approach too. Have a look at Catalyst::Action::REST as well.
Re: Convert Perl module to Web Service?
by rowdog (Curate) on Aug 21, 2010 at 18:43 UTC

    Well, the traditional approach would be CGI but you might want a higher level of abstraction like CGI::Application or Catalyst. If you're looking for something that will work without a web server, you might like HTTP::Engine.

    PS: If you're serious about running a "web service" you might want to search CPAN for SOAP or REST.

Re: Convert Perl module to Web Service?
by bluescreen (Friar) on Aug 21, 2010 at 19:34 UTC

    Take a look at SOAP::WSDL , JSON::RPC or CORBA ( but I can't recommend you any module as I haven't toy with it).

    My preference on any of these would depend on the type of application you're trying to build, whether it will be internal only or published to the internet

Re: Convert Perl module to Web Service?
by CountZero (Bishop) on Aug 21, 2010 at 18:55 UTC
    Did you search CPAN for SOAP or WDSL?

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: Convert Perl module to Web Service?
by ait (Friar) on Aug 23, 2010 at 18:16 UTC

    If you want something really simple to expose your module as a Web Service I would use HTTP::Server::Simple::CGI. Note that the vanilla module can only attend one requst at a time, but you also add Net::Server for forking.

    I would personally avoid the use of SOAP, XMLRPC and alike. IMHO these protocols add a lot of useless overhead and create all sorts of security issues. Since to properly apply security measures you have to inspect each package and understand it's contents, just to mention one of many flaws.

    To expose code correctly over HTTP, I would highly recommend to learn and use HTTP (Representational_State_Transfer) for what it was designed, and map the HTTP semantics to your api, avoiding the use of HTTP as a transport protocol, instead of tranfer.

    Basically this boils down to:

    • Think of your HTTP transactions as exchange of resources, and that each resource should have enough information for your client to figure out what to do with it (i.e. the resource should represent it's state). This can also be translated as: use HTTP to transfer data to and from the server, and the data should be enough for the client to make decisions on it. Never use HTTP to transport other protocols, for example, never put verbs on the URL, only nouns. And never use cookies and sessions (<grin> this will surely raise quite a polemic here).
    • Use HTTP methods for what they are:
      • GET is used to fetch a resource, it should be idempotent and never be used to modifiy stuff on the server.
      • POST is usually used to create new resources on the server. Your milage may vary.
      • PUT is usually used to update existing resources on the server. Your milage may vary.
      • DELETE is used to delete resources on the server.
      • HEAD, OPTIONS, etc. are used to obtaing information on your resource and other options.
    • Use the correct HTTP return codes for every transaction.
    • Use the correct HTTP headers for the types on content encodings, character sets, etc. etc.
    • Always think of ways to take advantage of caches. For example on data that is not likely to change you can use the appropriate HTTP Caching Headers and never have to go again to a database to fecth this data, even better, your client may use it's own cache and never even have to call you again to access the resource (it can access it off-line!).

    If you follow these simple recommendations your Web Services will be secure, simple, elegant, swift, and highly scallable.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://856444]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (11)
As of 2018-07-16 12:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (334 votes). Check out past polls.

    Notices?