Search code examples
perlcgiplack

Perl module usable with different web-frameworks


Want make a perl-module what basically:

  • get some params from the web-request
  • do some internal logic
  • and produce an json string

My problem is: here is many different methods handling web request (decoding the query string), like (two notable ones are:)

  • CGI $query->{‘somearg’} or like
  • Plack (Plack::Request)
  • and probably another ones… (web frameworks and so on…)

How I should write the module, to be usable with different frontends?

Thinking about something like:

pakage MyJSONProducer::Plack; # for Plack based apps
use MyJSONProducer;           # My module
my $prod = MyJSONProducer->new(); 
my $args = decode_plack_request_args($env); #make a hashref from args...
my $json = $prod->run($args); # call with args…


package MyJSONProducer::CGI;   # for CGI based ones
use MyJSONProducer;
my $prod = MyJSONProducer->new(); 
my $args = decode_cgi_args($query); #make a hashref from args...
my $json = $prod->run($args); # call with args...

and so on…

But somewhat i feeling that this is not a very nice solution...

Is here some recommented way how to deal with this situation? Any CPAN module what i should study?


Solution

  • Just write MyJSONProducer::Plack. Then, if you want to deploy your app in a CGI environment, you can use something like Plack::Handler::CGI.

    This is the point of Plack and PSGI. If you write your code to the PSGI specification then you can deploy it anywhere.