Mercurial > rlmcintyre
comparison BoosterPack/scripts/.svn/text-base/pjx_podex.pl.svn-base @ 0:0d795f02a8bb tip
initial committ. what was I thinking?
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Mon, 27 Sep 2010 16:57:26 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:0d795f02a8bb |
---|---|
1 #!C:/strawberry/perl/bin/perl.exe | |
2 use strict; | |
3 use CGI::Ajax; | |
4 use CGI; | |
5 | |
6 # define an anonymous perl subroutine that you want available to | |
7 # javascript on the generated web page. | |
8 | |
9 my $evenodd_func = sub { | |
10 my $input = shift; | |
11 | |
12 my $magic = " <font size=-1>look ma, no submit!</font><br>"; | |
13 | |
14 # see if input is defined | |
15 if ( not defined $input ) { | |
16 return("input not defined or NaN" . $magic); | |
17 } | |
18 | |
19 # see if value is a number (*thanks Randall!*) | |
20 if ( $input !~ /\A\d+\z/ ) { | |
21 return("input is NaN" . $magic); | |
22 } | |
23 | |
24 # got a number, so mod by 2 | |
25 $input % 2 == 0 ? return("$input is EVEN" . $magic) : return("$input is ODD" . $magic); | |
26 | |
27 }; # don't forget the trailing ';', since this is an anon subroutine | |
28 | |
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; | |
35 | |
36 <HTML> | |
37 <HEAD><title>CGI::Ajax Example</title> | |
38 </HEAD> | |
39 <BODY> | |
40 <form> | |
41 Enter a number: | |
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 | |
52 | |
53 return $html; | |
54 } | |
55 | |
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 ); | |
59 | |
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 | |
65 | |
66 # that's it! | |
67 |