view scripts/pjx_podex.pl @ 0:477258d09353 boosterpack

[svn r1] initial import
author robert
date Sun, 30 Aug 2009 02:19:26 -0400
parents
children
line wrap: on
line source
1 #!C:/strawberry/perl/bin/perl.exe
2 use strict;
3 use CGI::Ajax;
4 use CGI;
6 # define an anonymous perl subroutine that you want available to
7 # javascript on the generated web page.
9 my $evenodd_func = sub {
10 my $input = shift;
12 my $magic = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font size=-1>look ma, no submit!</font><br>";
14 # see if input is defined
15 if ( not defined $input ) {
16 return("input not defined or NaN" . $magic);
17 }
19 # see if value is a number (*thanks Randall!*)
20 if ( $input !~ /\A\d+\z/ ) {
21 return("input is NaN" . $magic);
22 }
24 # got a number, so mod by 2
25 $input % 2 == 0 ? return("$input is EVEN" . $magic) : return("$input is ODD" . $magic);
27 }; # don't forget the trailing ';', since this is an anon subroutine
29 # define a function to generate the web page - this can be done
30 # million different ways, and can also be defined as an anonymous sub.
31 # The only requirement is that the sub send back the html of the page.
32 sub Show_HTML {
33 my $html = "";
34 $html .= <<EOT;
36 <HTML>
37 <HEAD><title>CGI::Ajax Example</title>
38 </HEAD>
39 <BODY>
40 <form>
41 Enter a number:&nbsp;
42 <input type="text" name="val1" id="val1" size="6"
43 onkeyup="evenodd( ['val1'], ['resultdiv'] ); return true;"><br>
44 <hr>
45 <div id="resultdiv" style="border: 1px solid black;
46 width: 440px; height: 80px; overflow: auto">
47 </div>
48 </form>
49 </BODY>
50 </HTML>
51 EOT
53 return $html;
54 }
56 my $cgi = new CGI(); # create a new CGI object
57 # now we create a CGI::Ajax object, and associate our anon code
58 my $pjx = new CGI::Ajax( 'evenodd' => $evenodd_func );
60 # now print the page. This can be done easily using
61 # CGI::Ajax->build_html, sending in the CGI object to generate the html
62 # header. This could also be done manually, and then you don't need
63 # the build_html() method
64 print $pjx->build_html($cgi,\&Show_HTML); # this outputs the html for the page
66 # that's it!