![]() |
![]() |
cgi-bin
,
CGI,
%ENV
,
QUERY_STRING
,
GET
,
more Perl.
We start with Perl.
We discuss the incremental development of the following program that
This is really very easy:
We write this program and run it.#!/usr/bin/perl while ($x = <STDIN>) { ($com, $arg) = split(/ /, $x); print "You have typed: $x"; if ($com =~ /^bye/i) { print "Good-bye!\n"; exit; } elsif($com =~ /^add/i) { $acc += $arg; print "Acc is now $acc\n"; } elsif($com =~ /^sub/i) { $acc -= $arg; print "Acc is now $acc\n"; } else { print "Acc stays $acc\n"; } }
We thus introduce
STDIN
and the diamond operator
split
operator
=~
)
helloFive
context of last time.
Last time we developed a simple hello
script. Its output was not sophisticated.
The output was coded in HTML, and the only difference between it and a similar (identical, in fact) file was that the output of the script had to start with a label that was specifying what type it had:
followed by a blank line (hence theContent-type: text/html
\n\n
following it). We then extended this script to allow for variable output from it.
We also started talking about the misterious printenv
you found in your cgi-bin
directory.
And we said associative arrays (hashes) are important in Perl.
The purpose of today's lecture is to provide enough information to allow you to implement a script with the following functionality for your next assignment (which has two parts).
The lecture on Thursday will clarify the second part of that assignment.
Here's the prototype for the first part of assignment #2.
Notice that just like helloFive
:
cgi-bin
and does all the work by itself
helloFive
:
Here's what we will need to get this done:
We could, and will telnet
(or, rather, ssh
)
to our server.
printenv
does and what it prints.
printenv
. Once that is done, with the Thursday lecture we will move to:
Thus we return to the original calculator that started these notes.
After that we can define CGI, and implement a function readParse
that captures that definition.
We now start developing the helper program for the first part of the next assignment.
Here's the script we will develop in class:
#!/usr/local/bin/perl print "Content-type: text/html\n\n"; print "<html><body><pre>"; $string = $ENV{'QUERY_STRING'}; foreach $key (keys %ENV) { if ($key eq $string) { print $key, " --> "; print $ENV{$key}, "\n"; } else { print qq{<a href="/cgi-bin/circular?$key">$key</a>}, "\n"; } } print "</pre></body></html>";
Try the script above.
Did you notice the circular
in the code above?
What does it mean? What does it do? Why is it there for? What, if anything, is it?
How can we take it out of the script's code without disturbing the functionality of the script?