An example JSON endpoint in Perl


We will be using a module from CPAN for this. A bit tricky to find, but quite simple once you've got it. Code mostly taken from the CPAN documents, but with a few holes filled in.

Here is

# Daemon version
use JSON::RPC::Server::Daemon;

# see documentation at:

my $server = JSON::RPC::Server::Daemon->new(LocalPort => 8080);
$server -> dispatch({'/test' => 'myApp'});
$server -> handle();




use JSON::RPC::Client;

my $client = new JSON::RPC::Client;

my $uri = 'http://localhost:8080/test';
my $obj = {
method => 'sum', # or 'MyApp.sum'
params => [10, 20],

my $res = $client->call( $uri, $obj );

if ($res->is_error) {
print "Error : ", $res->error_message;
} else {
print $res->result;
} else {
print $client->status_line;

# or

#$client->prepare($uri, ['sum', 'echo']);
#print $client->sum(10, 23);

And which is called by the server:

package myApp;

#optionally, you can also
use base qw(JSON::RPC::Procedure);  # for :Public and :Private attributes

sub sum : Public(a:num, b:num) {
my ($s, $obj) = @_;
return $obj->{a} + $obj->{b};


ReadLine in Perl on Ubuntu


Update July 2012: A more robust solution, from within the cpan program, is: install Bundle::CPAN

A common Perl module, ReadLine is especially handy when running the cpan program to install other modules, as it lets you up-arrow through previous commands, edit your command with the arrows and so on.

At least on Ubuntu 10.10 ... CPAN will not install it. You get this error:

cpan> <span style="text-decoration: underline;">install Term::ReadLine</span>
Running install for module 'Term::ReadLine'
CPAN: Data::Dumper loaded ok (v2.124)
'YAML' not installed, falling back to Data::Dumper and Storable to read prefs '/home/luser/.cpan/prefs'
The most recent version "1.05" of the module "Term::ReadLine"
is part of the perl-5.12.2 distribution. To install that, you need to run
 force install Term::ReadLine   --or--
 install J/JE/JESSE/perl-5.12.2.tar.gz
Running make test
 Can't test without successful make
Running make install
 Make had returned bad status, install seems impossible
Failed during this command:
 JESSE/perl-5.12.2.tar.gz                     : make NO isa perl

(typed text is underlined.)

The proper Ubuntu way to get ReadLine installed is to:

$ <span style="text-decoration: underline;">sudo apt-get install libterm-readline-gnu-perl </span>

You probably also want to install Term::ReadLine::Perl (and) Term::ReadKey

Alternately, use the single-command-line cpanm to install one or more Perl modules and all dependencies, like this:

$ <u>cpanm Term::ReadLine Term::ReadLine::Perl Term::ReadKey</u>

You might also check Racker Hacker's article on auotmatically installing CPAN dependencies without requiring confirmation. Note, this will not prevent modules like those in the Test suite from asking:

<em>{this module} </em>is just needed temporarily during building or testing.
Do you want to install it permanently? [yes]

Strawberry Perl for Windows, including 7 64-bit


Strawberry Perl is a relatively new competitor to ActiveState Perl.

Strawberry includes MinGW (a port of the GNU compiler tools) and MSYS (a port of the bash command line) which run directly as Windows programs -- in contrast to Cygwin which is a separate environment. Yes that means you have 'make' and 'gcc' on Windows directly now!

Strawberry runs under 64-bit Windows 7; ActiveState doesn't yet. Strawberry installs even the latest modules from CPAN; ActiveState has precompiled 'ppm' modules which do not include the full CPAN complement nor the latest additions or updates.

CPAN won't install Perl GD module on Ubuntu 9.04


The 'cpan' command won't "install GD" as it complains that the GD library is not found. Although GD is installed in Ubuntu by default, you need the developer library. What you want instead is:

$ sudo apt-get install libgd-gd2-perl
$ perl
use GD;