![]() |
![]() Spring Semester 2003 |
19. Let's now move to the second problem, with one
(a suddenly simple program).
20. So far so good.#!/usr/bin/perl &printTop; print "Hello!"; &printBottom; sub printTop { print qq{Content-type: text/html\n\n<html> <head><title>My Pictures Script</title></head> <body bgcolor=white> }; } sub printBottom { print qq{</body></html>}; }
21. What do we add in two
to it?
22. This is#!/usr/bin/perl &printTop; $name = $ENV{"SCRIPT_NAME"}; $method = $ENV{"REQUEST_METHOD"}; print "Hello, I am $name, and I am called with method: $method. <p>"; &printBottom; sub printTop { print qq{Content-type: text/html\n\n<html> <head><title>My Pictures Script</title></head> <body bgcolor=white> }; } sub printBottom { print qq{</body></html>}; }
three
a bit reorganized for better focus.
23. Let's make a change (#!/usr/bin/perl &printTop; $name = $ENV{"SCRIPT_NAME"}; $method = $ENV{"REQUEST_METHOD"}; print "Hello, I am $name, and I am called with method: $method. <p>"; print qq{ <form method="$method" action="$name"> Argument: <input type="text" name="arg"> <p> Function: <select name="fun"> <option value="non"> Click Me! <option value="add"> Addition <option value="sub"> Subtraction </select> <p> <input type="submit" value="Proceed"> </form> }; &printBottom; sub printTop { print qq{Content-type: text/html\n\n<html> <head><title>My Pictures Script</title></head> <body bgcolor=white> }; } sub printBottom { print qq{</body></html>}; }
four
) and experiment.
24. Is your calculator working already?#!/usr/bin/perl &printTop; $name = $ENV{"SCRIPT_NAME"}; $method = $ENV{"REQUEST_METHOD"}; print "Hello, I am $name, and I am called with method: $method. <p>"; print "(", $ENV{"QUERY_STRING"}, ")<p>"; print qq{ <form method="$method" action="$name"> Argument: <input type="text" name="arg"> <p> Function: <select name="fun"> <option value="non"> Click Me! <option value="add"> Addition <option value="sub"> Subtraction </select> <p> <input type="submit" value="Proceed"> </form> }; &printBottom; sub printTop { print qq{Content-type: text/html\n\n<html> <head><title>My Pictures Script</title></head> <body bgcolor=white> }; } sub printBottom { print qq{</body></html>}; }
25. Are we getting any closer?
26. Perhaps we should take a break?
27. Nope, we should keep going.
28. Here's five
, taking the input apart.
29. But that's only part of it.#!/usr/bin/perl &printTop; $name = $ENV{"SCRIPT_NAME"}; $method = $ENV{"REQUEST_METHOD"}; print "Hello, I am $name, and I am called with method: $method. <p>"; $input = $ENV{"QUERY_STRING"}; print "($input)<p>"; @pairs = split(/&/, $input); foreach $pair (@pairs) { print "($pair)<p>"; } print qq{ <form method="$method" action="$name"> Argument: <input type="text" name="arg"> <p> Function: <select name="fun"> <option value="non"> Click Me! <option value="add"> Addition <option value="sub"> Subtraction </select> <p> <input type="submit" value="Proceed"> </form> }; &printBottom; sub printTop { print qq{Content-type: text/html\n\n<html> <head><title>My Pictures Script</title></head> <body bgcolor=white> }; } sub printBottom { print qq{</body></html>}; }
30. The next one (six
) does the rest of the work.
31. The next program (#!/usr/bin/perl &printTop; $name = $ENV{"SCRIPT_NAME"}; $method = $ENV{"REQUEST_METHOD"}; print "Hello, I am $name, and I am called with method: $method. <p>"; $input = $ENV{"QUERY_STRING"}; print "($input)<p>"; @pairs = split(/&/, $input); foreach $pair (@pairs) { print "($pair)<p>"; ($nam, $val) = split(/=/, $pair); print qq{ Name is ($nam) and value is ($val). <p> } } print qq{ <form method="$method" action="$name"> Argument: <input type="text" name="arg"> <p> Function: <select name="fun"> <option value="non"> Click Me! <option value="add"> Addition <option value="sub"> Subtraction </select> <p> <input type="submit" value="Proceed"> </form> }; &printBottom; sub printTop { print qq{Content-type: text/html\n\n<html> <head><title>My Pictures Script</title></head> <body bgcolor=white> }; } sub printBottom { print qq{</body></html>}; }
seven
) not only takes the input apart in
the same manner as six
does, it also carefully records the input in
a hash table, for future use, and gets prepared for its role as a calculator.
32. Are we getting any closer?#!/usr/bin/perl &printTop; $name = $ENV{"SCRIPT_NAME"}; $method = $ENV{"REQUEST_METHOD"}; print "Hello, I am $name, and I am called with method: $method. <p>"; $input = $ENV{"QUERY_STRING"}; print "($input)<p>"; @pairs = split(/&/, $input); foreach $pair (@pairs) { print "($pair)<p>"; ($nam, $val) = split(/=/, $pair); print qq{ Name is ($nam) and value is ($val). <p> }; $form{$nam} = $val; } foreach $key (keys %form) { print "($key) is associated with (", $form{$key}, ")<p>"; } if ($form{fun} eq "add") { } elsif ($form{fun} eq "sub") { } else { } print qq{ <form method="$method" action="$name"> Argument: <input type="text" name="arg"> <p> Function: <select name="fun"> <option value="non"> Click Me! <option value="add"> Addition <option value="sub"> Subtraction </select> <p> <input type="submit" value="Proceed"> </form> }; &printBottom; sub printTop { print qq{Content-type: text/html\n\n<html> <head><title>My Pictures Script</title></head> <body bgcolor=white> }; } sub printBottom { print qq{</body></html>}; }
33. The program above (#!/usr/bin/perl &printTop; $name = $ENV{"SCRIPT_NAME"}; $method = $ENV{"REQUEST_METHOD"}; print "Hello, I am $name, and I am called with method: $method. <p>"; $input = $ENV{"QUERY_STRING"}; print "($input)<p>"; @pairs = split(/&/, $input); foreach $pair (@pairs) { ($nam, $val) = split(/=/, $pair); $form{$nam} = $val; } if ($form{fun} eq "add") { $acc = $acc + $form{arg}; } elsif ($form{fun} eq "sub") { $acc = $acc - $form{arg}; } else { } print qq{ <form method="$method" action="$name"> Accumulator is: ($acc) <p> Argument: <input type="text" name="arg"> <p> Function: <select name="fun"> <option value="non"> Click Me! <option value="add"> Addition <option value="sub"> Subtraction </select> <p> <input type="submit" value="Proceed"> </form> }; &printBottom; sub printTop { print qq{Content-type: text/html\n\n<html> <head><title>My Pictures Script</title></head> <body bgcolor=white> }; } sub printBottom { print qq{</body></html>}; }
eight
) is trying. What's missing?
34. How's nine
handling this problem?
35. In#!/usr/bin/perl &printTop; $name = $ENV{"SCRIPT_NAME"}; $method = $ENV{"REQUEST_METHOD"}; print "Hello, I am $name, and I am called with method: $method. <p>"; $input = $ENV{"QUERY_STRING"}; print "($input)<p>"; @pairs = split(/&/, $input); foreach $pair (@pairs) { ($nam, $val) = split(/=/, $pair); $form{$nam} = $val; } $acc = $form{acc}; if ($form{fun} eq "add") { $acc = $acc + $form{arg}; } elsif ($form{fun} eq "sub") { $acc = $acc - $form{arg}; } else { } print qq{ <form method="$method" action="$name"> Accumulator is: ($acc) <p> Field named acc containg the accumulator value: <input type="text" value="$acc" name="acc"> <p> Argument: <input type="text" name="arg"> <p> Function: <select name="fun"> <option value="non"> Click Me! <option value="add"> Addition <option value="sub"> Subtraction </select> <p> <input type="submit" value="Proceed"> </form> }; &printBottom; sub printTop { print qq{Content-type: text/html\n\n<html> <head><title>My Pictures Script</title></head> <body bgcolor=white> }; } sub printBottom { print qq{</body></html>}; }
ten
we change one word only. Did you see which one?
36. Can you see which one, and what the difference is?#!/usr/bin/perl &printTop; $name = $ENV{"SCRIPT_NAME"}; $method = $ENV{"REQUEST_METHOD"}; print "Hello, I am $name, and I am called with method: $method. <p>"; $input = $ENV{"QUERY_STRING"}; print "($input)<p>"; @pairs = split(/&/, $input); foreach $pair (@pairs) { ($nam, $val) = split(/=/, $pair); $form{$nam} = $val; } $acc = $form{acc}; if ($form{fun} eq "add") { $acc = $acc + $form{arg}; } elsif ($form{fun} eq "sub") { $acc = $acc - $form{arg}; } else { } print qq{ <form method="$method" action="$name"> Accumulator is: ($acc) <p> Field named acc containg the accumulator value: <input type="hidden" value="$acc" name="acc"> <p> Argument: <input type="text" name="arg"> <p> Function: <select name="fun"> <option value="non"> Click Me! <option value="add"> Addition <option value="sub"> Subtraction </select> <p> <input type="submit" value="Proceed"> </form> }; &printBottom; sub printTop { print qq{Content-type: text/html\n\n<html> <head><title>My Pictures Script</title></head> <body bgcolor=white> }; } sub printBottom { print qq{</body></html>}; }
37. What is the difference between GET and POST?
The program below (eleven
) would be able to point it out to you.
38. Are we any closer now?#!/usr/bin/perl &printTop; $name = $ENV{"SCRIPT_NAME"}; $method = $ENV{"REQUEST_METHOD"}; print "Hello, I am $name, and I am called with method: $method. <p>"; if ($method eq "GET") { $input = $ENV{"QUERY_STRING"}; } else { # else what? read(STDIN, $input, $ENV{"CONTENT_LENGTH"}); } print "($input)<p>"; @pairs = split(/&/, $input); foreach $pair (@pairs) { ($nam, $val) = split(/=/, $pair); $form{$nam} = $val; } $acc = $form{acc}; if ($form{fun} eq "add") { $acc = $acc + $form{arg}; } elsif ($form{fun} eq "sub") { $acc = $acc - $form{arg}; } else { } print qq{ <form method="POST" action="$name"> Accumulator is: ($acc) <p> Field named acc containg the accumulator value: <input type="hidden" value="$acc" name="acc"> <p> Argument: <input type="text" name="arg"> <p> Function: <select name="fun"> <option value="non"> Click Me! <option value="add"> Addition <option value="sub"> Subtraction </select> <p> <input type="submit" value="Proceed"> </form> }; &printBottom; sub printTop { print qq{Content-type: text/html\n\n<html> <head><title>My Pictures Script</title></head> <body bgcolor=white> }; } sub printBottom { print qq{</body></html>}; }
39. Can you write the two programs?
40. I am sure you can.
41. If you have any questions please let me know!
42. But you know what: I want to make sure I have a question for you.
43. What, if you don't mind, is the
difference between eleven
and ten
?
(Take a look at their URLs when you work with them. Where does the input go?)
44. Let me know.