#!/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 '$operation'.\n"; my $result = $action->($operand1, $operand2); $result = int $result if $is_integer; say $result; #### [operation] op = / op1 = 10 op2 = 3 int = 1 #### dancer2 gen -a webapp ####
##
##

Result: <% result %>

##
## 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 '$operation'.\n"; my $result = $action->($operand1, $operand2); $result = int $result if $is_integer; return $result } __PACKAGE__ #### 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; #### plackup bin/app.psgi