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


Chapter 4: Loops, Conditionals, and Recursion puts a lot of emphasis on the forms of branched execution, such as

Conditional Execution:

if $number > 0 {
    say '$number is positive';

Alternative Execution:

if $number % 2 == 0 {
    say 'Variable $number is even'
} else {
    say 'Variable $number is odd'

Chained Conditionals:

if $x < $y {
    say 'Variable $x is less than variable $y'
} elsif $x > $y {
    say 'Variable $x is greater than variable $y'
} else {
    say 'Variables $x and $y are equal'

Nested Conditionals:

if $x == $y {
        say 'Variables $x and $y are equal'
    else {
        if $x < $y {
          say 'Variable $x is less than variable $y'
        else {
       say 'Variable $x is greater than variable $y'
If Conditionals as Statement Modifiers:

say '$number is negative.' if $number < 0;

and Unless Conditional Statements:

unless $number >= 0 {
    say '$number is negative.'

Add the Ternary Conditional Operator, not covered until Chapter 11, to the mix and you get:  

my $result;
if $num < 10 {
    $result = "One digit";
} else {
    $result = "More than one digit";
say $result;

reworked as:

say $num < 10 ?? "One digit" !! "More than one digit";

As a sidenote, and just for fun, in idiomatic Perl 5 this could be also be done with the do BLOCK:

say $result = do {
if ($num < 10) {
       "One digit";
    else if ($num > 10 and $num <100) {
       "More than one digit";
         "More than two digits";

Chapter 5: Fruitful Subroutines is just about functions with a return value instead of void which we had so far.The chapter cumulatively draws upon the concepts talked about in the previous chapters, and utilizes them in walking through the code of implementing a factorial function, enriching the example with type checking and multi subroutines as it progresses.It's material that requires slight exposure to  maths and typically projected to freshmen in CS.

Chapter 6: Iteration is about local variables and variable scoping, updating variables inside 'while' loops and controlling the loop with 'last', 'next', and 'next unless'.Everything is put together in an example calculating Square Roots.
In Chapter 7: Strings we are told that:

"A string is primarily a piece of textual data, but it is technically an ordered sequence of characters."

and are given operations that treat them as a such:

> my $string = "banana";

> my $st = $string.comb;
(b a n a n a)

> say $st[1];

> say $st[2];

> say $st[2..5]
(n a n a)

> say "banana".chars;

> say index "banana", "na";

> say rindex "banana", "na";

> say substr "I have a dream", 0, 6;
I have

> say "I have a dream".substr(9, 5)

The String class has a lot of utility methods so that you don't have to rely to regular expressions for simple cases of string manipulation as "Splitting on Words" with the 'words' function demonstrates:

> say "I have a dream".words.perl;
("I", "have", "a", "dream").Seq

> .say for "I have a dream".words;

which play along nicely with:
say 'I have a dream'.words.join('|');
# -> I|have|a|dream

say join ";", words "I have a dream";
# -> I;have;a;dream

throwing lambda expressions into the mix:

my $fruit = "banana";
for $fruit.comb -> $letter {
    say $letter

All that serves as a setup to the most exciting part of the language, Regular Expressions.Delicate material that is not easy for the uninitiated to decipher :

> my $string = "yellow submarine";
yellow submarine

> say ~$0 if $string ~~ / l (.) w .*? rin /;

There's a lot of theory behind Regular expressions;explaining capture, backtracking, substitutions, lookaheads, lookbehinds, global matching, non capturing parentheses and more, but the book, which postpones diving deep until the later dedicated chapter, does a decent job in going through the basics:

say ~$/ if "abcdef" ~~ / bc.e /;
# -> bcde

"The matching process might be described as follows (but please note that this is a rough simplification): look in the string (from left to right) for a character matching the first atom (i.e., the first matchable item) of the pattern; when found, see whether the second character can match the second atom of the pattern, and so on. If the entire pattern is used, then the regex is successful. If it fails during the process, start again from the position immediately after the initial match point. (This is called backtracking). And repeat that until one of following occurs:

• There is a successful match, in which case the process ends and success is reported.
• The string has been exhausted without finding a match, in which case the regex failed."

Look-around assertions and Adverbs which modify the way the regex engine works are next.Despite being compact, the chapter covers a lot of ground and puts everything together in the concrete coding examples of "Extracting  Dates" and "Extracting an IP Address" ala Cookbook style.

A more elaborate example is presented next in Chapter 8: Case Study Word Play  which combines the various String operations to the contents of files retrieved after IO.



Last Updated ( Tuesday, 18 July 2017 )

RSS feed of book reviews only
I Programmer Book Reviews
RSS feed of all content
I Programmer Book Reviews
Copyright © 2017 All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.