Advertisement

Tutorials

Home Tutorials Beginner Tutorial

Your First Perl Script

3.9/5.0 (11 votes total)
Rate:

John Saya
March 08, 2006


John Saya

http://www.cgiconnection.com/
John Saya has written 2 tutorials for CGIDir.
View all tutorials by John Saya...

All Perl scripts start off with the same line. It tells the server where the Perl interpreter is (Generally this does not matter on a Windows based server). There are two common locations, /usr/local/bin/perl and /usr/bin/perl. So, the very first line in your script should be:

#!/usr/bin/perl

Now keep in mind that every command in your script must end with a semicolor (;). This does not mean the entire command must reside on one line. It could span multiple lines, but the Perl interpreter knows it's the end of the command when it sees the semicolon. However, you should try to keep them on one line to make things less confusing. For example, if we wanted to print Hello world! to the screen, our script would look like this:


#!/usr/bin/perl

print "Hello world!\n";
exit;



The \n is the newline character, and does just that, goes to the next line.

Now, there are a couple things to keep in mind about scripts. First, most servers require you to give execute permission to a script before it will run at all. So, let's say you named the above script myscript.cgi. Generally on a Windows machine you would need to set this permission by your web server software, but at the command line, you probably don't need to do anything. However, on a UNIX based server, such as Linux, Solaris, BSDOS, or any other flavor, you would use the CHMOD command. So, you would go to the directory that the script resides in and type:

chmod 755 myscript.cgi

Usually the command is entered all in lowercase. Most UNIX platforms are case sensitive, so your filename is also case sensitive. This means that Myscript.cgi is not the same as myscript.cgi. The above example now gives the user, group, and world executable permission of that script. To execute it from the command line, you would just type the filename (myscript.cgi). On some UNIX platforms, you may need to use ./myscript.cgi instead. This tells the server to execute the script in that directory or folder. Be very careful when assigning permissions to scripts because other users may be able to execute and maliciously use your them!

Another thing to know about web programming, is the above script would not execute properly if called from your browser. First, the script must be in your cgi-bin directory. This is generally a folder (usually called cgi-bin) that allows scripts to be executed over the web. All of your scripts must be in that one folder if being called from the web. You cannot just create this folder. It must be set up by the web server software (which is usually done by a person from a web hosting company). Second, you must tell the browser that you want it to receive information. To do that, you will send a header. There are many different types of headers depending on what type of data you will be sending, but we'll just focus on one. It's the text/html header. This basically tells the browser it will be receiving a web page. So, the browser can now properly display the incoming data. Now, to see the above script in action over the web, it would look like this:


#!/usr/bin/perl

print "Content-type: text/html\n\n";
print "<HTML><BODY>\n";
print "Hello world!\n";
print "</BODY></HTML>\n";
exit;




In the above script we only added three lines. First is the content header type, the second are the HTML opening tags, and the last are the HTML closing tags. Now, be sure to save and CHMOD the above script to your cgi-bin directory. Once you do, you should be able to call the script from your web browser like this:

http://www.yourserver.com/cgi-bin/yourscript.cgi

You should note that if you uploaded the script to your cgi-bin directory using an ftp program, make sure that the transfer mode is set to ASCII (or TEXT) and not BINARY. Otherwise, the script will not execute properly.

If you decide to add an entire web page to your script, you might find it to be pretty annoying and redundant to keep using print statements. In this case, you could do this:


#!/usr/bin/perl

print "Content-type: text/html\n\n";

{
print<<END
<HTML><BODY>
Hello world!
</BODY></HTML>
END
}

exit;




What happens here is the Perl interpreter will output everything exactly the way it sees it below the print<<END line until it sees END on a separate line. You can use any boundary words other than END, just be sure you start and end the print statement with the same word and case.

Perl also makes dealing with strings (text) and numbers very easy. You do not need to define them separately like a language such as C++ would. For example, see how I define Hello world! to mystring and the number 10 to mynum:


#!/usr/bin/perl

$mystring = "Hello world!";
$mynum = 10;

print "$mystring\n";
print "$mynum\n";

exit;




Perl automatically can distringuish which is text and which is a number. So, let's put it all together. Below you'll see everything you've learned combined into one script.


#!/usr/bin/perl

$mystring = "Hello world!";
$mynum = 10;

print "Content-type: text/html\n\n";

{
print<<END
<HTML><BODY>
The value of <I>mystring<I> is: $mystring
The value of <I>mynum<I> is: $mynum
</BODY></HTML>
END
}

exit;




You have been shown some of the most important techniques you will need to know in creating your own scripts. You have learned how to set permissions, output data from the command line and to a web browser, and how to define and print variables!


Add commentAdd comment (Comments: 0)  

Advertisement

Partners

Related Resources

Other Resources

image arrow