Penn State

Web Conference 2004

Writing Perl/CGI Scripts for ITS/ASET Web services

First CGI program

<- Back - Penn State specifics| Up |Passing data - Next ->

The first thing to do when learning a new programming language, if not even just programming in general for the first time, is to do something as simple as possible to prove you understand the basics. Most computing languages start with the simple task of displaying something on the computer monitor, such as the timeless phrase, "Hello, World!" The following Perl CGI script does precisely that. Here is what the program looks like:

#!/usr/local/bin/perl

print <<END;
Content-Type: text/html

<html>
<body>
<p>Hello world!  Here is some HTML inside a basic CGI script</p>
</body>
</html>
END

How to edit and upload your first CGI script

NOTE: The directions here are directed to the Web Conference 2004, Pre-conference tutorial session in which the operating system used was Windows XP with the programs WordPad and SSH Secure File Transfer.

  1. You can download the program by clicking on this link with the right mouse button, and selecting "Save Target As..." or "Save Link Target As...". Save it some place memorable, such as the Desktop. You may need to save as type "All files" to make sure the suffix is .cgi and not .txt or .html.

  2. To edit the file, use WordPad ("Start" button -> "Programs" -> Accessories" -> "Wordpad"). Choose "File" -> "Open", and select helloworld.cgi as you just downloaded. You should recognise the HTML in the middle of the script. Feel free to add your own personal touch to the text "Here is some HTML inside a basic CGI script."

  3. After you save your changes to the file, you will need to upload it to your Personal Web space. We will use SSH Secure File Transfer to upload the file to the "www" folder inside your PASS. If your computer does not have SSH Secure File Transfer, you can download SSH Secure Shell, the program SSH Secure File Transfer comes with from https://www.work.psu.edu/access/ssh/.

    Content for ITS/ASET Web services is stored in PASS (DCE/DFS). You can access PASS through a variety of ways, including through a Web browser (via PASS Explorer), through your native operating system Windows, Macintosh or UNIX (via PASS Gateway), or via SSH Secure File Transfer / MacSFTP. The UNIX savvy may also access PASS via the ITS UNIX Cluster over ssh terminal sessions.

    Unix, Windows and Macintosh each use different byte values to indicate the end of a line in a text file. Text editors that come with Windows such as WordPad do not have the ability to save files in UNIX line-ending format, which are required for CGI scripts. SSH Secure File Transfer is used in these directions because it provides a means to convert line-ending byte-code values to UNIX format.

  4. Once you find "Secure File Transfer Client" under the "SSH Secure Shell" menu, follow the directions on http://www.work.psu.edu/help/acl_explorer_winsftp.html. This will set the permissions in the File Transfer Client to be compatible for CGI programs.

  5. Next, connect to the Personal Secure File Transfer Protocol (SFTP) server, sftp.personal.psu.edu. Click "Quick Connect", and then enter the following:

    Where it readsType in
    Host Name:sftp.personal.psu.edu
    User Name:Your Penn State Access Account userid, eg xyz123

    Leave the rest as is, and click "Connect". When prompted, type in your password.

  6. Select your "www" folder from the list that appears.

  7. Select "Operation" -> "New Folder" to create a new folder in your www folder. Label it "scripts". This is just to keep your CGI scripts separate from the rest of your site. If you already have a scripts folder you can continue to use it for this excersise, or create a new one of a different name.

  8. Select the "scripts" folder, or new folder you created so you are looking inside it on the right side.

  9. Select "Operation" -> "File Transfer Mode" -> "ASCII". This ensures the .cgi file uses the proper line-ending byte values for the specific platform after upload.

    Unix, Windows and Macintosh use different byte values to indicate the end of a line in a text file. When you saved the file in WordPad, you saved it in Windows format. The ASCII file transfer mode converts the line ending values to Unix format so the server can understand it. In the future, make sure you use "Binary" or "Auto" file transfer modes when uploading other files, in particular .jpg and .gif image files. .html files can transfer in any mode.

  10. Select "Operation" -> "Upload", and then select the helloworld.cgi file to upload.

  11. Once the file successfully transfers, use the PASS Explorer program on https://explorer.pass.psu.edu/ to make the script executable. (Updated 2008/12/26 for PASS Migration changes that took effect on July 4, 2008)

    1. Login as needed to Penn State WebAccess

    2. Browse until you see your www/scripts folder:

      1. Scroll through the list of files/folders until you see "www" (note that folders will be surrounded by [ brackets ] ).
      2. Select "www".
      3. Either double-click the folder or press the "Change Folder" button.
      4. Repeat the above steps for the "scripts" folder.
    3. Select "helloworld.cgi"

    4. Press the "Info" button.

    5. Press the "Go to permissions" button.

    6. Select the radio button for "Web Application File Permissions" then press the "<Next > >" button.

    7. Select the radio button for "Set Permissions Needed for CGI" then press the "<Next > >" button.

    8. Select the radio button for "Grant permissions for the test.scripts.psu.edu server to execute this file as a CGI script." then press the "<Apply>" button.

  12. After setting permissions, test it out by pointing your Web browser to http://test.scripts.psu.edu/xyz123/scripts/helloworld.cgi - where "xyz123" is your own userid and "scripts" is the name of the folder you transfered the script to.

  13. If everything worked out, it should look something like this. If so, contratulations! You just installed your first CGI script!

<- Back - Penn State specifics| Up |Passing data - Next ->

If you have any questions, feel free to ask me - mailto:jcd@psu.edu

Content by: Jeff D'Angelo <jcd@psu.edu> © 2004

Last update on: Friday, 26-Dec-2008 16:46:05 EST