Alex Tokarev > CGI-ExtDirect-2.02 > CGI::ExtDirect



Annotate this POD

View/Report Bugs
Module Version: 2.02   Source   Latest Release: CGI-ExtDirect-2.03


CGI::ExtDirect - Ext.Direct remoting interface for CGI applications


API definition

In api.cgi:

    use CGI::ExtDirect;
    use RPC::ExtDirect::API api_path     => '/extdirect_api',
                            router_path  => '/extdirect_router',
                            poll_path    => '/extdirect_events',
                            remoting_var => '',
                            polling_var  => '',
                            namespace    => 'myApp',  # Defaults to empty
                            auto_connect => 0,
                            no_polling   => 0,
                            debug        => 0,
                            before       => \&global_before_hook,
                            after        => \&global_after_hook,
    use My::ExtDirect::Published::Module::Foo;
    use My::ExtDirect::Published::Module::Bar;
    my $direct = CGI::ExtDirect->new();
    print $direct->api();      # Prints full HTTP response

Routing requests

In router.cgi:

    use CGI::ExtDirect;
    use My::ExtDirect::Published::Module::Foo;
    use My::ExtDirect::Published::Module::Bar;
    my $debug   = 1;  # Optional debugging flag
    my %headers = (   # Optional CGI headers
        -charset => 'iso-8859-1',
        -nph     => 1,
        -cookie  => $cookie,
    my $direct = CGI::ExtDirect->new( debug => $debug );
    print $direct->route(%headers);    # Prints full HTTP response

Providing Event polling service

In poll.cgi:

    use CGI;
    use CGI::ExtDirect;
    use My::ExtDirect::Event::Provider1;
    use My::ExtDirect::Event::Provider2;
    my $debug = 1; 
    my $cgi   = CGI->new;
    # do something with $cgi but do not print headers
    my $direct = CGI::ExtDirect->new( cgi => $cgi, debug => $debug );
    print $direct->poll();


This module provides RPC::ExtDirect gateway implementation for CGI compatible HTTP servers. It can be used wth Perl versions 5.6 and newer in about any environment; it was tested successfully with Apache, pure Perl server based on HTTP::Server::Simple and various other HTTP servers.

You can change default configuration options by passing corresponding parameters like shown above. For the meaning of parameters, see RPC::ExtDirect::API documentation.

Note that Ext.Direct specification requires server side implementation to return diagnostic messages only when debugging is explicitly turned on. This is why debug flag defaults to 'off' and CGI::ExtDirect returns generic error messages that do not contain any details as to where and what error has happened.



Creates a new CGI::ExtDirect object. $arguments is an optional hashref with the following options:


Instantiated CGI or similar object.


Debug flag, defaults to off. See the note above.


Creates JavaScript code with server side Action and Method declarations and prints it to default output handle along with HTTP headers. You can specify additional headers in CGI format: NPH, cookies, whatever; they will be passed to CGI->header() which is used to form HTTP header part.

Some of the headers, namely Content-Type, Content-Length and Status, are always overridden to provide client side with adequate response. Default Charset is UTF-8; however if you pass -charset header CGI::ExtDirect will honor it. It is implied that you should only do this when you clearly know what you are doing.

Other headers are passed along to CGI->header() unchanged.


Accepts Ext.Direct requests, dispatches them, collects results and prints them back as serialized stream.

%headers are treated the same way as in api(), see above.


Queries Event provider Methods registered with RPC::ExtDirect as pollHandlers for events, collects them and returns back serialized stream.

%headers are treated the same way as in api(), see above.


CGI::ExtDirect is dependent on the following modules: RPC::ExtDirect, JSON, Attribute::Handlers.


For explanation of RPC::ExtDirect attributes, see RPC::ExtDirect. For more detail on API options, see RPC::ExtDirect::API.

For more information on Ext.Direct API see specification: and documentation:!/api/

See included Ext JS examples for ideas on what Ext.Direct is and how to use it in CGI applications.


I would like to thank IntelliSurvey, Inc for sponsoring my work on version 2.0 of RPC::ExtDirect suite of modules.

The tiny but CGI-capable HTTP server used to provide working examples is (c) 2002-2004 by Hans Lub, <>. It is called p5httpd and can be found here:


Hooks functionality depend on RPC::ExtDirect 2.0 which is incompatible with Perls older than 5.12.

There are no known bugs in this module. Use github tracker to report bugs (better way) or just drop me an e-mail. Patches are welcome.


Alexander Tokarev <>


Copyright (c) 2011-2012 Alexander Tokarev.

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

Included Ext JS examples are copyright (c) 2011, Sencha Inc. Example code is used and distributed under GPL 3.0 license as provided by Sencha Inc. See Ext JS is available for download at

syntax highlighting: