Think Perl 6 |
Page 2 of 3
Chapter 4: Loops, Conditionals, and Recursion puts a lot of emphasis on the forms of branched execution, such as Conditional Execution:
Alternative Execution:
Chained Conditionals:
Nested Conditionals:
and Unless Conditional Statements:
Add the Ternary Conditional Operator, not covered until Chapter 11, to the mix and you get:
reworked as:
As a sidenote, and just for fun, in idiomatic Perl 5 this could be also be done with the do BLOCK:
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. "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:
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:
which play along nicely with:
throwing lambda expressions into the mix:
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 :
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:
"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. 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 ) |