Introduction to Perl
Programming Language Concepts

ICSS 4003-450

Winter Quarter 20062


Due Date

Friday February 16, 2007

Goals

 

Overview

 

Perl is short for “Practical Extraction and Report Language” and was invented by Larry Wall in the mid 1980’s.  It’s a highly flexible shell programming language that is omnipresent on the web and is a critical tool for many programmers.  In this lab you will be writing 9 very short and simple (famous last words) Perl programs which will build your understanding of the language.

 

Pre-lab Work

 

Read over the Perl Tutorial.  You’ll be using this as a general reference when working on the lab.  If you purchased the recommended Perl book, Learning Perl, be sure to bring it to lab with you, as it will prove to be an invaluable reference.

 

 

In-lab Work

 

For this lab, you will be working individually.  I encourage you to discuss the lab with your fellow classmates, but do not provide full solutions to each other.

 

Running perl

 

On our systems, the Perl interpreter is located in /usr/local/bin/perl and it is also linked to /usr/bin/perl.  The executable should already be in your path so it’s a simple matter of invoking it via:

           

% perl

 

Use ^D to exit the interpreter.  To invoke a program, simply provide the name of the program as the second command line argument.

 

There are several sample programs out in /usr/local/pub/chr/courses/plc/perl/  Try running the Perl program total:

 

% perl /usr/local/pub/chr/courses/plc/perl/total

 

It will prompt you to enter a series of numbers, followed by ^D, and it will produce a result to standard output.  Copy the program to your local directory and look at its contents.  The first line of the program has a unique signature:

 

#!/usr/bin/perl

 

Since it can be cumbersome to reference the interpreter every time you want to run a program, UNIX lets you specify the location of the program to run in the first line of the file.  You also must set the permissions of the file to allow execution.  Take a look at the total program:

 

% ls –l total

-r-xr-xr-x  1     chr   fac   1185  Jun 23 15:56      total*

 

Your output may be slightly different, but the key thing to look at is the permission bits at the start of the last line.  By having the x bit (execute bit) turned on, this program can be invoked via:

 

% total

 

By default a new file will not have this execute bit turned on.  The easiest way to make the file executable is:

 

% chmod u+x newfile

 

One other useful feature that you’ll want to know before diving in is how to enable warnings.  The use warnings pragma creates diagnostic messages, which would otherwise be skipped, which can lead you to potential problems in your program.  To use this, run perl with the option:

 

% perl –w newfile

 

Or change the top lines of your program to have:

 

#!/usr/bin/perl –w

 

With this enabled, you will see warning messages echoed to standard error that would normally be supressed.  If you are a Perl newbie, it is a good idea to have all your programs start with the line above for your own protection.

 

You now should have enough information to start causing some real damage!

 

Perl Fundamentals

This activity is due on Friday February 16, 2007

 

For each question you are to create a separate Perl file.  For naming sake, you will append the .pl extension to each of your Perl programs (although strictly speaking it is not necessary).

 

Question 1: hello.pl

 

Write a program that runs and prints the following message to standard output.

 

Sample output:

 

Hello, world!

 

Question 2: append.pl

 

Create a scalar variable, initials, for your initials in string form.  Create a scalar variable, $igits, for your 4 digit id in integer form.  Combine the two scalars into a new scalar, username, using the . operator.  Print username to standard output.

 

Sample output:

 

sps1234

 

Question 3: langs.pl

 

Create a list variable, @lang, which contains the strings which represent the 3 languages we are covering in depth this quarter.  Use the qw keyword to generate the initial list.  Now use push to add 3 more languages of your choosing onto the end of the list.  Print out the length of the list without looping over the elements.  Print out the final list sorted alphabetically. 

 

Sample output:

 

6

Scheme, Perl, G2, Python, C++, Algol

 

Question 4: cli.pl

 

Write a program that takes the command line arguments and echoes the total number of arguments and prints them to standard output, one per line.  Hint - try using @ARGV, foreach and $_.

 

Sample command input:

 

% cli.pl arg1 arg2 arg3

 

Sample output:

 

3

arg1

arg2

arg3

 

Question 5: repeat.pl

 

Write a program which prompts and reads a character string and a number (on separate lines of input), and prints out the string, once per line, the number of times indicated by the number.  Hint: use the x operator.

 

Enter the string and the number (separate lines):

Fred

3

Here’s your output:

Fred

Fred

Fred

 


Question 6: passwd.pl

 

Modify passwd so that the entire file is printed with the symbol # at the beginning of each line.

 

Sample passwd file:

 

abc123:x:10:1:User ABC:/:/sbin/sh

sps000:x:12345:5:User SPS:/:/sbin/sh

 

Sample output:

 

#abc123:x:10:1:User ABC:/:/sbin/sh

#sps000:x:12345:5:User SPS:/:/sbin/sh

 

Question 7: uid.pl

 

Modify passwd to split each line into a list and print out the username with the highest user id.  The username is the first field, and the user id is the third field in the password record for an individual.  The fields are separated by colons.

 

Sample passwd file:

 

abc123:x:10:1:User ABC:/:/sbin/sh

sps000:x:12345:5:User SPS:/:/sbin/sh

 

Sample output:

 

Username = sps000, UID = 12345

 

Question 8: circumference.pl

 

Write a subroutine, circumference, which computes the circumference of a circle:

 

            C = 2 * PI * R

 

The program should read the radius from standard input (no prompt required).  You may use 3.141592654 as your value for PI.  The program should output a string containing the value and the circumference.

 

Sample input:

 

5

 

Sample output:

 

The circumference of 5.000000 is 31.415927

 


Question 9: browser.cgi

 

Write a CGI script, browser.cgi, in Perl, that will echo some information to the user who is browsing your page.  Note that the extension of this file is .cgi.  It should use the pre-defined hash table, %ENV, which contains the following variables as lookup entries.  The output should contain the IP address of the visitor and the name of the browser they are running on (Internet Explorer or Mozilla only).

 

Sample output on a web browser page:

 

Hello, visitor from 129.21.36.56

You are running the script /home/fac/chr/public_html/cgi-bin/browser.cgi

You are running the Mozilla browser

The server administrator is webmaster@cs.rit.edu

 

There is a good tutorial on using CGI at http://www.cgi101.com/class/.  Use it!  (at least the first parts)

 

Make a subdirectory under your public_html directory which is called cgi-bin. Make sure to set the permissions of cgi-bin to world readable and executable.  Also be sure that the entire directory path including the root of your account has world execute.  To test your code, copy the program into your cgi-bin directory and then point your browser to http://www.cs.rit.edu/~username/cgi-bin/browser.cgi.  Remember to change the username.

 

To test whether your code has any syntax errors use:

 

% perl –c browser.cgi

browser.cgi syntax ok

 

How to Submit:

 

Upon successful completion and unit testing, submit all 8 perl files (minus the browser.cgi script) as a set of e-mail attachments to chr@cs.rit.edu.  Be certain before submitting your programs that they all have execute permission and run properly in the CS Unix environment! 

Include in your e-mail the URL for executing your browser.cgi script.  I will test your work on question 9 by going to your cgi-bin directory with my browser.

 

 

 

Grade Computation

 

The grade breakdown for this lab is as follows:

 

Questions 1-8: 10 points each

Question 9: 20 points