Previous Up Next

This HTML version of Think Perl 6 is provided for convenience, but it is not the best format of the book. You might prefer to read the PDF version.

Chapter 1  The Way of the Program

The goal of this book is to teach you to think like a computer scientist. This way of thinking combines some of the best features of mathematics, engineering, and natural science. Like mathematicians, computer scientists use formal languages to denote ideas (specifically computations). Like engineers, they design things, assembling components into systems and evaluating tradeoffs among alternatives. Like scientists, they observe the behavior of complex systems, form hypotheses, and test predictions.

The single most important skill for a computer scientist is problem solving. Problem solving means the ability to formulate problems, think creatively about solutions, and express a solution clearly and accurately. As it turns out, the process of learning to program is an excellent opportunity to practice problem-solving skills. That’s why this chapter is called, “The Way of the Program.”

On one level, you will be learning to program, a useful skill by itself. On another level, you will use programming as a means to an end. As we go along, that end will become clearer.

1.1  What is a Program?

A program is a sequence of instructions that specifies how to perform a computation. The computation might be something mathematical, such as solving a system of equations or finding the roots of a polynomial, but it can also be a symbolic computation, such as searching and replacing text in a document, or something graphical, like processing an image or playing a video.

The details look different in different languages, but a few basic instructions appear in just about every language:

Input
Get data from the keyboard, a file, the network, a sensor, a GPS chip or some other device.
Output
Display data on the screen, save it in a file, send it over the network, act on a mechanical device, etc.
Math
Perform basic mathematical operations like addition and multiplication.
Conditional execution
Check for certain conditions and run the appropriate code.
Repetition
Perform some action repeatedly, usually with some variation.

Believe it or not, that’s pretty much all there is to it. Every program you’ve ever used, no matter how complicated, is made up of instructions that look pretty much like these. So you can think of programming as the process of breaking a large, complex task into smaller and smaller subtasks until the subtasks are simple enough to be performed with one of these basic instructions.

Using or calling these subtasks makes it possible to create various levels of abstraction. You have probably been told that computers only use 0’s and 1’s at the lowest level; but we usually don’t have to worry about that. When we use a word processor to write a letter or a report, we are interested in files containing text and some formatting instructions, and with commands to change the file or to print it; fortunately, we don’t have to care about the underlying 0’s and 1’s; the word-processing program offers us a much higher view (files, commands, etc.) that hides the gory underlying details.

Similarly, when we write a program, we usually use and/or create several layers of abstraction, so that, for example, once we have created a subtask that queries a database and stores the relevant data in memory, we no longer have to worry about the technical details of the subtask. We can use it as a sort of black box that will perform the desired operation for us. The essence of programming is to a very large extent this art of creating these successive layers of abstraction so that performing the higher level tasks becomes relatively easy.

1.2  Running Perl 6

One of the challenges of getting started with Perl 6 is that you might have to install Perl 6 and related software on your computer. If you are familiar with your operating system, and especially if you are comfortable with the shell or command-line interface, you will have no trouble installing Perl 6. But for beginners, it can be painful to learn about system administration and programming at the same time.

To avoid that problem, you can start out running Perl 6 in a web browser. You might want to use a search engine to find such a site. Currently, the easiest is probably to connect to the https://glot.io/new/perl6 site, where you can type some Perl 6 code in the main window, run it, and see the result in the output window below.

Sooner or later, however, you will really need to install Perl 6 on your computer.

The easiest way to install Perl 6 on your system is to download Rakudo Star (a distribution of Perl 6 that contains the Rakudo Perl 6 compiler, documentation and useful modules): follow the instructions for your operating system at http://rakudo.org/how-to-get-rakudo/ and at https://perl6.org/downloads/.

As of this writing, the most recent specification of the language is Perl 6 version 6c (v6.c), and the most recent release available for download is Rakudo Star 2016.07; the examples in this book should all run with this version. You can find out the installed version by issuing the following command at the operating system prompt:

$ perl6 -v
This is Rakudo version 2016.07.1 built on MoarVM version 2016.07
implementing Perl 6.c.

However, you should probably download and install the most recent version you can find. The output (warnings, error messages, etc.) you’ll get from your version of Perl might in some cases slightly differ from what is printed in this book, but these possible differences should essentially be only cosmetic.

Compared to Perl 5, Perl 6 is not just a new version of Perl. It is more like the new little sister of Perl 5. It does not aim at replacing Perl 5. Perl 6 is really a new programming language, with a syntax that is similar to earlier versions of Perl (such as Perl 5), but still markedly different. Unless stated otherwise, this book is about Perl 6 only, not about Perl 5 and preceding versions of the Perl programming language. From now on, whenever we speak about Perl with no further qualification, we mean Perl 6.

The Perl 6 interpreter is a program that reads and executes Perl 6 code. It is sometimes called REPL (for “read, evaluate, print, loop”). Depending on your environment, you might start the interpreter by clicking on an icon, or by typing perl6 on a command line.

When it starts, you should see output like this:

To exit type 'exit' or '^D'
(Possibly some information about Perl and related software)
> 

The last line with > is a prompt that indicates that the REPL is ready for you to enter code. If you type a line of code and hit Enter, the interpreter displays the result:

> 1 + 1
2
>

You can type exit at the REPL prompt to exit the REPL.

Now you’re ready to get started. From here on, we assume that you know how to start the Perl 6 REPL and run code.

1.3  The First Program

Traditionally, the first program you write in a new language is called “Hello, World” because all it does is display the words “Hello, World.” In Perl 6, it looks like this:

> say "Hello, World";
Hello, World
>

This is an example of what is usually called a print statement, although it doesn’t actually print anything on paper and doesn’t even use the print keyword 1 (keywords are words which have a special meaning to the language and are used by the interpreter to recognize the structure of the program). The print statement displays a result on the screen. In this case, the result is the words Hello, World. The quotation marks in the program indicate the beginning and end of the text to be displayed; they don’t appear in the result.

The semi-colon “;” at the end of the line indicates that this is the end of the current statement. Although a semi-colon is technically not needed when running simple code directly under the REPL, it is usually necessary when writing a program with several lines of code, so you might as well just get into the habit of ending code instructions with a semi-colon.

Many other programming languages would require parentheses around the sentence to be displayed, but this is usually not necessary in Perl 6.

1.4  Arithmetic Operators

After “Hello, World,” the next step is arithmetic. Perl 6 provides operators, which are special symbols that represent computations like addition and multiplication.

The operators +, -, *, and / perform addition, subtraction, multiplication and division, as in the following examples under the REPL:

> 40 + 2
42
> 43 - 1
42
> 6 * 7
42
> 84 / 2
42

Since we use the REPL, we don’t need an explicit print statement in these examples, as the REPL automatically prints out the result of the statements for us. In a real program, you would need a print statement to display the result, as we’ll see later. Similarly, if you run Perl statements in the web browser mentioned in Section ??, you will need a print statement to display the result of these operations. For example:

say 40 + 2;   # -> 42

Finally, the operator ** performs exponentiation; that is, it raises a number to a power:

> 6**2 + 6
42

In some other languages, the caret (“^”) or circumflex accent is used for exponentiation, but in Perl 6 it is used for some other purposes.

1.5  Values and Types

A value is one of the basic things a program works with, like a letter or a number. Some values we have seen so far are 2, 42, and "Hello, World".

These values belong to different types: 2 is an integer, 40 + 2 is also an integer, 84/2 is a rational number, and 'Hello, World' is a string, so called because the characters it contains are strung together.

If you are not sure what type a value has, Perl can tell you:

> say 42.WHAT;
(Int)
> say (40 + 2).WHAT;
(Int)
> say (84 / 2).WHAT;
(Rat)
> say (42.0).WHAT
(Rat)
> say ("Hello, World").WHAT;
(Str)
>

In these instructions, .WHAT is known as an introspection method, that is a kind of method which will tell you what (of which type) the preceding expression is. 42.WHAT is an example of the dot syntax used for method invocation: it calls the .WHAT built-in on the “42” expression (the invocant) and provides to the say function the result of this invocation, which in this case is the type of the expression.

Not surprisingly, integers belong to the type Int, strings belong to Str and rational numbers belong to Rat.

Although 40 + 2 and 84 / 2 seem to yield the same result (42), the first expression returns an integer (Int) and the second a rational number (Rat). The number 42.0 is also a rational.

The rational type is somewhat uncommon in most programming languages. Internally, these numbers are stored as two integers representing the numerator and the denominator (in their simplest terms). For example, the number 17.3 might be stored as two integers, 173 and 10, meaning that Perl is really storing something meaning the 173/10 fraction. Although this is usually not needed (except for introspection or debugging), you might access these two integers with the following methods:

> my $num = 17.3;
17.3
> say $num.WHAT;
(Rat)
> say $num.numerator, " ", $num.denominator; # say can print a list
173 10
> say $num.nude;      # "nude" stands for numerator-denominator
(173 10) 

This may seem anecdotal, but, for reasons which are beyond the scope of this book, this makes it possible for Perl 6 to perform arithmetical operations on rational numbers with a much higher accuracy than most common programming languages. For example, if you try to perform the arithmetical operation 0.3 - 0.2 - 0.1, with most general purpose programming languages (and depending on your machine architecture), you might obtain a result such as -2.77555756156289e-17 (in Perl 5), -2.775558e-17 (in C under gcc), or -2.7755575615628914e-17 (Java, Python 3, Ruby, TCL). Don’t worry about these values if you don’t understand them, let us just say that they are extremely small, but they are not 0, whereas, obviously, the result should really be zero. In Perl 6, the result is 0 (even to the fiftieth decimal digit):

> my $result-should-be-zero = 0.3 - 0.2 - 0.1;
0
> printf "%.50f", $result-should-be-zero; # prints 50 decimal digits
0.00000000000000000000000000000000000000000000000000

In Perl 6, you might even compare the result of the operation with 0:

> say $result-should-be-zero == 0;
True

Don’t do such a comparison with most common programming languages; you’re very likely to get a wrong result.

What about values like "2" and "42.0"? They look like numbers, but they are in quotation marks like strings.

> say '2'.perl; # perl returns a Perlish representation of the invocant
"2"
> say "2".WHAT;
(Str)
> say '42'.WHAT;
(Str)

They’re strings because they are defined within quotes. Although Perl will often perform the necessary conversions for you, it is generally a good practice not to use quotation marks if your value is intended to be a number.

When you type a large integer, you might be tempted to use commas between groups of digits, as in 1,234,567. This is not a legal integer in Perl 6, but it is a legal expression:

> 1,234,567
(1 234 567)
>

That’s actually a list of three different integer numbers, and not what we expected at all!

> say (1,234,567).WHAT
(List)

Perl 6 interprets 1,234,567 as a comma-separated sequence of three integers. As we will see later, the comma is a separator used for constructing lists.

You can, however, separate groups of digits with the underscore character “_” for better legibility and obtain a proper integer:

> 1_234_567
1234567
> say 1_234_567.WHAT
(Int)
>

1.6  Formal and Natural Languages

Natural languages are the languages people speak, such as English, Spanish, and French. They were not designed by people (although people try to impose some order on them); they evolved naturally.

Formal languages are languages that are designed by people for specific applications. For example, the notation that mathematicians use is a formal language that is particularly good at denoting relationships among numbers and symbols. Chemists use a formal language to represent the chemical structure of molecules. And most importantly:

Programming languages are formal languages that have been designed to express computations.

Formal languages tend to have strict syntax rules that govern the structure of statements. For example, in mathematics the statement 3 + 3 = 6 has correct syntax, but not 3 + = 3 $ 6. In chemistry H2O is a syntactically correct formula, but 2Zz is not.

Syntax rules come in two flavors, pertaining to tokens and structure. Tokens are the basic elements of the language, such as words, numbers, and chemical elements. One of the problems with 3 += 3 $ 6 is that $ is not a legal token in mathematics (at least as far as I know). Similarly, 2Zz is not legal because there is no chemical element with the abbreviation Zz.

The second type of syntax rule, structure, pertains to the way tokens are combined. The equation 3 += 3 is illegal in mathematics because even though + and = are legal tokens, you can’t have one right after the other. Similarly, in a chemical formula, the subscript representing the number of atoms in a chemical compound comes after the element name, not before.

This is @ well-structured Engli$h sentence with invalid t*kens in it. This sentence all valid tokens has, but invalid structure with.

When you read a sentence in English or a statement in a formal language, you have to figure out the structure (although in a natural language you do this subconsciously). This process is called parsing.

Although formal and natural languages have many features in common—tokens, structure, and syntax—there are some differences:

Ambiguity
Natural languages are full of ambiguity, which people deal with by using contextual clues and other information. Formal languages are designed to be nearly or completely unambiguous, which means that any statement has exactly one meaning.
Redundancy
In order to make up for ambiguity and reduce misunderstandings, natural languages employ lots of redundancy. As a result, they are often verbose. Formal languages are less redundant and more concise.
Literalness
Natural languages are full of idiom and metaphor. If we say, “The penny dropped,” there is probably no penny and nothing dropping (this idiom means that someone understood something after a period of confusion). Formal languages mean exactly what they say.

Because we all grow up speaking natural languages, it is sometimes hard to adjust to formal languages. The difference between formal and natural language is like the difference between poetry and prose, but more so:

Poetry
Words are used for their sounds as well as for their meaning, and the whole poem together creates an effect or emotional response. Ambiguity is not only common but often deliberate.
Prose
The literal meaning of words is more important, and the structure contributes more meaning. Prose is more amenable to analysis than poetry but still often ambiguous.
Programs
The meaning of a computer program is unambiguous and literal, and can be understood entirely by analysis of the tokens and structure.

Formal languages are more dense than natural languages, so it takes longer to read them. Also, the structure is important, so it is not always best to read from top to bottom, left to right. Instead, learn to parse the program in your head, identifying the tokens and interpreting the structure. Finally, the details matter. Small errors in spelling and punctuation, which you can get away with in natural languages, can make a big difference in a formal language.

1.7  Debugging

Programmers make mistakes. Programming errors are usually called bugs and the process of tracking them down is called debugging.

Programming, and especially debugging, sometimes brings out strong emotions. If you are struggling with a difficult bug, you might feel angry, despondent, or embarrassed.

There is evidence that people naturally respond to computers as if they were people. When they work well, we think of them as teammates, and when they are obstinate or rude, we respond to them the same way we respond to rude, obstinate people2.

Preparing for these reactions might help you deal with them. One approach is to think of the computer as an employee with certain strengths, like speed and precision, and particular weaknesses, like lack of empathy and inability to grasp the big picture.

Your job is to be a good manager: find ways to take advantage of the strengths and mitigate the weaknesses. And find ways to use your emotions to engage with the problem, without letting your reactions interfere with your ability to work effectively.

Learning to debug can be frustrating, but it is a valuable skill that is useful for many activities beyond programming. At the end of each chapter there is a section, like this one, with our suggestions for debugging. I hope they help!

1.8  Glossary

Problem solving
The process of formulating a problem, finding a solution, and expressing it.
Abstraction
A way of providing a high-level view of a task and hiding the underlying technical details so that this task becomes easy.
Interpreter
A program that reads another program and executes it
Compiler
A program that reads another program and transforms it into executable computer code; there used to be a strong difference between interpreted and compiled languages, but this distinction has become blurred over the last two decades or so.
Prompt
Characters displayed by the interpreter to indicate that it is ready to take input from the user.
Program
A set of instructions that specifies a computation.
Print statement
An instruction that causes the Perl 6 interpreter to display a value on the screen.
Operator
A special symbol that represents a simple computation like addition, multiplication, or string concatenation.
Value
One of the basic units of data, like a number or string, that a program manipulates.
Type
A category of values. The types we have seen so far are integers (type Int), rational numbers (type Rat), and strings (type Str).
Integer
A type that represents whole numbers.
Rational
A type that represents numbers with fractional parts. Internally, Perl stores a rational as two integers representing respectively the numerator and the denominator of the fractional number.
String
A type that represents sequences of characters.
Natural language
Any one of the languages that people speak that evolved naturally.
Formal language
Any one of the languages that people have designed for specific purposes, such as representing mathematical ideas or computer programs; all programming languages are formal languages.
Token
One of the basic elements of the syntactic structure of a program, analogous to a word in a natural language.
Syntax
The rules that govern the structure of a program.
Parse
To examine a program and analyze the syntactic structure.
Bug
An error in a program.
Debugging
The process of finding and correcting bugs.

1.9  Exercises

Exercise 1  

It is a good idea to read this book in front of a computer so you can try out the examples as you go.

Whenever you are experimenting with a new feature, you should try to make mistakes. For example, in the “Hello, world!” program, what happens if you leave out one of the quotation marks? What if you leave out both? What if you spell say wrong?

This kind of experiment helps you remember what you read; it also helps when you are programming, because you get to know what the error messages mean. It is better to make mistakes now and on purpose than later and accidentally.

Please note that most exercises in this book are provided with a solution in the appendix. However, the exercises in this chapter and in the next chapter are not intended to let you solve an actual problem but are designed to simply let you experiment with the Perl interpreter; there is no good solution, just try out what is proposed to get a feeling on how it works.

  1. If you are trying to print a string, what happens if you leave out one of the quotation marks, or both?
  2. You can use a minus sign to make a negative number like -2. What happens if you put a plus sign before a number? What about 2++2?
  3. In math notation, leading zeros are OK, as in 02. What happens if you try this in Perl?
  4. What happens if you have two values with no operator between them, such as say 2 2;?
Exercise 2  

Start the Perl 6 REPL interpreter and use it as a calculator.

  1. How many seconds are there in 42 minutes, 42 seconds?
  2. How many miles are there in 10 kilometers? Hint: there are 1.61 kilometers in a mile.
  3. If you run a 10 kilometer race in 42 minutes, 42 seconds, what is your average pace (time per mile in minutes and seconds)? What is your average speed in miles per hour?


1
Perl also has a print function, but the say built-in function is used here because it adds a new line character to the output.
2
Reeves and Nass, The Media Equation: How People Treat Computers, Television, and New Media Like Real People and Places, (Center for the Study of Language and Information, 2003).)

Are you using one of our books in a class?

We'd like to know about it. Please consider filling out this short survey.


Think DSP

Think Java

Think Bayes

Think Python 2e

Think Stats 2e

Think Complexity


Previous Up Next