Learning How to Program in Perl

Dr. David J. Ritchie, Mr. Jim Skrine, Mr. Bruce Webber

Washington Junior High Computer Club

April 15, 1998

Summary

This is the second of several handouts for a short course in learning how to program in a computer language called "perl".

1.0 Introduction

This is the second of several handouts for a short course in learning how to program in a computer language called "perl".

This handout will cover features of the language in Parts 1 and 2. The treatment will be very schematic because it is assumed that you are trying out the features as you go along.

Perl itself will be your teacher. For that reason, I am trying not to get bogged down in the details. The material below will be approximately correct but may be slightly incorrect on the details. It is better that we get on with trying to learn the language and over time we will become familiar with the details.

In Part 3, it will cover the beginnings of our trying to understand how to write the "hangman spelling game" program.

2.0 Language Features - Part 1

This first part focuses on single elements, single things. For this reason, the word " scalar" is used.

2.1 Scalar Data

Scalar data are numbers or strings of characters where the number or the string of characters is considered to be one thing.

Examples of four pieces of scaler data are: 1, 2, 3.14, abcd

2.2 Numbers

Numbers are a type of scaler data. They may be integers (that is, whole numbers) like 1, 2, 3, 73. They may be real numbers like 7.23, 2.3, 1.4.

Perl treats them all as if they are double precision real numbers so that even if you write an integer like 5 internally perl will be treating it as if it is 5.000000...

When you enter a number, you need a convention for how a number is written. This is called a literal. Sometimes it seems so obvious that you don't even realize you are using a convention. You have seen something of the convention for numbers above.

To write 1 Million, you write 1e6. To write 1 millionth, you write 1e-6. The e stands for "exponent" to which 10 must be raised. So 1e6 is 1*10**6 to use a different literal notation. What is -1.25e6? What is -3.1e-6?

Similarly, integers are written as literals in the way that you have been taught: 3, -3, 4, 20, -2000. These are all examples of integer literals.

What about octal and hexadecimal numbers? The literal format for an octal number is a number that starts with a leading zero. That is, 0777 is an octal number. Similarly, -0377 is an octal number.

Hexadecimal literal format is designated as 0x33 or 0X33. What would 0xf be if it were converted to a decimal format?

2.3 Strings

A string is a sequence of characters. Since we are considering scalers, we are still considering the string of characters as a single "thing". A string can have any number from 0 characters to as many as will fit in the memory of your computer. Internally, strings are considered as 8 bits.

What about literal representations of strings? The single or double quotes are used. A string is represented literally as `abcd' or "abcd". To make a literal string which has a quote in it, we quote the quote: `abc''d' is a string with the three characters a, b, and c, followed by the character ` followed by the character d. You can also use the forward slash (/) to quote the quote. That is, `abc''d' could also be represented as `abc/'d'.

The double quotes act in similar fashion but they allow the computer to substitute certain things when it sees them. For example "abc/td" would be a string with the characters a, b, and c. Then, the string would contain a tab followed by a d.

2.4 Scalar Operators

It is all well and good to be able to specify numbers and strings but one would like to be able to do something with them. For example, one might like to add two numbers together or to join together two strings.

Scalar operators are the means to do this. Some are so obvious it is hard to understand that an operator is being defined. For example, to add the literal 5 to the literal 6, you write 5 + 6! Duh! Similarly obvious things apply for subtraction, multiplication, division, and so on.

Now, how do you join together string `abcd' and string `xyz'? You use the ".". That is, you use the period. It is `abcd'.'xyz' and that represents the string `abcdxyz'.

But, wait, I want to do more than write literals! How do I do that? That's next!

2.5 Scalar Variables

A variable is a container. A scaler variable is a container that holds "one thing". For example, it could hold one number--of any magnitude (up to the limit on representing a number in double precision format)--or one string--of any length (up to available memory).

A scalar container is a symbol that begins with a $ followed by a letter and then by any number of letters, numbers, or underscores (up to 256 total). So a scalar variable is $a, $fred, $sd203 but not $203!

You can store any of the previously discussed scalars in such a variable. Once you have done that, you can do various operations.

Some examples:

Assignment:

$a = 17 ; # assigns the literal 17 to the scalar variable $a.
$b = $a + 5 ; # assigns the sum of the value in $a and the literal 5 to $b
$a += 5 ; # which adds 5 to the value in $a and assigns the result to $a.

Note that case matters. $A is different from $a.

2.6 Input

The simplest way to obtain input from the keyboard is to use <STDIN> any place you would use a scalar variable. When you do that, the next complete line from the keyboard is taken as the value of what would have been the scalar variable in that spot.

Some examples:

$a = <STDIN> ; # puts the next line of input into the variable $a
$b = $a + <STDIN> ; # adds the input to the contents of $a and assigns it to $b

2.7 Output

The simplest way to generate output is to use the print command.

Some examples:

print ("Your result is $a"); # what will be printed if the value of $a is 17?
print "Your result is $a";

3.0 Language Features - Part 2

This second part focuses on collections of elements. For this reason, the word "list" is used. Lists are stored in arrays.

3.1 List Data

List data are are ordered collections of numbers or strings of characters.

One example of list data is: 1, 2, 3.14, abcd

3.2 Lists

Lists can contain both numbers and strings as the above example shows.

When you enter a list, you need a convention for how the list is written. This is called a literal. You have seen an informal convention in the example above.

The following are examples of literal lists and their meaning:

(1, 2, 3) - the list of the numbers 1, 2, and 3.
("fred", 4.1, 5) - the list of the string "fred", the number 4.1 and the number 5.
($a, $b) - the list of the values contained in the scalar variables $a and $b

() - the null list (that is, the list with no elements).

(1 .. 5) - the list 1, 2, 3, 4, 5

3.3 List Operators

There are of course operators for lists. In particular, there is the assignment operator.

3.4 Array Variables

Now we need a place to store lists. Those places are array variables. An array variable is a symbol that begins with @ and some letter and is followed by letters, numbers, or underscore.

Some examples:

@a - the array variable @a

@another - the array variable @another

@a = (1, 2, 3) ; # stores the list 1, 2, 3 into the array variable @a.

Note that @a is different from $a.

3.5 Accessing Arrays

Once one has arrays one would like to access them in various ways.

Examples:

@fred = (7, 8, 9) ; # puts the lists 7, 8, and 9 into the array variable @fred.
$b = $fred[0] ; # puts the first element of the list stored in @fred into $b.
$c = $fred[-1]; # puts the last element of the list stored in @fred into $c.


3.6 Functions

Finally, there are some useful functions that are defined for arrays.

Examples:

push (@fred, 10) ; # adds the scalar literal 10 to the end of the list @fred
pop (@fred) ; # removes the last element of the list stored in @fred.
unshift(@fred, 10) ; # adds the scalar literal 10 to the beginning of the list @fred
shift(@fred) ; # removes the first element of the list stored in @fred.
reverse (@fred) ; # try this. What does it do?
sort (@fred) ; # try this. What does it do?
chomp(@fred) ; # how about this?

The Hangman Spelling Game

This third part focuses on trying to begin using perl to program the Hangman Spelling Game.

4.1 Analyzing the Game

First, we have to analyze (figure out) how people play the game. We have to do this in as mechanical a fashion as possible so that it is easy to translate then from the people version to the computer version.

So, choose a partner and play the spelling game "Hangman" but write down each and every step you do.

4.2 Using the Results of your Analysis

With the results of your analysis in hand, try to identify the things that store data in the game.

Then, try to identify the operations that are done to that data.

5.0 Next Week

Next week, we will continue learning about perl techniques and trying to program the "Hangman" game.