Computer Programming (PERL)

This exercise is composed of several parts.

Click here to view exercise as a PDF instead.

The purpose of this exercise is for the student to gain familiarity with a computer program. Knowledge gained in this exercise will help the student appreciate the intricacies of designing a program, using the proper syntax for commands, and the requirement for precise entry of the code..

This exercise will provide a brief introduction to computer programming. In truth, the exercise barely scratches the surface; we will not cover software design, requirements, the coding process, testing, maintenance, best practices, etc., etc. -- that is, we skip all of the essentials of software engineering!

The intent is that you become familiar with what a computer program looks like, what a particular programming language looks like, the exactness required when entering code, and the process of making changes. The exercise is simple but will, hopefully, get the points across.

In this exercise, you will run (i.e., execute) a small program called, written in the Perl programming language. We're not going to get into any complicated semantics and syntax of the language, but enough so that you get the idea.

The program has three simple design criteria:

1. The program should display the author's name

2. The program should ask the user for two numbers, x and y

3. The program should then display x and y, as well as the sum of x and y (i.e., the value x+y).

In this exercise, you will:

1. Utilize a text editor to create a text file.

2. Prepare a text file containing a very basic Perl program.

3. Execute a Perl program, including data entry.

This exercise can be performed using any combination of local machine and/or VM. These instructions are written assuming that the exercise will be done using the Windows VM.

Whatever platform is used for the exercise must have an appropriate text editor and Perl interpreter.

Before you can run, or execute, a program, it must to be created in a form that can be read by the computer.

In this case, you will type the program below into a file called

For purposes of the exercise, it is important that you actually type it in because typographical errors -- a missed semicolon here, an additional space there -- can actually affect the output, so typing the program into a file is part of the learning experience! Each line of the program will be explained later in this exercise.

In the example shown, the file was created in the Windows VM using the Notepad++ text editor. If you use another program for data entry, be sure to save this as a text file.

Save the file on your VM's personal volume within a folder called "Exercises". If the folder does not exist, create it.

For our purposes, we edited the "" file located in the "Computer Programming" folder located in the "235 - Computers and Networks" folder. That folder is located on the Personal drive, the Corpora drive and can also be retrieved from the FTP site as well.


We are now ready to run the PERL program.

Open a command line interface and navigate to the folder where the Perl file can be found.

Test the program by typing perl as shown below.

You will be asked to supply two numbers, x and y. Type each number, followed by hitting the key and then make sure that the sum displayed is correct.


Now that you have run the program, let's look at it line by line:

Line: #!/usr/bin/perl
A comment line, not executed by the Perl interpreter (denoted by the leading #). Provides some library information to the interpreter.

Line: # Program
Another comment, this one just listing the name of the program file.

Line: $author = "Gary C. Kessler";
Variables are entities in a program that can take on a value, and are used or manipulated by the program. In Perl, variables are denoted by a name where the first character is a dollar sign ($). On this line of the program, the $author variable is assigned a character string value corresponding to the name of the program's author.

Line: print "\n$author\'s program\n";
The print command displays output to the screen. This line prints the name of the author, taken from the $author variable.

Line: print "\nEnter a number (x): ";
This line displays an instruction to the user, asking that they enter a number at the keyboard.

Line: chomp ($x = <STDIN>);
STDIN means "standard input," a reference to the keyboard. (STDOUT means "standard output," a reference to the screen.) This line says to accept input from the keyboard, assign the input value to the variable $x, and remove (i.e., chomp off) any extraneous characters at the end (such as <CR> and <LF>).

Line: print "\nEnter another number (y): ";
Same as line 5...

Line: chomp ($y = <STDIN>);
Same as line 6, except for the variable $y. (QUESTION: What would happen if we had used $x in both lines 6 and 8?)

Line: $sum = $x + $y;
Calculates the value of the variable $sum by adding $x and $y.

Line: print "\nx = $x y = $y The sum of x and y = $sum\n\n";
Displays the values of x, y, and the sum to the screen.

Your first task is to modify the program to use your name instead of "Gary C. Kessler" as the author. Update the program (i.e., edit the file) and execute it.

Your next task is to add the capability to the program to display the difference after subtracting y from x. (HINT: You need to add two lines to the end of the program that are very similar to lines 9 and 10 above. You will want a different variable name and arithmetic operation to represent the subtraction.)



Creative Commons License
CyberExplorations Exercises by Glenn S. Dardick is licensed under a Creative Commons Attribution 4.0 International License.