Create a CGI script that will print to the browser:
The name of the server is: <Put the values here.>
The gateway protocol is:
The client machine's IP address:
The client machine's name:
The document root is:
The CGI script name is:
(Hint: Use the %ENV hash.)
Creating A CGI Program
Write a CGI script called town_crier that will contain HTML text and Perl statements.
The script will contain two subroutines: & welcome and & countem .
The welcome subroutine will print Welcome Sir Richard!! . Use a blue font that blinks the welcome. (Note: Internet Explorer ignores blink .) The subroutine will also print today's date. (Use the ctime library function.)
The subroutine called countem will be written in a file called countem.pl . The town_crier script will call countem passing its name ( town_crier ) as an argument to the subroutine. Remember, the name of the script is stored in the $0 variable, e.g., &countem ($0); . The subroutine will return the number of times the page has been visited.
See Figure 10.40 for an idea of how this script will display its output in the browser's window.
The countem function should be designed to
Take an argument ” the name of the file that called it. Unless there is a file called town_crier.log already in the directory, the file will be created. Either way, the file will be opened for reading and writing. (If the countem function were called from another Perl script, then the log file created would have the name of that script, followed by the .log extension.)
If the log file is empty, countem will write the value 1 into the file; otherwise a line will be read from the file. The line will contain a number. The number will be read in and stored in a variable. Its value will be incremented by 1 . Each time town_crier is executed, this function is called.
The new number will be sent back to the file, overwriting the number that was there.
The log file will be closed.
The countem subroutine will return the value of the number to the calling program. (In the example, I put the number in a cell of an HTML table and sent the whole string back to the town_ crier . Don't bother to try to create the table if you don't have time. Just send back the number.)
If running on a UNIX system, use the flock function to put an exclusive lock on the log file while you are using it and will remove the lock when you are finished.
Figure C.40. Output of the CGI program in Exercise C.1.