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