您的位置:寻梦网首页编程乐园CGI编程Zhanshou's CGI Tutorial

Introduction to PERL


Perl (Practical Extraction and Report Language) is a language first designed for UNIX system administration. By borrowing heavily from C, sed, awk, and the Unix shells, Perl has become the language for many file processing and management, process management, and system administration tasks with its sophisticated pattern matching capabilities and flexible syntax. But it was not until the birth of CGI that PERL became so popular. Today no other language matches PERL's position in the CGI world. Over the years, Perl has been ported to many different platforms such as MS-DOS and Windows NT.

Unlike C and other compiled programming languages, Perl is interpreted. So we often call the Perl program "Perl script". The latest version is Perl 5.0.

Hello, World Example

Here is the famous Hello World example that we'll use to get started.

#!/usr/local/bin/perl
#-------------------------------------------
# helloWorld.pl by Zhanshou Yu
#-------------------------------------------

 #This is a html file
 print"Content-type: text/html\n\n";

 #The html output 
 print "<html>\n";
 print "<head><title>Hello World !!!</title></head>\n";
 print "<body>\n";
 print "<h2>Hello World !!!</h2>\n";
 print "</body>\n";
 print "</html>\n";
 exit;

Program Components

There are three main parts to this program:

  • The first line:

    #!/usr/local/bin/perl
    

    which starts with "#!", often appears as the first line in most Perl programs (especially for CGI programs). This line tells the computer to find the Perl intepreter, then run this program when it is executed. You must specify the Perl intepreter's absolute path in the system, which usually is at the "/usr/local/bin/" directory, so that the Perl intepreter can be found correctly . This is important for CGI programs because it runs through the Internet. Of course, if you run the program in a local system in the command line, the first line is not neccessary. You can just issue the following command in your Shell:

          perl helloWorld.pl
    
    It works well either with or without the first line. But for a CGI program, the first line is neccessary.

    If you do not know the directory in the system, you can use the:

           which perl
    
    command to check the directory in which directory Perl resides.

  • Comments

    Except for the first line, the content after "#" to the end of the line is the comments. The only way to stretch comments over several lines is to use a # on each line.

  • Statement

    Everything else is a Perl statement which must end with a semicolon, like the last line above. The print function outputs the "Hello World!" message.

How to Run the program

  • Run the program in shell

    There are two ways to run the program in a shell command line:

    1. run it directly using the Perl intepreter:
           perl helloWorld.pl
      
    2. First change the mode to be executable:
           Chmod 755 helloWorld.pl
      
      Then issue the command:
           helloWorld.pl
      
  • Run as a CGI program

    First change the mode to be executable:
             chmod 755 helloWorld.pl
      
    Then include it in HTML form as we talked before:
    <form method=post action="cgi-bin/helloWorld.pl">
    <input type=submit value="Run Example">    
    </form>
    
    Click the button to run the example:

Previous Page Table of Content Next Page