NAME
CGI::Perljax - a perl-specific system for writing AJAX- or DHTML-based web applications.
SYNOPSIS
use CGI::Perljax;
my $pjx = new CGI::Perljax( 'exported_func1' => \&perl_func1 );
DESCRIPTION
Perljax s an object-oriented module that provides a unique mechanism for using perl code asynchronously from javascript-enhanced web pages. You woul commonly use Perljax in AJAX/DHTML-based web applications. Perljax unburdens the user from having to write any javascript, except for having to associate an exported method with a document-defined event (such as onClick, onKeyUp, etc). Only in the more advanced implementations of a exported perl method would a user need to write any javascript.
Perljax supports methods that return single results, or multiple results to the web page.
Using Perljax, the URL for the HTTP GET request is automatically generated based on HTML layout and events, and the page is then dynamically updated.
Other than using the Class::Accessor module to generate Perljaxs' accessor methods, Perljax is completely self-contained - it does not require you to install a larger packacge or a full Content Management System.
USAGE
First, you create a cgi script: the only requirements for Perljax are that you hand it a CGI.pm object, and that the subroutines to be exported to javascript are declared prior to creating the Perljax object, like so:
# start us out with the usual suspects use strict; use Perljax; use CGI;
# define an anonymous perl subroutine that you want available to # javascript on the generated web page.
my $evenodd_func = sub { my $input = shift;
# see if input is defined
if ( not defined $input ) {
return("input not defined or NaN");
}
# see if value is a number (*thanks Randall!*)
if ( $input !~ /\A\d+\z/ ) {
return("input is NaN");
}
# got a number, so mod by 2
$input % 2 == 0 ? return("EVEN") : return("ODD");
}; # don't forget the trailing ';', since this is an anon subroutine
# define a function to generate the web page - this can be done # million different ways, and can also be defined as an anonymous sub. # The only requirement is that the sub send back the html of the page. sub Show_HTML { my $html = ""; $html .= <<EOT;
<HTML> <HEAD><title>Perljax Example</title> </HEAD> <BODY> Enter a number: <input type="text" name="val1" id="val1" size="6" onkeyup="evenodd( ['val1'], 'resultdiv' ); retu rn true;"><br> <hr> <div id="resultdiv" style="border: 1px solid black; width: 440px; height: 80px; overflow: auto"> </div> </BODY> </HTML> EOT
return $html;
}
my $cgi = new CGI(); # create a new CGI object # now we create a Perljax object, and associate our anon code my $pjx = new Perljax( 'evenodd' => $evenodd_func );
# now print the page. This can be done easily using # Perljax->build_html, sending in the CGI object to generate the html # header. This could also be done manually, and then you don't need # the build_html() method print $pjx->build_html($q,\&Show_Form); # this outputs the html for the page
# that's it!
INTERFACE
new()
build_html()
show_javascript()
BUGS
SUPPORT
AUTHORS
Brian C. Thomas Brent Pedersen
CPAN ID: BCT
bct.x42@gmail.com bpederse@gmail.com
COPYRIGHT
This program is free software licensed under the...
The General Public License (GPL)
Version 2, June 1991
The full text of the license can be found in the LICENSE file included with this module.
SEE ALSO
Class::Accessor, CGI
- build_html()
-
Purpose: associate cgi obj ($q) with pjx object, insert javascript into <HEAD></HEAD> element Arguments: either a coderef, or a string containing html Returns: html or updated html (including the header) Called By: originating cgi script
- show_javascript()
-
Purpose: builds the text of all the javascript that needs to be inserted into the calling scripts html header Arguments: Returns: javascript text Called By: originating web script
- show_common_js()
-
Purpose: create text of the javascript needed to interface with the perl functions Arguments: none Returns: text of common javascript subroutine, 'do_http_request' Called By: originating cgi script, or build_html()
- insert_js_in_head()
-
Purpose: searches the html value in the Perljax object and inserts the ajax javascript code in the <script></script> section, or if no such section exists, then it creates it. Arguments: none Returns: none Called By: build_html()
- handle_request()
-
Purpose: makes sure a fname function name was set in the CGI object, and then tries to eval the function with parameters sent in on fnargs Arguments: none Returns: the result of the perl subroutine, as text; if multiple arguments are sent back from the defined, exported perl method, then join then with a connector (__pjx__). Called By: build_html()
- make_function()
-
Purpose: creates the javascript wrapper for the underlying perl subroutine Arguments: CGI object from web form, and the name of the perl function to export to javascript Returns: text of the javascript-wrapped perl subroutine Called By: show_javascript; called once for each registered perl subroutine
- Subroutine: register()
-
Purpose: adds a function name and a code ref to the global coderef hash Arguments: function name, code reference Returns: none Called By: originating web script
1 POD Error
The following errors were encountered while parsing the POD:
- Around line 170:
'=item' outside of any '=over'
=over without closing =back