I have written a server module for using JSON::RPC::Server and am trying to import more than one package (using use
). This is the code for the Server.pl
file:
#!/usr/bin/perl
use JSON::RPC::Server::Daemon;
use add2Num;
JSON::RPC::Server::Daemon->new(LocalPort => 42337)
->dispatch({'/jsonrpc/API' => 'add2Num'})
->handle();
And this works fine. However, I want to import (use
) another file for, let's say, subtraction. I have tried rewriting the server module in 2 ways like so:
#!/usr/bin/perl
use JSON::RPC::Server::Daemon;
use add2Num;
use sub2Num;
JSON::RPC::Server::Daemon->new(LocalPort => 42337)
->dispatch({'/jsonrpc/API' => 'add2Num'})
->dispatch({'/jsonrpc/API' => 'sub2Num'})
->handle();
and
#!/usr/bin/perl
use JSON::RPC::Server::Daemon;
use add2Num;
use sub2Num;
JSON::RPC::Server::Daemon->new(LocalPort => 42337)
->dispatch({'/jsonrpc/API' => 'add2Num', 'sub2Num'})
->handle();
Both of these give me a "Procedure Error" when I attempt to access the subtract function which is written in sub2Num
. I followed this link for the syntax of dispatch
. Could someone please tell me my error?
After looking at the code for the module (well, almost), I think your assumption is right. You cannot have two modules for the same path.
elsif (ref $arg[0] eq 'HASH') { # Lazy loading
for my $path (keys %{$arg[0]}) {
my $pkg = $arg[0]->{$path};
$self->{dispatch_path}->{$path} = $pkg;
}
}
It will accept multiple key/value pairs of paths and modules in dispatch, so there is no need to call dispatch twice. But it will only allow one module per path. Your second call to dispatch
with sub2Num
was overwriting the previously set add2Num
.
Without trying anything of this, I see a few solutions:
dispatch(['add2Num', 'sub2Num')
and figure out where to set the path that the daemon is bound to (most likely)add2Num
and sub2Num
to combine them and dispatch({'/jsonrpc/API' => 'combinedNum})