#!/usr/bin/perl -w
use strict;
use CGI::Ajax;
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;
my $magic = " look ma, no submit!
";
# see if input is defined
if ( not defined $input ) {
return("input not defined or NaN" . $magic);
}
# see if value is a number (*thanks Randall!*)
if ( $input !~ /\A\d+\z/ ) {
return("input is NaN" . $magic);
}
# got a number, so mod by 2
$input % 2 == 0 ? return("$input is EVEN" . $magic) : return("$input is ODD" . $magic);
}; # 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 .= <
CGI::Ajax Example