Mercurial > boosterpack
diff 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 diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/scripts/pjx_podex.pl Sun Aug 30 02:19:26 2009 -0400 1.3 @@ -0,0 +1,67 @@ 1.4 +#!C:/strawberry/perl/bin/perl.exe 1.5 +use strict; 1.6 +use CGI::Ajax; 1.7 +use CGI; 1.8 + 1.9 +# define an anonymous perl subroutine that you want available to 1.10 +# javascript on the generated web page. 1.11 + 1.12 +my $evenodd_func = sub { 1.13 + my $input = shift; 1.14 + 1.15 + my $magic = " <font size=-1>look ma, no submit!</font><br>"; 1.16 + 1.17 + # see if input is defined 1.18 + if ( not defined $input ) { 1.19 + return("input not defined or NaN" . $magic); 1.20 + } 1.21 + 1.22 + # see if value is a number (*thanks Randall!*) 1.23 + if ( $input !~ /\A\d+\z/ ) { 1.24 + return("input is NaN" . $magic); 1.25 + } 1.26 + 1.27 + # got a number, so mod by 2 1.28 + $input % 2 == 0 ? return("$input is EVEN" . $magic) : return("$input is ODD" . $magic); 1.29 + 1.30 +}; # don't forget the trailing ';', since this is an anon subroutine 1.31 + 1.32 +# define a function to generate the web page - this can be done 1.33 +# million different ways, and can also be defined as an anonymous sub. 1.34 +# The only requirement is that the sub send back the html of the page. 1.35 +sub Show_HTML { 1.36 + my $html = ""; 1.37 + $html .= <<EOT; 1.38 + 1.39 +<HTML> 1.40 +<HEAD><title>CGI::Ajax Example</title> 1.41 +</HEAD> 1.42 +<BODY> 1.43 +<form> 1.44 + Enter a number: 1.45 + <input type="text" name="val1" id="val1" size="6" 1.46 + onkeyup="evenodd( ['val1'], ['resultdiv'] ); return true;"><br> 1.47 + <hr> 1.48 + <div id="resultdiv" style="border: 1px solid black; 1.49 + width: 440px; height: 80px; overflow: auto"> 1.50 + </div> 1.51 +</form> 1.52 +</BODY> 1.53 +</HTML> 1.54 +EOT 1.55 + 1.56 + return $html; 1.57 +} 1.58 + 1.59 +my $cgi = new CGI(); # create a new CGI object 1.60 +# now we create a CGI::Ajax object, and associate our anon code 1.61 +my $pjx = new CGI::Ajax( 'evenodd' => $evenodd_func ); 1.62 + 1.63 +# now print the page. This can be done easily using 1.64 +# CGI::Ajax->build_html, sending in the CGI object to generate the html 1.65 +# header. This could also be done manually, and then you don't need 1.66 +# the build_html() method 1.67 +print $pjx->build_html($cgi,\&Show_HTML); # this outputs the html for the page 1.68 + 1.69 +# that's it! 1.70 +