Reader small image

You're reading from  Perl 6 Deep Dive

Product typeBook
Published inSep 2017
Reading LevelIntermediate
PublisherPackt
ISBN-139781787282049
Edition1st Edition
Languages
Right arrow
Author (1)
Andrew Shitov
Andrew Shitov
author image
Andrew Shitov

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 dotCom, 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.
Read more about Andrew Shitov

Right arrow

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 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...

Operators in Perl 6

There are a few dozen built-in operators in Perl 6. To make the overview more structured, we will group them in groups corresponding to the categories that we described in the previous sections:

  • Infix operators
  • Postfix operators
  • Circumfix operators
  • Postcircumfix operators

In the following sections, we will examine operators of Perl 6 grouped into these categories. Within each category, operators are arranged in descending precedence.

Infix operators

Infix operators are probably the most commonly used operators in the language. They are also the most intuitive ones.

Assignment operators

...

Meta-operators in Perl 6

So far, we have covered many of the operators that operate on regular operands—values, variables, objects, and so on. In Perl 6, there are operators of another kind—operators that operate over operators. These operators are called meta-operators. We will examine them in the following sections. With some exceptions, every meta-operator can take any regular operator to create a new operation that follows certain rules. Meta-operators also work with user-defined operators, which we will discuss later in this chapter, in the User-defined operators section.

Assignment meta-operator

The assignment meta-operator takes the op= form, where op is one of the operators available in Perl 6.

For example...

User-defined operators

Perl 6 allows creating new operators. Unlike, for example, C++, new operators are not restricted to the predefined list of existing operators. You are free to name the operators as you want and choose a new combination of characters.

A user-defined operator should belong to one of the preceding-mentioned categories, such as infix, prefix, or circumfix, and so on.

Let's start with creating a new infix operator, +%, which calculates the sum of two numeric operands, but the result does not exceed 100:

sub infix:<+%>($a, $b) {
my $sum = $a + $b;
return $sum < 100 ?? $sum !! 100;
}

Defining an operator is similar to creating a subroutine, but the name of it should contain the name of the category and the operator itself.

It is now time to test the just created +% operator:

say 10 +% 20; # 30
say 40 +% 70; # 100

Another expressive example is...

Summary

In this long chapter, we talked about the operators in Perl 6. There are a few categories of operators, such as infix, prefix, postfix, circumfix, and postcircumfix. We discussed the operators from each group. Then, we looked at how meta-operators and hyper-operators create new operators based on the built-in ones. Finally, you learned how to create user-defined operators, which will be naturally embedded in the language of your program.

Up till now, we have covered all the basics of Perl 6 grammar. In the next chapter, we will move on to the next level of organizing code using subroutines.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Perl 6 Deep Dive
Published in: Sep 2017Publisher: PacktISBN-13: 9781787282049
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Andrew Shitov

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 dotCom, 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.
Read more about Andrew Shitov