Perl 6 Deep Dive
Total Page:16
File Type:pdf, Size:1020Kb
Perl 6 Deep Dive Data manipulation, concurrency, functional programming, and more Andrew Shitov BIRMINGHAM - MUMBAI Perl 6 Deep Dive Copyright © 2017 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: September 2017 Production reference: 1060917 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78728-204-9 www.packtpub.com Credits Author Copy Editor Andrew Shitov Safis Editing Reviewer Project Coordinator Alex Kapranoff Prajakta Naik Commissioning Editor Proofreader Merint Mathew Safis Editing Acquisition Editor Indexer Chaitanya Nair Francy Puthiry Content Development Editor Graphics Lawrence Veigas Abhinash Sahu Technical Editor Production Coordinator Mehul Singh Nilesh Mohite About the Author Andrew Shitov has been a Perl enthusiast since the end of the 1990s, and is the organizer of over 30 Perl conferences in eight countries. He worked as a developer and CTO in leading web-development companies, such as Art. Lebedev Studio, Booking.com, and eBay, and he learned from the "Fathers of the Russian Internet", Artemy Lebedev and Anton Nossik. Andrew has been following the Perl 6 development since its beginning in 2000. He ran a blog dedicated to the language, published a series of articles in the Pragmatic Perl magazine, and gives talks about Perl 6 at various Perl events. In 2017, he published the Perl 6 at a Glance book by DeepText, which was the first book on Perl 6 published after the first stable release of the language specification. Acknowledgements Through the course of writing this book, I contacted many people who helped me clarify many dark corners of Perl 6. First of all, thanks to the reviewers of this book, Liz Mattijsen, who is also the core developer of the Perl 6 compiler, and Alex Kapranoff, who has helped me promote Perl 6 for more than a decade already. I would also like to thank the whole Rakudo compiler team, who worked hard to make things happen even when almost nobody believed that Perl 6 would become a reality. So, primarily, thanks to Jonathan Worthington for not only working on the compiler, but also for his talks, which I found on YouTube and used for reference. Also, thanks to the rest of the team. Special thanks go to Wendy van Dijk, who has done great work to change the image of the Perl language and has personally helped me promote my books. And of course, thanks to the author of Perl, Larry Wall, and his wife, Gloria, for the years of patience and enthusiasm; they made Perl 6 fly in the end. This book has also been possible due to the number of other Perl 6 books that appeared this year; so, I want to thank the authors of those books as they indirectly stimulated me to write faster and more. Finally, this book took its shape with the efforts of the editors at Packt, Lawrence Veigas, who guided me through the writing process, and Mehul Singh, who joined at the last stage of the publishing process and provided many suggestions on how to make the book better and more useful for the readers. About the Reviewer Alex Kapranoff studied software engineering in one of the smaller Russian cities and then worked in the internet industry for about 20 years in roles ranging from software developer to CTO to technical manager. Having written production code in many programming languages, he still considers Perl his one true love. Perl 6 is a huge step forward for the language, for the ecosystem, and, maybe even for programming language theory. Agava, Inline Technologies, Rambler.ru, and Yandex are companies that provided Alex with the opportunities to be his best and write impactful code in Perl on a day-to-day basis throughout his career. Alex has worked on a number of books about the performance-oriented web server software, Nginx, and published his own take on Nginx Troubleshooting with Packt Publishing. I am very grateful to the whole worldwide Perl community – an amazing group of people using and constantly improving the language that we all love and have the most impact with. www.PacktPub.com For support files and downloads related to your book, please visit www.PacktPub.com. Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks. https://www.packtpub.com/mapt Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career. Why subscribe? Fully searchable across every book published by Packt Copy and paste, print, and bookmark content On demand and accessible via a web browser Customer Feedback Thanks for purchasing this Packt book. At Packt, quality is at the heart of our editorial process. To help us improve, please leave us an honest review on this book's Amazon page at https://www.amazon.com/dp/178728204X. If you'd like to join our team of regular reviewers, you can email us at [email protected]. We award our regular reviewers with free eBooks and videos in exchange for their valuable feedback. Help us be relentless in improving our products! Table of Contents Chapter 1: What is Perl 6? 7 Origins of Perl 6 7 Differences from Perl 5 8 Sigils 8 Signatures 9 Classes 11 Compatibility with Perl 5 13 Perl 6 resources 14 Documentation 14 Test Suite 15 STD.pm 15 Community 15 Compilers 16 Parrot 17 Pugs 17 Perlito 17 Rakudo 18 Working with Rakudo Star 18 Downloading and installing Rakudo Star 18 Command-line options 19 The -c command 19 The --doc command 20 The -e command 20 The -h and --help commands 21 The -n command 21 The -p command 21 The -I and -M commands 21 The -v and --version command 22 The --stagestats command 22 Writing our Hello World program 23 Summary 24 Chapter 2: Writing Code 25 Using Unicode 25 Whitespaces and unspaces 29 Comments 31 One-line comments 31 Multiline comments 33 Embedded comments 35 Creating Pod documentation 35 The =begin / =end Pod block 36 Phasers 39 Simple input and output 40 Summary 42 Chapter 3: Working with Variables and Built-in Data Types 43 Using variables 43 Declaring variables 44 Variable containers in Perl 6 44 Scalars 45 Arrays 45 Methods of the Array type 46 Hashes 48 Methods of the Hash class 49 Naming conventions 50 Typed variables 51 Using simple built-in data types 52 Integer data type 54 Methods of the Int type 56 Converting to a character using the chr method 56 Checking whether the number is prime 56 Generating a random number 57 Getting the sign of the value 57 Calculating the square root of the value 57 Getting the next and previous values 58 Getting the absolute value 58 Rational data type 58 Methods of the Rat type 59 Getting the Perl representation of the value 60 Converting to an Int value 60 Getting the numerator and denominator 60 Methods for rounding the value 61 Methods pred and succ 61 Numeric data type 62 Num versus Numeric versus Real 62 Enumerations 63 Boolean data type 63 Methods of the Bool type 64 Using pred and succ 64 Methods to generate random Boolean values 64 String data type 65 [ ] Methods of the Str class 65 Converting register 65 Methods to cut strings 66 Methods to check the content of a string 67 Length of the string 67 Reversing a string 67 Complex numbers 68 Methods of the Complex data type 68 Getting real and imaginary parts 68 Data types to manipulate date and time 69 Using the Date class 69 Using the DateTime data type 71 Summary 72 Chapter 4: Working with Operators 73 Operator classification 74 Categories of operators 74 Operators as functions 76 Operators in Perl 6 77 Infix operators 77 Assignment operators 77 Operators for multiplication and division 78 Operators for addition and subtraction 79 Modulo operator 80 Divisibility operator 81 Integer division and modulo operators 81 Bitwise operators 82 Integer shift operators 83 String logical operators 83 Boolean logical operators 83 Great common divisor and least common multiple operators 84 String repetition operator 85 List repetition operator 86 String concatenation operator 86 Junction operators 86 The does operator 87 The but operator 88 Universal comparison operator 88 String comparison operator leg 89 Comparison operator for Real numbers 90 Range creating operator 90 Equality and non-equality operators 90 Numerical comparison operators 91 String comparison