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]
|