CMPS 335 Advanced Web Publishing Perl and CGI Programming


Introduction to CGI and Perl

HTML is a markup language that can be used to create only static Web pages to display information.  HTML does not have facilities for user interaction, data processing, or access to a database.  One of the approaches to adding these abilities is to execute programs on the Web server.  With CGI (Common Gateway Interface) and Perl, we can create interactive Web applications that are more exciting and useful.

What is CGI? What is Perl? Web Servers Perl Scripts
Creating or Editing a Perl Script
   Example: $ pico example1a.cgi

Displaying Acess Permissions of a Perl Script
   Example: $ ls -l example1a.cgi

Changing Permissions of a Perl Script
   Example: $ chmod 755 example1a.cgi

Checking Syntax Erros of a Perl Script
   Example: $ perl -c example1a.cgi

Running a Perl non-CGI Script (in the Unix shell)
     Example: $ perl example1a.cgi

Running a Perl CGI Script
     (a) Running the Perl script in the Unix shell
     Example: $ perl example2a.cgi
     (b) Running the Perl script in the browser's window
     Example: http://cs.selu.edu/~jhu/cgi-bin/example2a.cgi   (URL in the Address box)

The print Function

The print function is essential to Perl CGI scripts.  It is the key to displaying the information gathered or processed by the scripts.  The print can display data the monitor screen or save data to a file.  The most frequently used print escape sequence is \n.

A print list can be enclosed by either double quotes or single quotes.  When double quotes are used, the list is printed with variable interpolation.  When single quotes are used, the list is printed exactly as it apears without variable interpolation.

The heredoc Operator (<<)

You can use the heredoc operator to save you time of typing, without using multiple print statements and \n escape sequences.   The syntax of heredoc is:
      print <<"heredoc-marker";  or print <<heredoc-marker;
      .
      .
      heredoc-marker

  where heredoc-marker is a word or a phrase.  The heredoc
  marker is a label to signal the end of the print statement.

  Note: 
   a. There should be no spaces between the heredoc operator << and 
      the heredoc marker.
   b. The ending heredoc-marker should start in column 1 and should contain
      nothing else, not even a trailing space or semicolon.
   c. You can enclose the heredoc-marker in single quotation marks so that
      there is no variable interpolation.

Simple Perl non-CGI Scripts
  (A) Example 1a  (Without using the heredoc operator)

  [jhu@cs cgi-bin]$
  [jhu@cs cgi-bin]$ chmod 755 example1a.cgi
  [jhu@cs cgi-bin]$ ls -la example1a.cgi
  -rwxr-xr-x    1 jhu      jhu           363 Aug 13 16:00 example1.cgi
  [jhu@cs cgi-bin]$ perl -c example1a.cgi
  example1.cgi syntax OK
  [jhu@cs cgi-bin]$ cat example1a.cgi
  #!/usr/bin/perl
  # --------------------------------------------------------
  #  Perl script to run in the UNIX command shell 
  #  (Without using the heredoc operator)
  # --------------------------------------------------------
  #  This program is not a CGI.  It is a simple Perl script.
  #
  $you = "Janice";
  print "Hello, $you.\n";
  print "Welcome to Perl CGI Programming.\n\n";
  print 'Hello, $you.\n';
  [jhu@cs cgi-bin]$ perl example1a.cgi
  Hello, Janice.
  Welcome to Perl CGI Programming.

  Hello, $you.\n

  (B) Example 1b   (Using the heredoc operator)

  [jhu@cs cgi-bin]$ perl -c example1b.cgi
  example1b.cgi syntax OK
  [jhu@cs cgi-bin]$ cat example1b.cgi
  #!/usr/bin/perl
  # --------------------------------------------------------
  #  Perl script to run in the UNIX shell 
  #  (Using the heredco operator <<)
  # --------------------------------------------------------
  #
  $you = "Janice";
  print <<"End1";
  Hello, $you.
  Welcome to Perl CGI Programming.
  End1
  print <<'End2';
  Hello, $you.
  Welcome to Perl CGI Programming.
  End2

  [jhu@cs cgi-bin]$ perl example1a.cgi
  Hello, Janice.
  Welcome to Perl CGI Programming.

  Hello, $you.\n

Simple Perl CGI Scripts
(A) Example 2a  (Without using the heredoc operator)

  [jhu@cs cgi-bin]$ chmod 755 example2a.cgi
  [jhu@cs cgi-bin]$ perl -c example2a.cgi
  example1.cgi syntax OK
  [jhu@cs cgi-bin]$ cat example2a.cgi
  #!/usr/bin/perl
  print "Content-type:text/html\n\n";
  #
  #-------------------------------------------------------------
  #  Perl CGI script that creates output for a browser
  #  (Without using the heredoc operator)
  #-------------------------------------------------------------
  #  Perl does not interpret HTML tags
  #  Browser does not interpret \n (Perl print escape sequence)
  #
  $you = "Janice";
  print "<html>\n<head><title>Example 2a</title></head>\n";
  print "<body>\n<br><br>";
  print "<font size=4><b>Hello, $you.</b>
  <br>Welcome to Perl CGI Programming.</font>\n";
  print </body>\n</html>\n";


  Running the script in the Unix command shell

  jhu@cs cgi-bin]$ perl example2a.cgi

  <html>
  <head><title>Example 2a</title></head>
  <body>
  <br><br><font size=4><b>Hello, Janice.</b>
  <br>Welcome to Perl CGI Programming.</font>
  </body>
  </html>

  Running the script (example2a.cgi) in the browser's window 
  (Enter the address: http://cs.selu.edu/~jhu/cgi-bin/example2a.cgi
   and the following output appears in the browser's window)
  
  Hello, Janice. 
  Welcome to Perl CGI Programming. 

  (B) Example 2b  (Using the heredoc operator)

  [jhu@cs cgi-bin]$ chmod 755 example2b.cgi
  [jhu@cs cgi-bin]$ perl -c example2b.cgi
  example2b.cgi syntax OK
  [jhu@cs cgi-bin]$ cat example2b.cgi
  #!/usr/bin/perl
  print "Content-type:text/html\n\n";
  #
  # -------------------------------------------------------
  #  Perl CGI script that creates output for a browser 
  #  (Using the heredoc operator)
  # -------------------------------------------------------
  #
  $you = "Janice";
  print <<End
  <html>
  <head><title>Example 2b</title>
  </head>
  <body>
  <br><br><font size=4><b>Hello, $you.</b>
  <br>Welcome to Perl CGI Programming.</font>
  </body>
  </html>
  End

  Running the script in the Unix shell

  [jhu@cs cgi-bin]$ perl example2b.cgi
  Content-type:text/html

  <html>
  <head><title>Example 2b</title>
  </head>
  <body>
  <br><br><font size=4><b>Hello, Janice.</b>
  <br>Welcome to Perl CGI Programming.</font>
  </body>
  </html>

  Running the script in the browser's window  
  (The following output appears in the browser's window)

  Hello, Janice. 
  Welcome to Perl CGI Programming. 
Example of An FTP Session
   C:\> ftp cs.selu.edu
   User: (your user name)
   Password: (Your password)
   ftp> cd public_html/cgi-bin
   ftp> ascii
   ftp> send a:\counter.cgi   
   ftp> send a:\count.dat
   ftp> cd ../
   ftp> bin
   ftp> send a:\butterfly.gif
   ftp> quit
   C:\> exit
  Click Here for Unix commands
   Also see Appdendices B and D for more on file permissions and Unix commands
  Click Here for more on Pico

Return to CMPS 335 Home Page
Return to Web Site Home Page