CGI::Application::Demo::Ajax - A search engine using CGI::Application,
AJAX and JSON
Either:
#!/usr/bin/perl
use CGI::Application::Demo::Ajax;
CGI::Application::Demo::Ajax -> new() -> run();
or:
#!/usr/bin/perl
use strict;
use warnings;
use CGI::Application::Dispatch;
use CGI::Fast;
use FCGI::ProcManager;
# ---------------------
my($proc_manager) = FCGI::ProcManager -> new({processes => 2});
$proc_manager -> pm_manage();
my($cgi);
while ($cgi = CGI::Fast -> new() )
{
$proc_manager -> pm_pre_dispatch();
CGI::Application::Dispatch -> dispatch
(
args_to_new => {QUERY => $cgi},
prefix => 'CGI::Application::Demo',
table =>
[
'' => {app => 'Ajax', rm => 'initialize'},
'/search' => {app => 'Ajax', rm => 'search'},
],
);
$proc_manager -> pm_post_dispatch();
}
CGI::Application::Demo::Ajax demonstrates how to use CGI::Application together with AJAX and JSON.
It ships with:
CGI instance scripts: ajax.cgi and ajaxajax.cgi is a trivial CGI script, while ajax is a fancy script using CGI::Application::Dispatch and FCGI::ProcManager.
This will be installed into the same directory as Ajax.pm. And that's where Ajax.pm looks for it.
By default, form_action is /cgi-bin/ajax.cgi, so you'll need to edit it to use form_action=/local/ajax.
Also, the default logging directory is /tmp, so this might call for another edit of .htajax.conf.
HTML::Template templates: *.tmplCGI::Application::Demo::AjaxThis module is available as a Unix-style distro (*.tgz).
See http://savage.net.au/Perl-modules/html/installing-a-module.html for help on unpacking and installing distros.
All these assume your doc root is /home/ron/httpd/prefork/htdocs.
Browse to http://developer.yahoo.com/yui/, download, and unzip into htdocs:
shell>cd /home/ron/httpd/prefork/htdocs
shell>unzip ~/Desktop/yui_2.7.0b.zip
This creates /home/ron/httpd/prefork/htdocs/yui, and yui_url in .htajax.conf must match.
Install this as you would for any Perl module:
Unpack the distro, and then either:
perl Build.PL
./Build
./Build test
sudo ./Build install
or:
perl Makefile.PL
make (or dmake)
make test
make install
HTML::Template files. shell>cd /home/ron/httpd/prefork/htdocs
shell>mkdir -p assets/templates/cgi/application/demo/ajax
shell>cp distro's/htdocs/*.tmpl assets/templates/cgi/application/demo/ajax
Alternately, edit the now installed .htajax.conf, to adjust tmpl_path.
shell>cd /home/ron/httpd/prefork
shell>cp distro's/htdocs/ajax.cgi cgi-bin
shell>chmod 755 cgi-bin/ajax.cgi
shell>cd /home/ron/httpd/prefork/htdocs
shell>mkdir local
shell>cp distro's/htdocs/ajax local
shell>chmod 755 local/ajax
Apache to use local/ajaxIf in fancy mode, add these to httpd.conf:
LoadModule fcgid_module modules/mod_fcgid.so
and:
<Location /local>
SetHandler fcgid-script
Options ExecCGI
Order deny,allow
Deny from all
Allow from 127.0.0.1
</Location>
And restart Apache.
Point your broswer at http://127.0.0.1/cgi-bin/ajax.cgi (trivial script), or http://127.0.0.1/local/ajax (fancy script, nice-and-clean URL).
Here's a step-by-step description of what's happening:
Point your web client at http://127.0.0.1/cgi-bin/ajax.cgi or http://127.0.0.1/local/ajax.
This is equivalent to CGI::Application::Demo::Ajax -> new() -> run().
Since there is no run mode input, the code defaults to Ajax.pm's sub initialize(). See sub setup() for details.
The work is done in Ajax.pm's sub initialize().
This page is sent from the server to the client.
It contains the contents of web.page.tmpl, with both search.js and search.tmpl embedded therein.
Of course, it also contains a minimal set of YUI Javascript files.
The default web page is displayed.
The CGI form in search.tmpl is set to not submit, but rather to call the Javascript function search_onsubmit(), which lives in search.js.
It's actually the copy of this code, now inside web.page.tmpl, now inside your client, which gets executed.
CGI form is submittedHere, Javascript does the submit, in such a way as to also specify a call-back (Javascript) function, search_callback(), which will handle the response from the server.
This function also lives in search.js.
This time a run mode was submitted, either as form data or as path info data.
And this means that when using the fancy script, you don't need the line in search.tmp referring to the hidden form variable 'rm', because of the path info '/search' in search_onsubmit().
The run mode causes Ajax.pm's sub search() to be the sub which gets executed this time.
It assembles the results, and uses JSON::XS to encode them.
The results of the search are sent to the client.
When the client receives the message, these events occur, in this order:
This object displays its data automatically. Actually, the object's constructor displays the data, which is why we call new by assigning the object to a Javascript variable, data_table.
It should be obvious that the code in Ajax.pm's sub search() can be extended in any manner, to pass more complex hash refs to the Javascript function search_callback().
This data can then be ignored by the Javascript, or you can extend the responseSchema and column_defs to display it.
Given this framework, extending these data structures is basically effortless.
CGI::Application::Demo::Ajax was written by Ron Savage
in 2009.
Home page: http://savage.net.au/index.html
Australian copyright © 2009, Ron Savage. All rights reserved.
All Programs of mine are 'OSI Certified Open Source Software';
you can redistribute them and/or modify them under the terms of
The Artistic License, a copy of which is available at:
http://www.opensource.org/licenses/index.html