in reply to Need to know the process to implement perl script into web application server.
#!/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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Need to know the process to implement perl script into web application server.
by perlfan (Parson) on Mar 29, 2021 at 19:50 UTC | |
|
Re^2: Need to know the process to implement perl script into web application server.
by chandantul (Scribe) on Apr 22, 2021 at 04:39 UTC | |
by choroba (Cardinal) on Apr 22, 2021 at 06:06 UTC | |
by chandantul (Scribe) on Apr 22, 2021 at 14:55 UTC | |
by choroba (Cardinal) on Apr 22, 2021 at 15:34 UTC | |
by chandantul (Scribe) on Apr 22, 2021 at 17:24 UTC | |
| |
| A reply falls below the community's threshold of quality. You may see it by logging in. |