It's really not that difficult...
Server
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
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.