use v5.14;
use CGI ();
use JSON::PP ();
my $q = 'CGI'->new;
my $json = 'JSON::PP'->new;
my $input = $json->decode( $q->param('POSTDATA') );
my $a = $input->{a};
my $b = $input->{b};
my $c = $a + $b;
print $q->header("application/json");
print $json->encode({ c => $c });
Client
use v5.14;
use HTTP::Tiny ();
use JSON::PP ();
my $json = 'JSON::PP'->new;
my $http = 'HTTP::Tiny'->new;
my $url = 'http://buzzword.org.uk/2013/json-addition.cgi';
my $input = { a => 40, b => 2 };
my @request = ($url => { content => $json->encode($input) });
my $response = $http->post(@request);
my $output = $json->decode($response->{content});
say $output->{c};
(All modules used above are part of Perl core as of 5.14+, but CGI is scheduled to leave core in a future version of Perl.)
package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
|