|
Perl Case Study - Simple CGI
#!/usr/local/bin/perl
# What this script does: print "Content-type: text/html\n\n"; # Print a Hello World HTML page:
print qq|
See the script in action: simple.cgi The good news: it's just that simple! Here you've seen a full-fledged Perl CGI that works on on a UNIX server (given that perl is installed). There are 5 elements demonstrated here which are useful to know. The first line is rather important. It points to your perl interpreter (the stuff after the first #!). And this may actually vary from what I've written above. How to find out: if you have telnet, type which perl at the prompt. The number sign # works much like a // comment line does in C++. It basically tells the perl interpreter to disregard that commented line of text. As you can see from the script, putting multiple #s in a row don't make a difference (just done for aesthetic purposes). So near the top I've included a little file description, all commented out. The first line that was printed onto the screen is "Content-type: text/html\n\n" which just happens to be a HTTP header. This is required by the server to get this kind of output to the browser. I mentioned a few more HTTP headers in my analysis of Meta Tags. If you were to take out this line, you would be given a nasty 500 Server Error which happens to be one of the most annoying and common errors seen when trying to execute a CGI script [click here to get a list of server error messages with corresponding numbers] . Another important thing to see is the actual ouput command. Perl uses the print "The text you want to output goes inside these quotes."; syntax. Notice the semicolon; it is VERY important that there is a semicolon at the end of the statement (same goes for C++)! This print is swell and fine for outputting one line... but what if you wanted to write a whole web page code into it? Quite tedious. So there's another type of printable quoting using print qq* ... *; where (*) refers to any character type (the example used | ) called a handle. So you can write a whole page, spacing and all, without ending on each line and worrying about syntax errors. An added plus is that it isn't even bothered by quotes ("). print qq| (beginning statement) |; (end statement) Make sure that the character (*) that is used after the qq does not show up before the ending statement (the print routine here truncate as soon as it encounters this character - if it is cut off prematurely, it will serve you up a 500 Server Error message). Some not-so-commonly used characters include | * ~ ^ { and } A method similar to qq is to use what is called the "here-document syntax:
print <<EOF; There's one classic "what if" case I want to mention at the start before you start getting (more) confused. What if the text in the print command has quotes?
Example:
A better example would've been using quotes in HTML: But in any case, the print function will interpret the first end quote it finds and truncates the line. It will eventually happen so, what can you do to prevent this type of error, apart from eliminating quotes from your all your text? This is a situation is for super spiffy metacharacters, in particular the backslash ( \ ). It marks the character to its right a literal character (meaning that the print function will interpret a blackslashed quote just as another character to print out!). And here are the two examples with the backslashes added in. These shouldn't give you server errors now: print "The Curious One, George, said in his ape-ish tone of voice, \"Can I have a banana?\" "; print "<a href=\"thispage.html\" target=\"_none\">Click Here</a>"; With the advent of backslashed characters, you get special characters like the newline ( \n ). Newline works like <br>, except helps space your characters in the code (not in the browser since it is invisible and counted like whitespace). So you can write something like: print "<a href=\"thispage.html\" target=\"_none\">Click Here</a>\n"; Below is a list of special characters, escape sequences, and control characters that perl accepts:
A piece of trivia at this point: a list of metacharacters \ | ( ) [ { ^ $ * + ? .
|
||||||||||||||||||||||||||||||||||||||
©1999 Team 26297 "Ad Infinitum Web." All rights reserved. Any reproduction of this document for commercial or redistribution purposes without the permission of the author is forbidden.