JavaScript::RPC::Server::CGI - Remote procedure calls from JavaScript


        package MyJSRPC;
        use base qw( JavaScript::RPC::Server::CGI );
        sub add {
                my $self = shift;
                unless( @_ == 2 and $_[ 0 ] =~ /^\d+$/ and $_[ 1 ] =~ /^\d+$/ ) {
                        return $self->error( 'inputs must be digits only' )
                return $self->result( $_[ 0 ] + $_[ 1 ] );
        sub subtract {
                my $self = shift;
                unless( @_ == 2 and $_[ 0 ] =~ /^\d+$/ and $_[ 1 ] =~ /^\d+$/ ) {
                        return $self->error( 'inputs must be digits only' )
                return $self->result( $_[ 0 ] - $_[ 1 ] );
        package main;
        use strict;
        my $server = MyJSRPC->new;


JavaScript::RPC::Server::CGI is a CGI-based server library for use with Brent Ashley's JavaScript Remote Scripting (JSRS) client library. It works asynchronously and uses DHTML to deal with the payload.

In order to add your custom meothds, this module should be subclassed.

The most current version (as of the release of this module) of the client library as well as a demo application have been included in this distribution.



Creates a new instance of the module. No further options are available at this time.


Gets / sets the query object.


This method generates a new query object. It is used internally by the query() method. This method should only be used if you want to supply a query object other than the standard object. However, it must be a compatible object. Here's an example using CGI::Simple.

        sub get_new_query {
                require CGI::Simple;
                my $q = CGI::Simple->new();

                return $q;


Gets / sets a hash of information related to the currently query. The data is empty until after process() has been executed. The resulting structure contains four items:

  • method - the method called

  • params - an array of parameters for the method

  • uid - the unique id for this query

  • context - the context id


Get / sets the error message sent to the client if an error occurred.


Processes the current query and either returns the result from the appropriate method, or an error to the client and returns either true or false, respectively, to the caller. An error will occur if the method name is blank, or the method has not been defined. This function takes an optional compatible object as an input.

Your subclass' methods MUST finish off with one of the following:

        # for an error...
        return $self->error( $message );

        # for a successful call...
        return $self->result( $result );


Returns a valid error payload to the client and false to the caller. It will automatically call error_message() for you.


Returns a valid result payload to the client and true to the caller.




  • Brian Cassidy <>


Copyright 2004 by Brian Cassidy

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.