Question

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?

Was it helpful?

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.

OTHER TIPS

There isn't much more you can do. You can make it more user-friendly, though.

Say your module is called Kobame. Then you'll create that plus several othermodules named:

  • Kobame:CGI
  • Kobame:Plack
  • ...

Each of these will handle the specifics of getting started and will then call out to your core Kobame module. The Kobame module will have to take generic perl datastructures and should not reference anything of the web modules.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top