O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Perl 6 Deep Dive

Book Description

Learn Perl 6 effortlessly to solve everyday problems

About This Book

  • Filled with practical examples, this comprehensive guide explores all aspects of Perl 6.
  • Leverage the power of Perl 6 concurrency to develop responsive and high-performant software.
  • Delves into various programming paradigms (such as Object Oriented, functional, and reactive) that can be adopted by Perl 6 developers to write effective code.

Who This Book Is For

This book is for developers who would like to learn the Perl programming language. A basic knowledge of programming is assumed.

What You Will Learn

  • Learn the background from which Perl 6 appeared and how it developed.
  • How to use Rakudo to run your programs.
  • Various Perl 6 built-in types and details about their behavior
  • Understand how scalar variables, hash variables, and arrays work
  • Create meta operators and hyper operators
  • How classes work and how to build software based on the Object Oriented Paradigm
  • How Perl 6 provides support for concurrency, functional programming, and reactive programming.

In Detail

Perl is a family of high-level, general-purpose, interpreted, dynamic programming languages consisting of Perl 5 and Perl 6. Perl 6 helps developers write concise and declarative code that is easy to maintain.

This book is an end-to-end guide that will help non-Perl developers get to grips with the language and use it to solve real-world problems.

Beginning with a brief introduction to Perl 6, the first module in the book will teach you how to write and execute basic programs. The second module delves into language constructs, where you will learn about the built-in data types, variables, operators, modules, subroutines, and so on available in Perl 6. Here the book also delves deeply into data manipulation (for example, strings and text files) and you will learn how to create safe and correct Perl 6 modules. You will learn to create software in Perl by following the Object Oriented Paradigm. The final module explains in detail the incredible concurrency support provided by Perl 6. Here you will also learn about regexes, functional programming, and reactive programming in Perl 6.

By the end of the book, with the help of a number of examples that you can follow and immediately run, modify, and use in practice, you will be fully conversant with the benefits of Perl 6.

Style and approach

This book will take you through essential Perl 6 concepts so you can implement them immediately

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

Table of Contents

  1. What is Perl 6?
    1. Origins of Perl 6
    2. Differences from Perl 5
      1. Sigils
      2. Signatures
      3. Classes
      4. Compatibility with Perl 5
    3. Perl 6 resources
      1. Documentation
      2. Test Suite
      3. STD.pm
      4. Community
    4. Compilers
      1. Parrot
      2. Pugs
      3. Perlito
      4. Rakudo
    5. Working with Rakudo Star
      1. Downloading and installing Rakudo Star
      2. Command-line options
        1. The -c command
        2. The --doc command
        3. The -e command
        4. The -h and --help commands
        5. The -n command
        6. The -p command
        7. The -I  and -M commands
        8. The -v and --version command
        9. The --stagestats command
    6. Writing our Hello World program
    7. Summary
  2. Writing Code
    1. Using Unicode
    2. Whitespaces and unspaces
    3. Comments
      1. One-line comments
      2. Multiline comments
      3. Embedded comments
    4. Creating Pod documentation
      1. The =begin / =end Pod block
    5. Phasers
    6. Simple input and output
    7. Summary
  3. Working with Variables and Built-in Data Types
    1. Using variables
      1. Declaring variables
      2. Variable containers in Perl 6
        1. Scalars
        2. Arrays
          1. Methods of the Array type
        3. Hashes
          1. Methods of the Hash class
      3. Naming conventions
      4. Typed variables
    2. Using simple built-in data types
      1. Integer data type
        1. Methods of the Int type
          1. Converting to a character using the chr method
          2. Checking whether the number is prime
          3. Generating a random number
          4. Getting the sign of the value
          5. Calculating the square root of the value
          6. Getting the next and previous values
          7. Getting the absolute value
      2. Rational data type
        1. Methods of the Rat type
          1. Getting the Perl representation of the value
          2. Converting to an Int value
          3. Getting  the numerator and denominator
          4. Methods for rounding the value
          5. Methods pred and succ
      3. Numeric data type
        1. Num versus Numeric versus Real
      4. Enumerations
      5. Boolean data type
        1. Methods of the Bool type
          1. Using pred and succ
          2. Methods to generate random Boolean values
      6. String data type
        1. Methods of the Str class
          1. Converting register
          2. Methods to cut strings
          3. Methods to check the content of a string
          4. Length of the string
          5. Reversing a string
      7. Complex numbers
        1. Methods of the Complex data type
          1. Getting real and imaginary parts
    3. Data types to manipulate date and time
      1. Using the Date class
      2. Using the DateTime data type
    4. Summary
  4. Working with Operators
    1. Operator classification
      1. Categories of operators
      2. Operators as functions
    2. Operators in Perl 6
      1. Infix operators
        1. Assignment operators
        2. Operators for multiplication and division
        3. Operators for addition and subtraction
        4. Modulo operator
        5. Divisibility operator
        6. Integer division and modulo operators
        7. Bitwise operators
        8. Integer shift operators
        9. String logical operators
        10. Boolean logical operators
        11. Great common divisor and least common multiple operators
        12. String repetition operator
        13. List repetition operator
        14. String concatenation operator
        15. Junction operators
        16. The does operator
        17. The but operator
        18. Universal comparison operator
        19. String comparison operator leg
        20. Comparison operator for Real numbers
        21. Range creating operator
        22. Equality and non-equality operators
        23. Numerical comparison operators
        24. String comparison operators eq and ne
        25. Other string comparison operators
        26. The before and after operators
        27. Equivalency test operator
        28. Value identity operator
        29. Bound check operator
        30. Smartmatch operator
        31. Approximate-equality operator
        32. Boolean logical operators
        33. Defined-or operator
        34. Operators for minimum and maximum
        35. Pair creation operator
        36. Comma operator
        37. Invocant separator
        38. Zip operator
        39. Cross operator
        40. Sequence operator
        41. Binding operators
        42. Logical operator with lower precedence
        43. Data pipe operators
      2. Ternary operator  ?? !!
      3. Prefix operators
        1. Increment and decrement operators ++ and --
        2. Boolean coercion operator
        3. Boolean negation operator
        4. Numeric coercion operator
        5. Numeric negation operator
        6. String coercion operator
        7. Two-complement binary negation operator
        8. Boolean coercion and negation operator
        9. The upto operator
        10. The temp operator
        11. The let operator
        12. The not operator
        13. The so operator
      4. Postfix operators
        1. Object-oriented postfix operators
        2. Increment and decrement operators
      5. Circumfix operators
        1. Quote-word operator
        2. Group operator
        3. Hash or block creation operator
      6. Postcircumfix operators
        1. Positional access operator
        2. Element access operators
        3. Invoke operator 
    3. Meta-operators in Perl 6
      1. Assignment meta-operator
      2. Negation meta-operator
      3. Reverse meta-operator
      4. Reduction meta-operator
      5. Cross meta-operator
      6. Zip meta-operator
      7. Hyper-operators
    4. User-defined operators
    5. Summary
  5. Control Flow
    1. Understanding code blocks and variable scoping
      1. The do keyword
    2. Conditional checks
    3. Using loops
      1. The loop cycle
      2. The for loop
      3. Using while, until, and repeat
      4. Breaking the loop
        1. Using labels
    4. Executing code once
    5. Collecting data with gather and take
    6. Setting the topic with given
    7. Summary
  6. Subroutines
    1. Creating and calling subroutines
      1. Default values
      2. Optional parameters
      3. Named parameters
      4. Parameter traits
      5. Slurpy parameters
      6. Parameter placeholders
    2. Type constraints
      1. Typed parameters
      2. Return type
    3. Multi subs
      1. An example
    4. Nested subroutines
    5. Creating operators
    6. Passing functions as arguments
    7. Anonymous subs
    8. Summary
  7. Modules
    1. Creating a module
    2. Using modules
      1. The need keyword
      2. The import keyword 
      3. The use keyword
      4. The require keyword
      5. Scoping
      6. More on is export
        1. Exporting variables
        2. Selective import
    3. Introspection
    4. Using zef
      1. Installing a module
      2. Searching for a module
      3. Uninstalling modules
      4. zef command summary
      5. How Rakudo stores modules
    5. Summary
  8. Object-Oriented Programming
    1. Creating a class
    2. Working with attributes
      1. Read-and-write attributes
      2. Typed attributes
      3. Using other classes as data types
    3. Working with methods
      1. Private methods
    4. More about attributes
      1. Public and private attributes
      2. Automatic getters and setters
      3. Class attributes
    5. Class methods
    6. Inheritance
      1. Inheriting from a class
      2. Using child class instances as objects of a base class
      3. Overriding methods
      4. Submethods
      5. Multiple inheritance
    7. Appending objects and classes using roles
    8. Using introspection to learn more
    9. Method postfix operators
    10. Summary
  9. Input and Output
    1. Standard input and output
    2. Working with files and directories
      1. Opening a file
      2. Closing a file
      3. Testing file and directory properties
      4. Manipulating files
      5. Working with directories
    3. Reading from a stream
      1. Reading a single line
      2. Reading characters
      3. Lazy readers
      4. The eof method
    4. Writing to a stream
      1. The print  function
      2. The say method
        1. Example of using the gist method
      3. The printf method
        1. Characters and strings
        2. Integers
        3. Floating-point numbers
    5. Summary
  10. Working with Exceptions
    1. The try block
      1. The $! variable
    2. Soft failures
    3. The CATCH phaser
    4. The Exception object
      1. Throwing exceptions
      2. Resuming from exceptions
      3. Typed exceptions
      4. Rethrowing exceptions
    5. The Failure object
    6. Creating custom exceptions
    7. Summary
  11. Regexes
    1. Matching against regexes
    2. Literals
    3. Character classes
      1. The . (dot) character
      2. Backslashed character classes
        1. \s and \S characters
        2. \t and \T characters
        3. \h and \H characters
        4. \v and \V characters
        5. \n and \N characters
        6. \d and \D characters
        7. \w and \W characters
      3. Character classes
      4. Predefined subrules
      5. Using Unicode properties
      6. Character class arithmetics
    4. Creating repeated patterns with quantifiers
      1. Greediness
    5. Extracting substrings with capturing
      1. Capturing groups
      2. The Match object
      3. Named captures
    6. Using alternations in regexes
    7. Positioning regexes with anchors
      1. Matching at the start and at the end of lines or strings
      2. Matching word boundaries
    8. Looking forward and backward with assertions
    9. Modifying regexes with adverbs
      1. :i (:ignorecase)
      2. :s (:sigspace)
      3. :p (:pos)
      4. :g (:global)
      5. :c (:continue) 
      6. :r (:ratchet)
      7. :ov (:overlap)
      8. :ex (:exhaustive)
    10. Substitution and altering strings with regexes
    11. Summary
  12. Grammars
    1. Creating a grammar
      1. Matching a grammar
    2. Using rules and tokens
    3. Using actions
    4. Using abstract syntax tree attributes
      1. Handling expressions
    5. Using the actions class
    6. The whole program
    7. Summary
  13. Concurrent Programming
    1. Junctions
      1. Autothreading
    2. Threads
      1. Starting a thread
      2. Creating and running a new thread
      3. The id and name methods
      4. Printing thread objects as a string
      5. Lifetime threads
      6. Using locks in Perl 6
    3. Promises
      1. Creating a promise
      2. Statuses of a promise
      3. Factory methods
      4. The result of a promise
      5. Combining promises
      6. Executing code after the promise is kept or broken
      7. The anyof and allof methods
    4. Channels
      1. Basic use cases
      2. To wait or not to wait?
      3. Closing channels
      4. Channels as queues
    5. Summary
  14. Functional Programming
    1. What is functional programming?
    2. Using recursion
    3. Using reduction
    4. Higher-order functions and lambdas
      1. The WhateverCode blocks
    5. Piping data and feed operators
    6. Manipulating the scope
      1. Closures
      2. Currying
      3. Dynamic scope
    7. Creating and using iterators
    8. Lazy and infinite lists
    9. Summary
  15. Reactive Programming
    1. What is reactive programming?
    2. On-demand supplies
      1. Generating data with supplies
      2. The react and whenever keywords
      3. Using lists as the source of the supply data
    3. Live supplies
      1. Filtering and transforming data streams
    4. Summary