Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | daniel-mar | 1 | #! /usr/bin/perl -w |
2 | |||
3 | # Perl version cloned from the original PHP by http://www.modernmethod.com/sajax/ |
||
4 | # I've left commented-out examples in the code for running with static methods. |
||
5 | # For moving to ModPerl it's important to no be redefining subs all the time, |
||
6 | # so this code adds the rs_register(funcname,coderef) call, which takes a coderef |
||
7 | # rather than the name of a sub to be called. |
||
8 | |||
9 | |||
10 | use Sajax; |
||
11 | use CGI; |
||
12 | |||
13 | my $q = new CGI; |
||
14 | |||
15 | my $rv = ""; |
||
16 | |||
17 | $rv .= "content-type: text/html\n\n"; |
||
18 | |||
19 | |||
20 | =pod |
||
21 | #for static method calls |
||
22 | sub Sajax::multiply { |
||
23 | my($x, $y)=@_; |
||
24 | return $x * $y; |
||
25 | } |
||
26 | sub Sajax::divide { |
||
27 | my($x, $y)=@_; |
||
28 | return $x / $y; |
||
29 | } |
||
30 | =cut |
||
31 | |||
32 | #equivalent modperl methods |
||
33 | my $msub = sub { |
||
34 | my($x, $y)=@_; |
||
35 | return $x * $y; |
||
36 | }; |
||
37 | my $dsub = sub { |
||
38 | my($x, $y)=@_; |
||
39 | return $x / $y; |
||
40 | }; |
||
41 | |||
42 | |||
43 | |||
44 | Sajax::rs_init(); |
||
45 | |||
46 | #register static methods (called by name) |
||
47 | #Sajax::rs_export("multiply","divide"); |
||
48 | |||
49 | #modperl methods (called as anonymous coderefs) |
||
50 | Sajax::rs_register("multiply",$msub); |
||
51 | Sajax::rs_register("divide",$dsub); |
||
52 | |||
53 | |||
54 | my $handled_value = Sajax::rs_handle_client_request($q); |
||
55 | |||
56 | if(defined $handled_value) { |
||
57 | $rv .= $handled_value; |
||
58 | } else { |
||
59 | |||
60 | $rv .= "<html>\n<head>\n<title>Multiplier</title>\n<script>\n\n"; |
||
61 | |||
62 | $rv .= Sajax::rs_show_javascript($q); |
||
63 | |||
64 | $rv .= <<EOT; |
||
65 | function do_multiply_cb(z) { |
||
66 | document.getElementById("z").value = z; |
||
67 | } |
||
68 | function do_divide_cb(z) { |
||
69 | document.getElementById("zz").value = z; |
||
70 | } |
||
71 | |||
72 | |||
73 | function do_multiply() { |
||
74 | var x, y; |
||
75 | x = document.getElementById("x").value; |
||
76 | y = document.getElementById("y").value; |
||
77 | x_multiply(x, y, do_multiply_cb); |
||
78 | } |
||
79 | |||
80 | function do_divide() { |
||
81 | var x, y; |
||
82 | x = document.getElementById("x").value; |
||
83 | y = document.getElementById("y").value; |
||
84 | x_divide(x, y, do_divide_cb); |
||
85 | } |
||
86 | EOT |
||
87 | |||
88 | |||
89 | $rv .= "</script>\n\n</head>\n"; |
||
90 | $rv .= <<EOT; |
||
91 | <body> |
||
92 | <input type="text" name="x" id="x" value="2" size="3"> |
||
93 | * |
||
94 | <input type="text" name="y" id="y" value="3" size="3"> |
||
95 | = |
||
96 | <input type="text" name="z" id="z" value="" size="5"> |
||
97 | <input type="text" name="zz" id="zz" value="" size="5"> |
||
98 | <input type="button" name="check" value="Multiply" onclick="do_multiply(); return false;"> |
||
99 | <input type="button" name="check" value="Divide" onclick="do_divide(); return false;"> |
||
100 | <input type="button" name="check" value="Both" onclick="do_multiply();do_divide(); return false;"> |
||
101 | |||
102 | <BR> |
||
103 | <A HREF=showsource.cgi>Show source</A> |
||
104 | </body> |
||
105 | </html> |
||
106 | EOT |
||
107 | } |
||
108 | |||
109 | |||
110 | print $rv; |
||
111 | |||
112 | |||
113 |