Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Recommendation: CGI for a remote procedure call

by BernieC (Pilgrim)
on Apr 03, 2019 at 18:00 UTC ( [id://1232107]=perlquestion: print w/replies, xml ) Need Help??

BernieC has asked for the wisdom of the Perl Monks concerning the following question:

I used to use CGI.pm for everything, decades ago. I recently have a need to do some CGI hacking again and I'm surprised to see that it has become deprecated, and the "alternatives" page claims that the new/replacement packages are easier and better to use. I have a super-simple thing I'm trying to do and would appreciate recommendations on how to do it in the "modern" web world: I am going to build a remove procedure call CGI. That is, there'll be no web page to POST from and the result will go back in the most minimal HTML I can get away with. Basically, I'll be called via a canned

https://mysite/cgi-bin/rpc.pl?argstothecall

and it'll do nothing more than return the information desired. I don't even know what html functions are nor do I need anything fancy like templates. I need a feature-minimal module, not a fancy feature-rich one. Any help and advice appreciated. Thanks /bernie\

Replies are listed 'Best First'.
Re: Recommendation: CGI for a remote procedure call
by Your Mother (Archbishop) on Apr 03, 2019 at 21:11 UTC

    You're welcome to just use CGI (if your webserver supports it). It's deprecated because it's a poor foundation for a full application not because it's not useful and easy.

    use strictures; use CGI ":standard"; use HTML::Entities; # Called https://mysite/cgi-bin/rpc.pl?args=to;the=call my $explicitly_scalar_param = param("args"); my $the = param("the"); print header(), start_html("Title..."), h1("header"), div("Do something safely with", encode_entities($explicitly_scalar_param), "and", encode_entities($the)), end_html; # Test on command line: ./rpc.pl 'args=to;the=call'

    A *minimalist* modern take. If the thing is to grow, it should be in a framework like Mojolicious and friends. This uses PSGI via Plack::Request and the command line tool plackup. There are many deployment options for PSGI; from plackup through uWSGI.

    use strictures; use Plack::Request; # Called https://mysite/not-the-cgi-bin/rpc.pl?args=to;the=call sub { my $req = Plack::Request->new(+shift); my $args = $req->parameters->{args}; my $the = $req->parameters->{the}; [ 200, [ "Content-Type" => "text/html" ], [ "HTML tags + output. You HTML encode ALL unknown data." ] ]; }; # Command line: plackup rpc.pl # HTTP::Server::PSGI: Accepting connections at http://0:5000/ # -> port 5000 on localhost ^^^

    Update: the PSGI version would not go in the cgi-bin.

Re: Recommendation: CGI for a remote procedure call
by haukex (Archbishop) on Apr 04, 2019 at 07:48 UTC
Re: Recommendation: CGI for a remote procedure call
by talexb (Chancellor) on Apr 03, 2019 at 19:49 UTC

    I'd recommend Mojolicious::Lite .. as the name says, it's quite light-weight, and will do what you need.

    Alex / talexb / Toronto

    Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

Re: Recommendation: CGI for a remote procedure call
by bliako (Monsignor) on Apr 03, 2019 at 21:55 UTC

    So BernieC, do you want a CGI-based solution or prefer alternatives?

      I have no problem with alternatives! My only problem was that all of the aternatives in the "old" ALTERNATIVES were all too ambitious. I'll look at mojolicious::Lite and the others mentioned.. It has been more than a decade since I messed with CGI.pm and so I'd have to learn anything that I decided to use.

      I can't tell much about mojolicious::Lite because the docs seem to be focusing it on a subset of mojolicious, and I'm not really interested in learning a big "frameworkd" just so I can use a mini-package that only implements part of it.

      CGI::LITE and CGI::SIMPLE both look like just what I was looking for! The main difference I can see {for my purposes} is that ::SIMPLE includes the calls to set up simple HTML responses. I vaguely remember that I could/did do "my own" HTML responses and it was pretty simple {and mostly I didn't bother with CGI's html-construction stuff}, but I don't remember how any more so I might swing towards ::SIMPLE and let it handle the headers and such.

      Thanks!

        I'm not really interested in learning a big "frameworkd" just so I can use a mini-package that only implements part of it

        That's not really the case here... Mojolicious::Lite is part of the Mojolicious distribution, and instead of only implementing "parts" of the full framework, it just makes the full framework easier to use to write small applications. See Mojolicious::Guides::Tutorial.

        I don't remember how any more so I might swing towards ::SIMPLE and let it handle the headers and such.

        Note that the HTTP headers are quite different from HTML generation, and it's usually a good idea to use a module to handle the HTTP headers. As for HTML generation, you might be interested in HTML::Tiny, as mentioned in CGI::Alternatives, if you want to go that route instead of using a templating engine such as Template::Toolkit.

Re: Recommendation: CGI for a remote procedure call
by bliako (Monsignor) on Apr 09, 2019 at 08:39 UTC

    One more thing to consider here is whether your host will allow you to run your own web server. For my free-host there is no way I can run external commands. Not even use a compiler. Unless I run them via a CGI but then the webserver monitors it and eventually kills it. So, it would be useful, definetely, to hear which of the non-CGI-based alternatives can work in such environments or even if someone knows of a free-host who supports, say, Mojolicious::Lite. Personally, if I had a commercial project which paid well and had control over the host, I would surely try the alternatives (despite having invested effort on building a CGI-based backend with templates). The good news, CGI or alternatives, the main underlying engine is Perl:

    Mr Eddy: This is where mechanical excellence and one-thousand four-hundred horsepower pays off.

    bw, bliako

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1232107]
Approved by Corion
Front-paged by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-03-29 08:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found