![]() |
![]() Fall Semester 2002 |
1. This is a script, called eno
:
Try it, see how you can enter some data into it. (The program is all geared up for work with a user).#!/usr/bin/perl print qq{Content-type: text/html\n\n<html><head> <title>Hello script</title></head><body bgcolor=white>}; $date = localtime; print "<h1> Hello $date </h1>"; $valueOne = $ENV{"QUERY_STRING"}; $valueTwo = $ENV{"SCRIPT_NAME"}; print qq{ env of query string is: $valueOne <p> env of script name is: $valueTwo <p> <a href="$valueTwo?something">Click Here</a> }; print qq{</body></html>};
2. This a script called owt
:
For#!/usr/bin/perl print qq{Content-type: text/html\n\n<html><head> <title>Hello script</title></head><body bgcolor=white>}; $date = localtime; print "<h1> Hello $date </h1>"; $valueOne = $ENV{"QUERY_STRING"}; $valueTwo = $ENV{"SCRIPT_NAME"}; print qq{ env of query string is: $valueOne <p> env of script name is: $valueTwo <p> <a href="$valueTwo?something">Click Here</a> }; print qq{</body></html>};
owt
the question is: do you see any differences, as compared with the previous program?
3. Can two programs (such as eno
and owt
above)
How could that be? (So the contents is not all that matters!)
4. Ignoring the time stamp aspect, what makes the two identical programs produce different output?
5. The program below (called eerht
) will now get us started.
6. Can you explain what it does, and how it works?#!/usr/bin/perl %images = ( "One" => "http://www.cs.indiana.edu/dept/img/lh01.gif", "Seven" => "http://www.cs.indiana.edu/dept/img/lh07.gif", "Eight" => "http://www.cs.indiana.edu/dept/img/lh08.gif", "Nine" => "http://www.cs.indiana.edu/dept/img/lh09.gif" ); &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>}; }
7. Let's change eerht
into ruof
below:
8. What does it do? How does it work?#!/usr/bin/perl %images = ( "One" => "http://www.cs.indiana.edu/dept/img/lh01.gif", "Seven" => "http://www.cs.indiana.edu/dept/img/lh07.gif", "Eight" => "http://www.cs.indiana.edu/dept/img/lh08.gif", "Nine" => "http://www.cs.indiana.edu/dept/img/lh09.gif" ); &printTop; print "Let me show you my images: <p>"; @lst = (keys %images); $input = $ENV{"QUERY_STRING"}; print "Your input is: $input <p> "; foreach $i (@lst) { $name = $i; $pic = $images{$i}; print qq{$name: <img src="$pic"><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>}; }
9. Are you comfortable with foreach
?
10. Can you express it in terms of a for
construct?
11. If the program above was called#!/usr/bin/perl %h = ( "one" => "Larry", "two" => "Michael", "three" => "Tony" ); foreach $k (keys %h){ # do something with $k # and with $h {$k} print $k, " ", $h{$k}, "\n"; } print "----( again, again! )----\n"; @a = keys %h; for ($i = 0; $i <= $#a; $i++) { # do something with $a[$i] (which is # the $k of before) and with $h{$a[$i]} print $a[$i], " ", $h{$a[$i]}, "\n"; }
evif
this next one is xis
and
improves on ruof
. Stage one first:
When you run it what potential expressiveness remains unexpressed?#!/usr/bin/perl %images = ( "One" => "http://www.cs.indiana.edu/dept/img/lh01.gif", "Seven" => "http://www.cs.indiana.edu/dept/img/lh07.gif", "Eight" => "http://www.cs.indiana.edu/dept/img/lh08.gif", "Nine" => "http://www.cs.indiana.edu/dept/img/lh09.gif" ); &printTop; print "Let me show you my images: <p>"; @lst = (keys %images); $input = $ENV{"QUERY_STRING"}; print "Your input is: $input <p> "; foreach $i (@lst) { $name = $i; if ($input eq $name) { $pic = $images{$i}; print qq{$name: (shown below) }; print qq{<img src="$pic"><p>}; } else { print "$name (not shown) "; } } &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>}; }
(How can you get specific images from the script, that is)?
And now Stage Two:
12. Can you see the improvement?#!/usr/bin/perl %images = ( "One" => "http://www.cs.indiana.edu/dept/img/lh01.gif", "Seven" => "http://www.cs.indiana.edu/dept/img/lh07.gif", "Eight" => "http://www.cs.indiana.edu/dept/img/lh08.gif", "Nine" => "http://www.cs.indiana.edu/dept/img/lh09.gif" ); &printTop; print "Let me show you my images: <p>"; @lst = (keys %images); $input = $ENV{"QUERY_STRING"}; print "Your input is: $input <p> "; $pic = "http://www.cs.indiana.edu/l/www/classes/a202-dger/sum99/a202.gif"; foreach $i (@lst) { $name = $i; if ($input eq $name) { $pic = $images{$i}; print qq{$name: (shown below) }; } else { print "$name (not shown) "; } } print qq{<p> <img src="$pic"><p>}; &printBottom; sub printTop { print qq{Content-type: text/html\n\n<html> <head><title>My Pictures Script</title></head> <body bgcolor=white> }; }
Is there any improvement? (Try to look for differences, if not).
13. Here's neves
which improves the user interface.
(Review#!/usr/bin/perl %images = ( "One" => "http://www.cs.indiana.edu/dept/img/lh01.gif", "Seven" => "http://www.cs.indiana.edu/dept/img/lh07.gif", "Eight" => "http://www.cs.indiana.edu/dept/img/lh08.gif", "Nine" => "http://www.cs.indiana.edu/dept/img/lh09.gif" ); &printTop; print "Let me show you my images: <p>"; @lst = (keys %images); $input = $ENV{"QUERY_STRING"}; print "Your input is: $input <p> "; $pic = "http://www.cs.indiana.edu/l/www/classes/a202-dger/sum99/a202.gif"; foreach $i (@lst) { $name = $i; if ($input eq $name) { $pic = $images{$i}; print qq{$name: (shown below) }; } else { print qq{ <a href="neves?$name">$name</a> }; } } print qq{<p> <img src="$pic"><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>}; }
eno
and owt
if not clear). The program now is again geared up for user interaction.
14. Here's thgie
that has a default case.
Nothing new, just a different approach, actually.#!/usr/bin/perl %images = ( "One" => "http://www.cs.indiana.edu/dept/img/lh01.gif", "Seven" => "http://www.cs.indiana.edu/dept/img/lh07.gif", "Eight" => "http://www.cs.indiana.edu/dept/img/lh08.gif", "Nine" => "http://www.cs.indiana.edu/dept/img/lh09.gif" ); &printTop; print "Let me show you my images: <p>"; @lst = (keys %images); $input = $ENV{"QUERY_STRING"}; print "Your input is: $input <p> "; foreach $i (@lst) { $name = $i; if ($input eq $name) { print qq{$name: (shown below) }; } else { print qq{ <a href="thgie?$name">$name</a> }; } } if ($images{$input}) { $pic = $images{$input}; } else { $pic = "http://www.cs.indiana.edu/classes/a202-dger/sum99/a202.gif"; } print qq{<p> <img src="$pic"><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>}; }
15. What do we need to do if we don't want to have to change thgie
to enin
later?
16. Here's enin
that does not care about that:
17. How does it do it?#!/usr/bin/perl %images = ( "One" => "http://www.cs.indiana.edu/dept/img/lh01.gif", "Seven" => "http://www.cs.indiana.edu/dept/img/lh07.gif", "Eight" => "http://www.cs.indiana.edu/dept/img/lh08.gif", "Nine" => "http://www.cs.indiana.edu/dept/img/lh09.gif" ); &printTop; @lst = (keys %images); $input = $ENV{"QUERY_STRING"}; print "<table border cellpadding=6 width=100%><tr>"; $script = $ENV{"SCRIPT_NAME"}; foreach $i (@lst) { $name = $i; if ($input eq $name) { print qq{<td> $name </td> }; } else { print qq{ <td> <a href="$script?$name">$name</a> </td> }; } } print "</tr>"; if ($images{$input}) { $pic = $images{$input}; } else { $pic = "http://www.cs.indiana.edu/classes/a202-dger/sum99/a202.gif"; } print qq{<tr> <td align=center colspan=4> <p> <img src="$pic"><p> </td> </tr> }; print "</table>"; &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>}; }
18. What else is enin
adding to the previous stage
(and is taken straight from eno
and owt
)?
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?
(What's that: GET? And, is there any other method?)#!/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>}; }
22. This is three
a bit reorganized for better focus.
(When trying this out, one needs to observe the changes that occur in the URL.)#!/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>}; }
23. Let's make a change (four
) and experiment.
If we missed it at 22, it should now be clear: we're getting the values inside.#!/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>}; }
24. Is your calculator working already?
(Still, the format's a bit strange).
25. Are we getting any closer?
(Yes, but we need to take the input string apart).
26. Perhaps we should take a break?
(Yes, I think I need a way to store intermediary results, too).
27. OK, let's stop here. See you next time.
(You bet.)