Perl Assignment Lab

CS 29

Feb. 27, 1997

Here is the assignment:

Write a program which interactively asks for a file name, then opens and reads the file, line by line. Each line should be echoed to the screen, with a line number and a tab (\t) preceding it. (Advanced: In addition, output the number of words in the file. A word is defined to be a contiguous sequence of non-space characters.)

Some of you have discovered that this program is in the book. That's good and bad. It's good in the sense that you've demonstrated good search skills. It's bad if you quickly ``looked up'' the answer without trying to solve the problem on your own.

Here's the strategy:

  1. Login to the Novell network. If you haven't used Windows 95 before, you'll discover that many things are ``hidden'' behind the Start button.

  2. Open a telnet session. We may have to experiment with this a bit. The host we're connecting to is If possible, use the vt100 terminal emulation.

  3. When you get the login prompt, enter your username. If you make a mistake, type ^u (hold down the ``ctrl'' key and press the ``u'' key) and start over.

  4. When you've successfully logged in a lot of unimportant messages scroll by quickly. You may see the line:
    TERM = (hp)
    Type vt100 and press enter. You should now see the shell prompt:
    [1] %
    (This is what my prompt looks like. Yours may be different.) This prompt is how the system tells you it's waiting for a command from you. How nice to be waited upon like this.

  5. Let's create a directory (folder). To do this, we use the mkdir command followed by the name of the directory which we wish to create. Type a space to separate the two. Pick any name you like for your directory. I suggest hw1 .

  6. Now, enter your directory using the cd command. Again, follow the command name with the directory name you chose.

  7. Create your perl program file. We'll use the pico command to start an editor. Follow the command name with the name of the file which you want to create. Use an extension (suffix) of .pl in your file name. The command and the file name must always be separated by a space character.

  8. Let's write the first part of this perl program. Here's a short program which tries to open a file, then print it, line by line, on the screen. Type it in, save it, and exit from pico:
    #!/usr/contrib/bin/perl -w
    # This is a small perl program which demonstrates some file I/O
    # operations:
    #    o File handles.
    #    o Opening a file for input and checking to see if it opened.
    #    o Using an opened file.
    #    o Closing a file.
    # This program interactively reads a file name and then copies the contents
    # of that file to the screen.
    print "Enter file name: ";      # Get the file name.
    $name = <STDIN>;
    if (!open(INPUT, $name))        # Try to open the file.
        die "Cannot open $name";
    print "Contents of $name:\n";
    $line = <INPUT>;
    while ($line)                   # Print the file on the screen.
        print $line;
        $line = <INPUT>;
    close(INPUT);                   # Close the file.
    exit 0;
    What's really important here is that you understand what each statement of the program does. Do you?

  9. You must now make your file executable. Use the command chmod u+x followed by your file name. Now run the command ls -l. (The period is a part of the sentence, not the command.) The beginning of the line for your file should start off with -rwx. If it doesn't, stop and call me over.

  10. Now, create a small data file. Run pico again, using a file name this time of data (don't forget the space between the two words). Just type a few lines into the file and save it. Exit from pico.

  11. Run your program. Let's say you named your program file To run your program type ./ (substitute the file name you chose). Did your program do what you expected? What happens if you type in the name of a non-existent file? Did the program do what you expected.

  12. Now, you're ready to re-edit your program in pico, adding statements to print a line number and a tab character at the beginning of each line. Do that, save your program, and re-run it.

  13. When you've finished, use the logout command to log out from unix1, then log out of Windows 95.

Thomas P. Kelliher
Tue Feb 25 15:47:44 EST 1997
Tom Kelliher