Perl 6 Deep Dive
eBook - ePub

Perl 6 Deep Dive

Andrew Shitov

Share book
  1. 402 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Perl 6 Deep Dive

Andrew Shitov

Book details
Book preview
Table of contents
Citations

About This Book

Learn Perl 6 effortlessly to solve everyday problemsAbout 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 ForThis 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 DetailPerl 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 approachThis book will take you through essential Perl 6 concepts so you can implement them immediately

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Perl 6 Deep Dive an online PDF/ePUB?
Yes, you can access Perl 6 Deep Dive by Andrew Shitov in PDF and/or ePUB format, as well as other popular books in Informatica & Linguaggi di programmazione. We have over one million books available in our catalogue for you to explore.

Information

Year
2017
ISBN
9781787123458

Working with Operators

Operators are the elements of the syntax of the language which perform actions over their operands and return a result. Perl 6 is a language with dozens of operators. Some of them are inherited from Perl 5 (directly or with modifications), some were invented especially for Perl 6. On top of the set of regular operators, Perl 6 defines the so-called meta-operators and hyper-operators, which extend the meaning of regular operators for working on a group of values.
In this chapter, we will cover the following topics:
  • Operator classification
  • Unary operators
  • Binary operators
  • Ternary operator
  • Bitwise operators
  • Miscellaneous operators
  • Operator precedence
  • Substitution meta-operators
  • Assignment meta-operators
  • Negation meta-operators
  • Reversed meta-operators
  • Creating hyper-operators
  • Types of hyper-operations
  • Reduction hyper-operators
  • Cross hyper-operators
  • Zip hyper-operator
  • Sequential hyper-operators

Operator classification

First, let's remind ourselves of some of the basic terminology that we need when talking about operators. Consider a simple example:
my $a = 10;
my $b = 20;
my $c = 0;
$c = $a + $b;
say $c; # 30
Let's concentrate on the following line of code:
$c = $a + $b;
Here, we tell the compiler to perform two actions—first, calculate the sum of the $a and $b variables, and second, assign the result to the third variable, that is, $c. There are two operators in this example—+ and =. Operators are presented by their one-character names. In this case, the names are chosen to copy the corresponding operators in mathematics. Later, we will see examples of other operators, which are not just a character. They can be, for example, a sequence of two or three non-alphabetical symbols, such as >= or <= operators. Or, they can be a string identifier, for example—cmp or eq.

Categories of operators

In the previous section, we saw an example of the + operator, which takes two arguments. There are many other operators that are similar to +. For example, * is the operator for multiplication. Like the + operator, the * operator takes two arguments and returns a value.
my $c = $a * $b;
This kind of operator is called an infix operator, or simply infix. The operands of such operators are often called the left-hand side and right-hand side operands. As the operators take two arguments, they are also often called binary operators.
Another kind of operator needs only one argument. These operators are called unary operators. A typical example of a unary operator is unary minus. In the following example, this operator negates the value of its argument:
my $a = 10;
my $b = -$a;
say $b; # prints -10
Notice that this operator uses the same character as the binary minus operator, but both the programmer and the compiler can distinguish between the two:
my $a = 10;
my $b = -$a; # unary minus, $b becomes -10
my $c = $a - $b; # binary subtraction, $c is 20
Different unary operators can be placed either before the argument or after it. For example, the ++ operator has two forms—prefix and postfix. The following example demonstrates the two alternatives:
my $a = 10;
++$a; # prefix operator ++
$a++; # postfix operator ++
The position of an operator (it is either placed before or after an argument) changes its meaning.
So far, we've met infix, prefix, and postfix operators. There are two more categories of operators in Perl 6.
Circumfix operators are another kind of unary operators. Unlike operators like unary -, circumfix operators consist of two complementary parts, such as parentheses. The only operand of a circumfix operator is placed between them, for example—the [$a] construction uses the [] circumfix operator that takes $a as an argument.
Finally, there are postcircumfix operators. They need two operands, and the syntax is the following—operand1[operand2]. One of the most practical examples of the postcircumfix operator is the function call. We've seen it a few times already—add($a, $b).
Let's summarize the operator categories in the following table using the + operator symbol as an example:
Category Syntax
infix operand1 + operand2
prefix +operand
postfix operand+
circumfix (operand)
postcircumfix operand1[operand2]

Operators as functions

Operators perform some actions over their arguments. Operator's arguments are called operands. In the preceding example, the + operator takes two operands, $a and $b. The = operator also takes two operands—on the left side of it, it expects the variable, to which it will assign the value of the operand on the right side.
In any programming language, operators are simply a handy syntactical solution to have more expressive programs and can be replaced with calling a function. For example, in the preceding example, you write $c = $a + $b, but you can also do the same by calling the add function that we saw in Chapter 1, What is Perl 6?. Let's rewrite the previous example:
my $a = 10;
my $b = 20;
my $c = 0;
$c = add($a, $b);
say $c; # 30

sub add($a, $b) {
return $a + $b;
}
Of course, the add function uses the + operator itself, but we cannot avoid it here because there are no more low-level functions for addition in Perl 6. The purpose of the example was to demonstrate that operators can always be treated as functions that accept a few arguments and return a value, but you do not call them directly; rather via a good-looking operator.
In Perl 6, you may use the functional style when working with operators. For that, use the key...

Table of contents