The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

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 is an object-oriented module that provides a unique mechanism for using perl code asynchronously from javascript-enhanced web pages. You would 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 package 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 CGI::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 = <<EOT;

<HTML>
<HEAD><title>Perljax Example</title>
</HEAD>
<BODY>
  Enter a number:&nbsp;
  <input type="text" name="val1" id="val1" size="6"
     onkeyup="evenodd( ['val1'], 'resultdiv' );
     return 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 CGI::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

# this outputs the html for the page
print $pjx->build_html($cgi,\&Show_Form);

# that's it!

METHODS

build_html()
    Purpose: associate cgi obj ($cgi) 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()

BUGS

SUPPORT

Check out the sourceforge discussion lists at:

http://www.sourceforge.net/projects/pjax

AUTHORS

Brian C. Thomas     Brent Pedersen
CPAN ID: BCT
bct.x42@gmail.com   bpederse@gmail.com

COPYRIGHT

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

The full text of the license can be found in the LICENSE file included with this module.

SEE ALSO

Class::Accessor, CGI

2 POD Errors

The following errors were encountered while parsing the POD:

Around line 127:

'=item' outside of any '=over'

Around line 157:

You forgot a '=back' before '=head1'