Think Perl 6
Article Index
Think Perl 6
Chapters 4 to 8

Author: Allen Downey, Laurent Rosenfeld
Publisher: O'Reilly
ISBN: 978-1491980552
Print: 1491980559
Kindle: B0716P9W11
Audience: Beginners to programming
Rating: 5:
Reviewer: Nikos Vaggalis 

Will this book really teach you how to think like a Computer Scientist?

This title comes amid a shower of announcements of forthcoming Perl 6 books, therefore I initially thought  that so many releases at the same time could potentially disorient the aspiring reader. Fortunately it looks more like that each title addresses different kinds of audiences and at I Programmer we aims to find more about what's in it for each audience.

Think Perl 6 is targeting the uninitiated in programming in general, as such it goes over the pure basics and the raw elements that make up programming as a science.

That said, I don't have the answer as to why someone would be compelled to kick off his exploration of Computer Science with Perl 6 as his first language when there are so many, popular and well tried alternatives such as Python, JavaScript or Java. That person would be someone already versed in some computing, more like a teacher or mentor who finds the idea of tackling Perl 6 rather exciting, and rightly so, as 6 is a post-modern programming language that adopts the latest, state of the art advancements in the world of programming language design.

Another candidate that I can imagine, would be a scientist in a field like Physics or Data Science looking for a tool to make his job easier, or, a high school graduate looking to bootstrap his journey to the world of programming, more likely being recommended Perl 6 by someone more knowledgeable than make that choice by himself.

And, certainly 6 ought to be particularly attractive to Perl 5 hackers eager to see through the hype and discover what Perl's version 6 has in stock for them despite the fact that apart from some similarities  in symbols and syntax, everything else differs. As Larry Wall once said Perl 6 would be:

"Perl 5 done right".

What's been carried over unaltered though is Perl 5's philosophy and innovative thinking.




In Chapter 1: The Way of the Program, the first step the authors provide guidance with is installing the compiler kit and setting up the programming environment, two actions which always pose barriers to getting into programming. One easy way out of this is by calling into the simplified environment offered by the Web IDE's."Type some code in the main window, run it, and see the result in the output window below". Fortunately Perl 6 is one of the languages supported by Sooner or later however, installing Perl 6 on your computer is inevitable. Leveraging this installation procedure and going through the usual "Hello World" example, the book promptly introduces the notion of the interpreter as a program that reads and executes Perl 6 code: 

"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."

This paragraph is relayed here 'as-is' as a representative sample of the approach the book is embracing; decomposing everything to the elementary level.

The merits of Perl 6 are shyly making their début, starting with  the language's internal value representation system:

"you might access these two integers with the following methods:

> my $num = 17.3;

> say $num.WHAT;

> 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)"

At its end, the chapter provides a nice breakdown of parsing and debugging, and relays a list of the concepts gone over thus far, which are further elaborated in the rest of the book.



Chapter 2: Variables, Expressions and Statements involves topics like naming, declaring and assigning  variables, with more elaborate terms like 'lexical' variables briefly mentioned and explained later on.
After that, a good amount of material on expressions and statements, operators and operands coercion of strings into numbers, runtime vs semantic errors and more get covered in just a few pages, when in other books the same would span several chapters. If you've read Chromatic's Modern Perl and liked its style you'll find yourself in familiar waters.

In Chapter 3: Functions we find yet another example of Perl 6 differentiating from Perl 5, in embracing the 'everything is an object' Ruby-like syntax such as '42.say' and '42.WHAT.say'. Functional syntax is not forgotten though as 'say 42', 'say WHAT 42' demonstrate.

Moving from the built-in subroutines or functions to user-defined ones, we get acquainted with the 'sub' keyword and the statements it encloses within its brackets.The topic of function parameters is also briefly explained although not against its relation to 'types', as of yet.The "Flow of Execution" and its associated "Stack diagrams" of the stack and its frames, once more re-affirm that this book is about Computer Science using a programming language as a vehicle and not about the programming language itself. The Stack paradigm is further exemplified in Chapter 4 upon tackling Recursion.

Resuming at defining "function signatures", the chapter goes over function parameters again but this time calls their 'types' into play to define method signatures and type constraints.That's yet another difference from ancestor Perl 5.In 6 we have a well defined hierarchical type system as in 'Int, Real, Numeric'  whereas in Perl 5 every number is represented as a double:

"You can use the Real or Numeric types to make the function more general (the difference between the two types is that the Numeric type will accept not only Real but also Complex numbers)"

Last Updated ( Tuesday, 18 July 2017 )