rlm@0: #!C:/strawberry/perl/bin/perl.exe rlm@0: use strict; rlm@0: use CGI::Ajax; rlm@0: use CGI; rlm@0: rlm@0: # define an anonymous perl subroutine that you want available to rlm@0: # javascript on the generated web page. rlm@0: rlm@0: my $evenodd_func = sub { rlm@0: my $input = shift; rlm@0: rlm@0: my $magic = "      look ma, no submit!
"; rlm@0: rlm@0: # see if input is defined rlm@0: if ( not defined $input ) { rlm@0: return("input not defined or NaN" . $magic); rlm@0: } rlm@0: rlm@0: # see if value is a number (*thanks Randall!*) rlm@0: if ( $input !~ /\A\d+\z/ ) { rlm@0: return("input is NaN" . $magic); rlm@0: } rlm@0: rlm@0: # got a number, so mod by 2 rlm@0: $input % 2 == 0 ? return("$input is EVEN" . $magic) : return("$input is ODD" . $magic); rlm@0: rlm@0: }; # don't forget the trailing ';', since this is an anon subroutine rlm@0: rlm@0: # define a function to generate the web page - this can be done rlm@0: # million different ways, and can also be defined as an anonymous sub. rlm@0: # The only requirement is that the sub send back the html of the page. rlm@0: sub Show_HTML { rlm@0: my $html = ""; rlm@0: $html .= < rlm@0: CGI::Ajax Example rlm@0: rlm@0: rlm@0:
rlm@0: Enter a number:  rlm@0:
rlm@0:
rlm@0:
rlm@0:
rlm@0:
rlm@0: rlm@0: rlm@0: EOT rlm@0: rlm@0: return $html; rlm@0: } rlm@0: rlm@0: my $cgi = new CGI(); # create a new CGI object rlm@0: # now we create a CGI::Ajax object, and associate our anon code rlm@0: my $pjx = new CGI::Ajax( 'evenodd' => $evenodd_func ); rlm@0: rlm@0: # now print the page. This can be done easily using rlm@0: # CGI::Ajax->build_html, sending in the CGI object to generate the html rlm@0: # header. This could also be done manually, and then you don't need rlm@0: # the build_html() method rlm@0: print $pjx->build_html($cgi,\&Show_HTML); # this outputs the html for the page rlm@0: rlm@0: # that's it! rlm@0: