Let's say your script looks like this:
#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; use Config::Tiny; my $config = 'Config::Tiny'->read('config.properties'); my $operand1 = $config->{operation}{op1}; my $operand2 = $config->{operation}{op2}; my $operation = $config->{operation}{op}; my $is_integer = $config->{operation}{int}; my %dispatch = ('+' => sub { $_[0] + $_[1] }, '-' => sub { $_[0] - $_[1] }, '/' => sub { $_[0] / $_[1] }, '*' => sub { $_[0] * $_[1] }); my $action = $dispatch{$operation} or die "Unknown operation '$operati +on'.\n"; my $result = $action->($operand1, $operand2); $result = int $result if $is_integer; say $result;

A possible config file might look like

[operation] op = / op1 = 10 op2 = 3 int = 1

We will use the Dancer2 framework to help us with the work. It's not the only option, but we have to pick one, so why not this one. Install Dancer2 and App::Cmd from CPAN and run

dancer2 gen -a webapp

This creates a skeleton of the web application. For our simple script, we'll only need to handle two methods: get (which will display a form) and post (which will display the result). Each of them will have a different page template, so go to views/ and create query.tt:

<form id="f1" method="post" action="/"> <div> <label for="op1">Op1</label> <input id="op1" name="op1"> <label for="op2">Op2</label> <input id="op2" name="op2"> </div> <div> <input type="radio" name="op" value="plus" id="plus"> <label for="plus">Plus</label> </div> <div> <input type="radio" name="op" value="minus" id="minus"> <label for="minus">Minus</label> </div> <div> <input type="radio" name="op" value="times" id="times"> <label for="times">Times</label> </div> <div> <input type="radio" name="op" value="divide" id="divide"> <label for="divide">Divide</label> </div> <div> <input type="checkbox" name="int" value="int" id="int"> <label for="int">Integer</label> </div> <button>Sumbit</button> </form>

and result.tt:

<p> Result: <% result %> </p>

You see, all the parameters that were origianlly coming from the config will now come from the web form. Create the Op.pm in lib/ from the script:

package Op; use warnings; use strict; my %dispatch = (plus => sub { $_[0] + $_[1] }, minus => sub { $_[0] - $_[1] }, divide => sub { $_[0] / $_[1] }, times => sub { $_[0] * $_[1] }); sub result { my ($args) = @_; my $operand1 = $args->{op1}; my $operand2 = $args->{op2}; my $operation = $args->{op}; my $is_integer = $args->{int}; my $action = $dispatch{$operation} or die "Unknown operation '$ope +ration'.\n"; my $result = $action->($operand1, $operand2); $result = int $result if $is_integer; return $result } __PACKAGE__

Now just wire the routing in lib/webapp.pm:

package webapp; use Dancer2; use Op; our $VERSION = '0.1'; get '/' => sub { template 'query' => { title => 'webapp' }; }; post '/' => sub { my $r = Op::result({op1 => param('op1'), op2 => param('op2'), int => param('int'), op => param('op')}); template result => { title => 'webapp', result => $r } }; true;

Now, your application is ready to run. Launch

plackup bin/app.psgi

and open http://localhost:5000 in your browser.

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

In reply to Re: Need to know the process to implement perl script into web application server. by choroba
in thread Need to know the process to implement perl script into web application server. by chandantul

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.