- Perl 6 Deep Dive
- Andrew Shitov
- 417字
- 2021-07-03 00:05:55
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:
- 新編Visual Basic程序設(shè)計(jì)上機(jī)實(shí)驗(yàn)教程
- JavaScript從入門到精通(微視頻精編版)
- SpringMVC+MyBatis快速開發(fā)與項(xiàng)目實(shí)戰(zhàn)
- Mastering Python Scripting for System Administrators
- 匯編語(yǔ)言程序設(shè)計(jì)(第2版)
- 人臉識(shí)別原理及算法:動(dòng)態(tài)人臉識(shí)別系統(tǒng)研究
- YARN Essentials
- Java Web開發(fā)技術(shù)教程
- C++ 從入門到項(xiàng)目實(shí)踐(超值版)
- SQL Server與JSP動(dòng)態(tài)網(wǎng)站開發(fā)
- Android開發(fā)三劍客:UML、模式與測(cè)試
- Processing創(chuàng)意編程指南
- HTML5+CSS3+jQuery Mobile APP與移動(dòng)網(wǎng)站設(shè)計(jì)從入門到精通
- 監(jiān)控的藝術(shù):云原生時(shí)代的監(jiān)控框架
- 深度學(xué)習(xí)程序設(shè)計(jì)實(shí)戰(zhàn)