#!/usr/bin/perl
There should generally not be anything after this statement. If you aren't sure where Perl lives on your system, try typing this command:
which perl
If the system can find it, it will tell you the path name to Perl. That path
is what you should put in the above statement.Beyond the first line, you'll write your perl code. Perl is case sensitive, meaning that 'x' is not the same as 'X'. Also, most lines of Perl code must end in a semicolon (;), unless it is a loop or other special conditional structure. We'll cover those later.
Let's write a simple first program. Enter the following lines into a new file, and call it "first.pl".
#!/usr/bin/perl print "Hello, world!\n";
Save the file. Now, from the Unix shell, you'll need to type:
chmod 755 first.plThis fixes the file permissions (via the change mode command) so that the script can be executed (run). You will have to do this every time you create a new script; however, if you're re-editing an existing script, the permissions should remain the same.
Now, to run the script, just type:
./first.plIf all goes well, you should see it print Hello, world! to your screen.
Now, let's say you want to have a few variable names in your script. A variable is just a name that stores some value in it, so you can refer to it or manipulate it throughout your program. Perl variables are prefixed with a dollar sign ($), so for example, $x, $y, $z, $username, and $url are all examples of variable names.
Let's edit first.pl again and add some variables to it:
#!/usr/bin/perl $name="HAL"; print "Hello there. What is your name?\n"; $you = <STDIN>; chop($you); print "Hello, $you. My name is $name. Welcome to the CGI Class.\n";
Save, and run the script just as before. Notice the results? First a few comments. This line:
$you = <STDIN>;actually reads input from your keyboard. The program will pause and wait for you to type something in, followed by a carriage return. Whatever you typed is stored in the variable $you. Since $you also contains the carriage return itself, we use
chop($you);to remove the return from the end of the string you typed in. (the chop function simply removes the last character from any string.) The following print statement:
print "Hello, $you. My name is $name. Welcome to the CGI Class.\n";substitutes the value of $you that you entered in , as well as the value of $name that was defined earlier in the script. The "\n" at the end if the line is the perl syntax for a carriage return.
Now let's take the original program that was in first.pl, and make it into a CGI script that displays a web page. If you are running this on an io.com account, or another Unix that lets you run CGI's in your public_html directory, you will probably need to rename the file to first.cgi, so that it ends in the .cgi extension.
Here is what it should look like:
#!/usr/bin/perl print "Content-type:text/html\n\n"; print "<html><head><title>Test Page</title></head>"; print "<body>"; print "<h2>Hello, world!</h2>"; print "</body></html>";Now, first save this file and run it in the Unix shell, like you ran the other one. Notice how the script will just print out a bunch of HTML? This is what it should do, BUT, very importantly, if there's an error in your script, the perl interpreter in Unix will tell you exactly what line the error is on. This is good to remember in the future, because when you're writing longer, more complex scripts, you may get bugs or errors, and the error message you get on the web site (500 Server Error) is not useful at all for debugging.
Now you'd like to use this script as an actual CGI. You don't need to call it from a web page. Just move it into your public_html or public_web or CGI-bin directory, and call it directly. For this example, here's my URL:
Another way to write the above CGI, without using multiple print statements, is as follows:
#!/usr/bin/perl print "Content-type:text/html\n\n"; print <<EndOfHTML; <html><head><title>Test Page</title></head> <body> <h2>Hello, world!</h2> </body></html> EndOfHTML ;Note that there are no spaces between the << and the EndOfHTML, in this statement:
print <<EndOfHTML;
Also, there should be no spaces before the closing EndOfHTML - it must be flush left against the left margin. (If you get an error along the lines of "Can't find string terminator EndOfHTML", this means you forgot to move the EndOfHTML to the left margin.)
This manner of outputting HTML will become more useful with future CGI's, because it doesn't require you to escape embedded quotes, like you would with a normal print statement:
print "<a href=\"http://www59.metronet.com/\">foo</a>";Note that the quotes around the URL have to be escaped with a backslash in front of them for it to work.
Lesson 2 will cover parsing environment variables, so you can do something useful with CGI's.