Think Perl 6 - Green Tea Press

2y ago
18 Views
2 Downloads
1.30 MB
431 Pages
Last View : 1m ago
Last Download : 3m ago
Upload by : Ronan Orellana
Transcription

Think Perl 6How to Think Like a Computer Scientist1st Edition, Version 0.5.0May 2017

Think Perl 6How to Think Like a Computer Scientist1st Edition, Version 0.5.0May 2017Laurent Rosenfeld, with Allen B. DowneyGreen Tea PressNeedham, Massachusetts

Copyright 2017 Allen Downey, Laurent Rosenfeld.Green Tea Press9 Washburn AveNeedham MA 02492Permission is granted to copy, distribute, and/or modify this document under the terms of theCreative Commons Attribution-NonCommercial 3.0 Unported License, which is available at http://creativecommons.org/licenses/by-nc/3.0/.The original form of this book is LATEX source code. Compiling this LATEX source has the effect of generating a device-independent representation of a textbook, which can be converted to other formatsand printed.The LATEX source for this book is available from https://github.com/LaurentRosenfeld/thinkperl6/

PrefaceWelcome to the art of computer programming and to the new Perl 6 language. This willprobably be the first published books using Perl 6 (or one of the first), a powerful, expressive, malleable and highly extensible programming language. But this book is less aboutPerl 6, and more about learning how to write programs for computers.This book is intended for beginners and does not require any prior programming knowledge, but it is my hope that even those of you with programming experience will benefitfrom reading it.The Aim of this BookThis aim of this book is not primarily to teach Perl 6, but instead to teach the art of programming, using the Perl 6 language. After having completed this book, you should hopefullybe able to write programs to solve relatively difficult problems in Perl 6, but my main aim isto teach computer science, software programming, and problem solving rather than solelyto teach the Perl 6 language itself.This means that I will not cover every aspect of Perl 6, but only a (relatively large, butyet incomplete) subset of it. By no means is this book intended to be a reference on thelanguage.It is not possible to learn programming or to learn a new programming language by justreading a book; practicing is essential. This book contains a lot of exercises. You arestrongly encouraged to make a real effort to do them. And, whether successful or notin solving the exercises, you should take a look at the solutions in the Appendix, as, veryoften, several solutions are suggested with further discussion on the subject and the issuesinvolved. Sometimes, the solution section of the Appendix also introduces examples oftopics that will be covered in the next chapter–and sometimes even things that are not covered elsewhere in the book. So, to get the most out the book, I suggest you try to solve theexercises as well as review the solutions and attempt them.There are more than one thousand code examples in this book; study them, make sure tounderstand them, and run them. When possible, try to change them and see what happens.You’re likely to learn a lot from this process.

viChapter 0. PrefaceThe History of this BookIn the course of the last three to four years, I have translated or adapted to French a numberof tutorials and articles on Perl 6, and I’ve also written a few entirely new ones in French. 1Together, these documents represented by the end of 2015 somewhere between 250 and300 pages of material on Perl 6. By that time, I had probably made public more material onPerl 6 in French than all other authors taken together.In late 2015, I began to feel that a Perl 6 document for beginners was something missingthat I was willing to undertake. I looked around and found that it did not seem to existin English either. I came to the idea that, after all, it might be more useful to write such adocument initially in English, to give it a broader audience. I started contemplating writinga beginner introduction to Perl 6 programming. My idea at the time was something like a50- to 70-page tutorial and I started to gather material and ideas in this direction.Then, something happened that changed my plans.In December 2015, friends of mine were contemplating translating into French Allen B.Downey’s Think Python, Second Edition2 . I had read an earlier edition of that book and fullysupported the idea of translating it3 . As it turned out, I ended up being a co-translator andthe technical editor of the French translation of that book4 .While working on the French translation of Allen’s Python book, the idea came to methat, rather than writing a tutorial on Perl 6, it might be more useful to make a “Perl 6translation” of Think Python. Since I was in contact with Allen in the context of the Frenchtranslation, I suggested this to Allen, who warmly welcomed the idea. This is how I startedto write this book late January 2016, just after having completed the work on the Frenchtranslation of his Python book.This book is thus largely derived on Allen’s Think Python, but adapted to Perl 6. As ithappened, it is also much more than just a “Perl 6 translation” of Allen’s book: with quitea lot of new material, it has become a brand new book, largely indebted to Allen’s book,but yet a new book for which I take all responsibility. Any errors are mine, not Allen’s.My hope is that this will be useful to the Perl 6 community, and more broadly to the opensource and general computer programming communities. In an interview with LinuxVoice(July 2015), Larry Wall, the creator of Perl 6, said: “We do think that Perl 6 will be learnableas a first language.” Hopefully this book will contribute to making this happen.AcknowledgmentsI just don’t know how I could thank Larry Wall to the level of gratitude that he deservesfor having created Perl in the first place, and Perl 6 more recently. Be blessed for eternity,Larry, for all of that.And thank to you all of you who took part to this adventure (in no particular order), Tom,Damian, chromatic, Nathan, brian, Jan, Jarkko, John, Johan, Randall, Mark Jason, Ovid,1 See2 Seefor example ttp://greenteapress.com/wp/think-python-2e/.3 I know, it’s about Python, not Perl. But I don’t believe in engaging in “language wars” and think that we allhave to learn from other languages; to me, Perl’s motto, “there is more than one way to do it,” also means thatdoing it in Python (or some other language) is truly an acceptable possibility.4 See ensez-python/.

viiNick, Tim, Andy, Chip, Matt, Michael, Tatsuhiko, Dave, Rafael, Chris, Stevan, Saraty, Malcolm, Graham, Leon, Ricardo, Gurusamy, Scott and too many others to name.All my thanks also to those who believed in this Perl 6 project and made it happen, including those who quit at one point or another but contributed for some time; I know that thiswasn’t always easy.Many thanks to Allen Downey, who very kindly supported my idea of adapting his bookto Perl 6 and helped me in many respects, but also refrained from interfering in what I wasputting into this new book.I very warmly thank the people at O’Reilly who accepted the idea of this book and suggested many corrections or improvements. I want to thank especially Dawn Schanafelt, myeditor at O’Reilly, whose advice has truly contributed to making this a better book. Manythanks also to Charles Roumeliotis, the copy editor, and Kristen Brown, the productioneditor, who fixed many typographical problems and spelling mistakes.Thanks a lot in advance to readers who will offer comments or submit suggestions or corrections, as well as encouragements.If you see anything that needs to be corrected or that could be improved, please kindlysend your comments to think.perl6(at)gmail.com.Contributor ListI would like to thank especially Moritz Lenz and Elizabeth Mattijsen, who reviewed in detail drafts of this book and suggested quite a number of improvements and corrections. Lizspent a lot of time on a detailed review of the full content of this book and I am especiallygrateful to her for her numerous and very useful comments. Thanks also to Timo Paulssenand ryanschoppe who also reviewed early drafts and provided some useful suggestions.Many thanks also to Uri Guttman, who reviewed this book and suggested a number ofsmall corrections and improvements shortly before publication. Kamimura submitted acouple of corrections in the errata list on the O’Reilly web site.

viiiChapter 0. Preface

ContentsPrefacevIStarting with the Basics11The Way of the Program51.1What is a Program? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .51.2Running Perl 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .61.3The First Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .71.4Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .81.5Values and Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .81.6Formal and Natural Languages . . . . . . . . . . . . . . . . . . . . . . . . .111.7Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .121.8Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .121.9Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .132Variables, Expressions and Statements152.1Assignment Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .152.2Variable Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .162.3Expressions and Statements . . . . . . . . . . . . . . . . . . . . . . . . . . .182.4Script Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .202.5One-Liner Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .212.6Order of Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .222.7String Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .232.8Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .242.9Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .242.10Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .252.11Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .26

x34ContentsFunctions273.1Function Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .273.2Functions and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303.3Math functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303.4Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .323.5Adding New Functions (a.k.a. Subroutines) . . . . . . . . . . . . . . . . . .323.6Definitions and Uses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .343.7Flow of Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .353.8Parameters and Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . .353.9Variables and Parameters Are Local . . . . . . . . . . . . . . . . . . . . . .373.10Stack Diagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .373.11Fruitful Functions and Void Functions . . . . . . . . . . . . . . . . . . . . .383.12Function Signatures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .403.13Immutable and Mutable Parameters . . . . . . . . . . . . . . . . . . . . . .413.14Functions and Subroutines as First-Class Citizens . . . . . . . . . . . . . .423.15Why Functions and Subroutines? . . . . . . . . . . . . . . . . . . . . . . . .443.16Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .443.17Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .453.18Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .46Loops, Conditionals, and Recursion494.1Integer Division and Modulo . . . . . . . . . . . . . . . . . . . . . . . . . .494.2Boolean Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .504.3Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .524.4Conditional Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .544.5Alternative Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .544.6Chained Conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .554.7Nested Conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .554.8If Conditionals as Statement Modifiers . . . . . . . . . . . . . . . . . . . . .564.9Unless Conditional Statement . . . . . . . . . . . . . . . . . . . . . . . . . .574.10For Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .574.11Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .59

Contents56xi4.12Stack Diagrams for Recursive Subroutines . . . . . . . . . . . . . . . . . . .614.13Infinite Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .614.14Keyboard Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .624.15Program Arguments and the MAIN Subroutine . . . . . . . . . . . . . . . .624.16Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .634.17Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .644.18Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .65Fruitful Subroutines695.1Return Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .695.2Incremental Development . . . . . . . . . . . . . . . . . . . . . . . . . . . .715.3Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .735.4Boolean Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .735.5A Complete Programming Language . . . . . . . . . . . . . . . . . . . . . .755.6More Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .755.7Leap of Faith . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .775.8One More Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .785.9Checking Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .795.10Multi Subroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .805.11Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .815.12Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .825.13Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .83Iteration856.1Assignment Versus Equality . . . . . . . . . . . . . . . . . . . . . . . . . . .856.2Reassignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .866.3Updating Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .866.4The while Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .876.5Local Variables and Variable Scoping . . . . . . . . . . . . . . . . . . . . . .896.6Control Flow Statements (last, next, etc.) . . . . . . . . . . . . . . . . . . .916.7Square Roots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .936.8Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .956.9Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .956.10Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .966.11Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .96

xii7ContentsStrings997.1A String is a Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .997.2Common String Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . .1007.2.1String Length . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1007.2.2Searching For a Substring Within the String . . . . . . . . . . . . . . . 1007.2.3Extracting a Substring from a String . . . . . . . . . . . . . . . . . . . 1017.2.4A Few Other Useful String Functions or Methods . . . . . . . . . . . 1027.3String Traversal With a while or for Loop . . . . . . . . . . . . . . . . . . . 1047.4Looping and Counting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7.5Regular Expressions (Regexes) . . . . . . . . . . . . . . . . . . . . . . . . . 1067.6Using Regexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1077.7Building your Regex Patterns . . . . . . . . . . . . . . . . . . . . . . . . . .1097.87.91067.7.1Literal Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1097.7.2Wildcards and Character Classes . . . . . . . . . . . . . . . . . . . . .1097.7.3Quantifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1107.7.4Anchors and Assertions . . . . . . . . . . . . . . . . . . . . . . . . . .1117.7.5Alternation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1137.7.6Grouping and Capturing . . . . . . . . . . . . . . . . . . . . . . . . .1147.7.7Adverbs (a.k.a. Modifiers) . . . . . . . . . . . . . . . . . . . . . . . . .1147.7.8Exercises on Regexes . . . . . . . . . . . . . . . . . . . . . . . . . . . .115Putting It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1167.8.1Extracting Dates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1167.8.2Extracting an IP Address . . . . . . . . . . . . . . . . . . . . . . . . .117Substitutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1187.9.1The subst Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1197.9.2The s/search/replace/ Construct . . . . . . . . . . . . . . . . . . . . 1197.9.3Using Captures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1207.9.4Adverbs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1207.10Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1207.11Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1227.12Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .123

Contentsxiii8Case Study: Word Play1278.1Reading from and Writing to Files . . . . . . . . . . . . . . . . . . . . . . .1278.2Reading Word Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1298.3Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1308.4Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13098.4.1Words Longer Than 20 Characters (Solution) . . . . . . . . . . . . . . 1308.4.2Words with No “e” (Solution) . . . . . . . . . . . . . . . . . . . . . . .8.4.3Avoiding Other Letters (Solution) . . . . . . . . . . . . . . . . . . . . 1328.4.4Using Only Some Letters (Solution) . . . . . . . . . . . . . . . . . . . 1338.4.5Using All Letters of a List (Solution) . . . . . . . . . . . . . . . . . . . 1348.4.6Alphabetic Order (Solution) . . . . . . . . . . . . . . . . . . . . . . . .8.4.7Another Example of Reduction to a Previously Solved Problem . . . 1351311348.5Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1368.6Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1368.7Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .136Arrays and Lists1399.1Lists and Arrays Are Sequences . . . . . . . . . . . . . . . . . . . . . . . . . 1399.2Arrays Are Mutable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9.3Adding New Elements to an Array or Removing Some . . . . . . . . . . . 1439.4Stacks and Queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1459.5Other Ways to Modify an Array . . . . . . . . . . . . . . . . . . . . . . . . .1469.6Traversing a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1489.7New Looping Constructs . . . . . . . . . . . .

vi Chapter 0. Preface The History of this Book In the course of the last three to four years, I have translated or adapted to French a number of tutorials and articles on Perl 6, and I’ve also written a few entirely new ones in French. 1 Together, these documents represe

Related Documents:

Lemon Ginseng Green Tea Black Tea - Bavarian Wild Berry Black Tea - Black Pearl Black Tea - Tuscan Lemon 100% Natural Green Tea with Citrus 100% Natural Green Tea w/ Passionfruit Mango 100% Natural Iced Tea with Pomegranate Blueberry Iced Tea Lemonade Diet Green Tea with Citrus Diet Green Tea with Watermelon Diet Iced Tea with Lemon

Why Perl? Perl is built around regular expressions -REs are good for string processing -Therefore Perl is a good scripting language -Perl is especially popular for CGI scripts Perl makes full use of the power of UNIX Short Perl programs can be very short -"Perl is designed to make the easy jobs easy,

Perl can be embedded into web servers to speed up processing by as much as 2000%. Perl's mod_perl allows the Apache web server to embed a Perl interpreter. Perl's DBI package makes web-database integration easy. Perl is Interpreted Perl is an interpreted language, which means that your code can be run as is, without a

manufactured tea, tea is classified into six types: green tea, yellow tea, dark tea (containing brick tea and pu-erh tea), white tea, oolong tea and black tea. The so called fermentation in tea processing is not the anaerobic breakdown of energy-rich compound (as a carbohydrate to carbon dioxide

Other Perl resources from O’Reilly Related titles Learning Perl Programming Perl Advanced Perl Programming Perl Best Practices Perl Testing: A Developer’s . Intermedi

Run Perl Script Option 3: Create a Perl script my_script.pl: Run my_script.pl by calling perl: 8/31/2017 Introduction to Perl Basics I 10 print Hello World!\n; perl ./my_script.pl Option 4: For a small script with several lines, you can run it directly on the command line: perl -e print Hello World!\n;

Perl's creator, Larry Wall, announced it the next day in his State of the Onion address. Most notably, he said "Perl 6 is going to be designed by the community." Everyone thought that Perl 6 would be the version after the just-released Perl v5.6. That didn't happen, but that's why "Perl" was in the name "Perl 6."

tutorial Sorry about that but I have to keep my tutorial's example scripts short and to the point Finally, this is a tutorial for Perl/Tk only I will not be teaching perl here So if you know perl, continue But if you are a beginner to perl, I would recommend that you read my perl tutorial