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.”
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:
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.
To exit type 'exit' or '^D' (Possibly some information about Perl and related software) >
> 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
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,
These values belong to different types:
2 is an integer, 40 + 2 is also an integer,
84/2 is a rational number,
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
> 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.
> 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)
> 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.
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:
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.
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!
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.
Start the Perl 6 REPL interpreter and use it as a calculator.
Are you using one of our books in a class?We'd like to know about it. Please consider filling out this short survey.