#!/usr/bin/perl
print qq{Content-type: text/html\n\n};
print qq{<html><head><title>Title</title<>/head><body>};
print qq{<h1>Hello!</h1>};
print qq{</body></html>};
We abstract a few actions:
#!/usr/bin/perl
sub HtmlTop {
print qq{Content-type: text/html\n\n};
print qq{<html><head><title>Title</title<>/head><body>};
}
sub HtmlBot {
print qq{</body></html>};
}
# and here comes the actual program
&HtmlTop;
print qq{<h1>Hello</h1>};
&HtmlBot;
We have looked at %ENV:
&HtmlTop;
foreach $key (keys %ENV) {
print $key, " --> ", $ENV{$key}, "\n";
}
&HtmlBot;
or with each as explained in the lab.
The essence of CGI is to use the values transmitted through these variables, do some processing, and send back a document that describes the result of the computation.
A few of the environment variables that will be of interest to us:
Maybe we can do that randomly.
rand generates random numbers.
But you need srand first.
Although the distribution is OK the sequence is deterministic.
The general use is:
which returns a random fractional number between 0 and the value to whichrand (EXPR)
EXPR evaluates to. Generate 10 random numbers between 0 and 50:
for ($i=0; $i<10; $i++) {
print rand(50);
}
Every time you run it: same numbers.
srand($ARGV[0]);
for ($i=0; $i<10; $i++) {
print rand(50);
}
is more flexible, you cant start it wherever you want it. Numbers are real (floating point).
If you need integers:
for ($i=0; $i<10; $i++) {
print int(rand(50));
}
which truncates them. Now we want to be surprised, we want the sequence to be out of our reach. We will use the system time.
$seconds = time;
$date = localtime;
print $seconds, " seconds since Jan 1970, ",
"today's date and time is: $date\n";
time returns a number of seconds, and localtime
returns an actual date in a scalar context (such as the one dictated by $date).We can use this to generate numbers:
srand(time);
for ($i=0; $i < 10; $i++) {
print rand(50);
}
Even less predictable:
wheresrand (time % $$); print rand(50);
% is the modulus operator and $$ is the process id. So now we are ready to print random quotations.
@sayings = ("saying 1", "saying 2", "saying 3");
srand(time % $$);
print int(rand($#saying + 1));
Everytime we run this program we get a new one, hard to predict which. Now we return to the world of scripts:
print the date on the server.$time = localtime; &htmltop; print $time; &HtmlBot;
The output varies but we can't control it.
Fortunately we remember the %ENV experiment.
Let's redo part of it. Create myscript:
#!/usr/bin/perl
&HtmlTop;
$qs = $ENV{'QUERY_STRING'};
print "The query string is: ", $qs;
&HtmlBot;
Now call this program like this:
and thenhttp://tucotuco:19800/cgi-bin/myscript
$ENV{'QUERY_STRING'} binds to the part that comes after ? (question mark). That's the convention. Now that we know it we can use this as a sort of $ARGV[0] parameter:http://tucotuco:19800/cgi-bin/myscript?test
#!/usr/bin/perl
&HtmlTop;
$qs = $ENV{'QUERY_STRING'};
if ($qs eq "time") {
print "The date is: ", localtime;
} elsif ($qs eq "hostname") {
print "You are calling from: ",
$ENV{'REMOTE_HOST'};
} elsif ($qs eq "quotation") {
@sayings = ("saying 1", "saying 2", "saying 3");
srand (time % $$);
print $sayings[int(rand($#sayings + 1))];
} elsif ($qs eq "ipnumber") {
print "Your ip number is ",
$ENV{'REMOTE_ADDR'};
} else {
print "Hello!";
}
&HtmlBot;
So now we can query the script from the 'command line' (that is, the URL).