by Mícheál Ó Foghlú
Perl has a range of operators, many of which are similar to the operators used in C. Also, many Perl functions can be used either as a unary operator or as a function. The difference in the syntax is that the function call has parentheses enclosing the parameters. The difference in semantics is that the function form has a higher precedence. All such operators are listed as functions rather than as operators in this book.
This chapter categorizes each operator in several ways:
The following list shows the precedence of the operators:
This chapter contains detailed descriptions of these operators.
Name: logical negation Precedence: 5 Associativity: right Type of operands: numeric, string Number of operands: one (unary) Context: scalar
The return value of this operation is 1 (true) if the operand has a false value that is defined as 0 in a numeric operand, a null string, or an undefined value. Otherwise, the return value is '' (false)-that is, a null string that evaluates to 0 in a numeric context.
Example:
$one = !1; $two = !22; $three = !0; $four = !'hello'; $five = !''; print "1=$one, 2=$two, 3=$three, 4=$four, 5=$five, \n";
Name: relational not equal to Precedence: 12 Associativity: nonassociative Type of operands: string Number of operands: two (binary) Context: scalar
The return value of this operation is 1 (true) if the string operands are not equal. The return value is '' (false) if the string operands are equal. Every character in the strings is compared based on the character codes.
Example:
$tmp = "aaa "; $ans = "aaa" != $tmp; if ($ans) { print "true\n"; } else { print "false\n"; }
Name: bind pattern (with negation of return value) Precedence: 6 Associativity: left Type of operands: string Number of operands: two (binary) Context: scalar See also: =~
This operator binds a pattern-matching operation to a string variable other than $_. If the pattern match is successful, the return value is '' (false); if the pattern match is not successful, the return value is 1 (true).
Example:
$tmp = "superduper"; if ($tmp !~ s/duper/dooper/) {print "Did not do a substitute, tmp still is: $tmp\n";} else {print "Did a substitute, tmp now is: $tmp\n";}
Name: modulus Precedence: 7 Associativity: left Type of operands: numeric Number of operands: two (binary) Context: scalar
The operands are converted to integers, if necessary. The left side is divided by the right side, and the integer remainder is returned.
Example:
$ans = 48 % 5; print "48 mod 4 is: $ans\n";
Name: modulus assignment Precedence: 18 Associativity: right Type of operands: numeric Number of operands: two (binary) Context: scalar
This operation, like all the extra assignment operations, is a way to make the evaluation of the arguments more efficient.
Example:
$ans = 48; $ans %= 5; print "48 mod 4 is: $ans\n";
Name: bitwise and Precedence: 13 Associativity: left Type of operands: numeric (integer) Number of operands: two (binary) Context: scalar
This operator performs a bitwise and on the binary representation of the two numeric operands.
Example:
$ans = 456 & 111; print "Bitwise and 456 & 111 is: $ans\n";
Name: symbolic logical and Precedence: 15 Associativity: left Type of operands: numeric, string Number of operands: two (binary) Context: scalar
As in all logical operations, a null string and zero are false. This operator returns 1 (true) if both of the operands are true or null (false) if either operand is false or both operands are false.
Example:
$ans = 1 && print("This will print.\n") && 0 && print("This won't print!\n"); if ($ans) {print("So it's all true!\n");} else {print("So it's not all true. (expected)\n");}
Name: assignment logical and Precedence: 19 Associativity: right Type of operands: numeric, string Number of operands: two (binary) Context: scalar
This operator is a combination of the logical and assignment operators. This operator is more efficient when a new value is being reassigned to the same variable, because the reference needs to be computed only one time.
Example:
$ans = 1; $ans &&= "eggs" eq "eggs"; if ($ans) {print("It's as true as eggs is eggs. (expected)\n");} else {print("Not true, I'm afraid.");}
Name: assignment bitwise and Precedence: 19 Associativity: right Type of operands: numeric (integer) Number of operands: two (binary) Context: scalar
This operator is a combination of the bitwise and assignment operators. This operator is more efficient when a new value is being reassigned to the same variable, because the reference needs to be computed only one time.
Example:
$ans = 456; $ans &= 111; print("Bitwise and 456 & 111 is $ans\n");
Name: multiplication Precedence: 7 Associativity: left Type of operands: numeric Number of operands: two (binary) Context: scalar
This operator returns the numeric result of multiplying the two numeric operands.
Example:
$ans = 7 * 10; print("$ans (expected 70)\n");
Name: exponentiation Precedence: 4 Associativity: right Type of operands: numeric Number of operands: two (binary) Context: scalar
The operation x**y returns the value of x raised to the power of y.
Example:
$ans = 2 ** 3; print ("$ans (expected 8)\n");
Name: assignment exponentiation Precedence: 19 Associativity: right Type of operands: numeric Number of operands: two (binary) Context: scalar
This operator is a combination of the exponentiation and assignment operators. This operator is more efficient when a new value is being reassigned to the same variable, because the reference needs to be computed only one time.
Example:
$ans = 2; $ans **= 3; print ("$ans (expected 8)\n");
Name: assignment multiplication Precedence: 19 Associativity: right Type of operands: numeric Number of operands: two (binary) Context: scalar
This operator is a combination of the multiplication and assignment operators. This operator is more efficient when a new value is being reassigned to the same variable, because the reference needs to be computed only one time.
Example:
$ans = 7; $ans *= 10; print ("$ans (expected 70)\n");
Name: unary plus Precedence: 5 Associativity: right Type of operands: numeric, string Number of operands: one (unary) Context: scalar
This operator does not actually have any operation on a numeric or a string operand. In certain circumstances, the operator can disambiguate an expression. When a parenthesis follows a function name, it is taken to indicate a complete list of the arguments to the function, unless the parenthesis is preceded by + to make the parenthesized expression just one of the list arguments for that function.
Example:
@ans = sort +(5 + 5) * 10, -4; print("@ans (expected 100, -4)\n");
Name: addition Precedence: 8 Associativity: left Type of operands: numeric Number of operands: two (binary) Context: scalar
This operator returns the sum of the two operands.
Example:
$ans = 15 + 5; print("$ans (expected 20)\n");
Name: autoincrement Precedence: 3 Associativity: nonassociative Type of operands: numeric, string Number of operands: one (unary) Context: scalar
In a numeric context, the autoincrement adds 1 to the operand. If the syntax is prefix, the value before the increment is returned. If the syntax is postfix, the value after the increment is returned.
With a string operand (that has never been used in a numeric context), the autoincrement has a "magic" behavior. If the string is an alphanumeric expression, such as /^[a-zA-Z]*[0-9]*$/, the increment is carried out on the string, including a carry.
Example:
$ans = 45; print $ans, " (expected 45) "; print $ans++, " (expected 45) "; print ++$ans, " (expected 47)\n";
Name: Precedence: 19 Associativity: right Type of operands: numeric Number of operands: two (binary) Context: scalar
This operator is a combination of the summation and assignment operators. This operator is more efficient when a new value is being reassigned to the same variable, because the reference needs to be computed only one time.
Example:
$ans = 15; $ans += 5; print("$ans (expected 20)\n");
Name: comma Precedence: 20 Associativity: left Type of operands: numeric, string Number of operands: two (binary) Context: scalar, list
In a scalar context, the comma operator evaluates the operand to the left, discards the result, evaluates the operand to the right, and returns that value as the result.
In an array context, the comma operator separates items in the list. The operator behaves as though it returns both operands as part of the list.
Example:
$ans = ('one', 'two', 'three'); print("$ans (expected three)\n");
Name: negation Precedence: 5 Associativity: right Type of operands: numeric, string, identifier Number of operands: one (unary) Context: scalar
This operator returns the negated value of a numeric operand. If the operand is a string that begins with a plus (+) or minus (-) sign, the operator returns a string that has the opposite sign. If the argument is an identifier, the operator returns a string that comprises the identifier prefixed with a minus sign.
Example:
$ans = 45; $ans = -$ans; print("$ans (expected -45)\n");
Name: subtraction Precedence: 8 Associativity: left Type of operands: numeric Number of operands: two (binary) Context: scalar
This operator returns the first operand minus the second operand.
Example:
$ans = 50 - 10; print("$ans (expected 40)\n");
Name: autodecrement Precedence: 3 Associativity: nonassociative Type of operands: numeric Number of operands: one (unary) Context: scalar
This operator decrements its operand. The value returned is before the decrement takes place if the operator is in prefix notation (- - 56 returns 56), and the value returned is with the decrement having taken place if the operator is in postfix notation (56 - - returns 55).
Unlike the autoincrement operator, ++, this operator does not operate on strings.
Example:
$ans = 45; print $ans, " (expected 45) "; print $ans--, " (expected 45) "; print --$ans, " (expected 43)\n";
Name: assignment subtraction Precedence: 19 Associativity: right Type of operands: numeric Number of operands: two (binary) Context: scalar
This operator is a combination of the subtraction and assignment operators. This operator is more efficient when a new value is being reassigned to the same variable, because the reference needs to be computed only one time.
Example:
$ans = 50; $ans -= 10; print("$ans (expected 40)\n");
Name: dereference Precedence: 2 Associativity: left Type of operands: special Number of operands: two (binary) Context: scalar, array
This operator is new to Perl 5. The capability to create and manipulate complex data types with references provides flexibility in Perl 5 that was not present in Perl 4. This operator is just one of the aspects of this functionality.
The operands for this operator can be:
The operator allows you to access the elements in the data structure referenced by the left side (an array name, a hash name, an object, or a class name). Because there is no automatic dereferencing, you must use this syntax to dereference such a reference.
Example:
@ans = (100, 200, 300); $ansref = \@ans; $ansref->[2] = 400; print $ans[2], " (expected 400)\n";
Name: string concatenation Precedence: 8 Associativity: left Type of operands: string Number of operands: two (binary) Context: scalar
This operator joins the two string operands, returning a longer string.
Example:
$ans = "jordy" . " jordy"; print $ans, " (expected jordy jordy)\n";
Name: range operator Precedence: 17 Associativity: nonassociative Type of operands: numeric, string Number of operands: two (binary) Context: scalar, list
In a list context, the range operator returns an array of values, starting from the left operand up to the right operand in steps of 1. In this context, the range operator can use "magic" increments to increment strings, as with the autoincrement operator (++).
In a scalar context, the range operator returns a Boolean value. In effect, the return value remains false as long as the left operand is false. When the left operand becomes true, it becomes true until the right operand is true, after which it becomes false again.
The range operator can be used in a scalar context to set conditions for certain ranges of line numbers of an input file. This works because the default behavior when either operand is numeric is to compare that operand with the current line number (the $INPUT_LINE_NUMBER or $. special variable). Thus it is easy, using this construct, to treat certain lines in an input file differently. In the following example, the first five lines of the input file are suppressed from being output.
Example:
@ans = 1..5; print("@ans (expected 12345)\n"); open(INFILE,"<infile.tst"); while(<INFILE>) { print unless (1..5); }
Name: assignment concatenation Precedence: 19 Associativity: right Type of operands: string Number of operands: two (binary) Context: scalar
This operator is a combination of the concatenation and assignment operators. This operator is more efficient when a new value is being reassigned to the same variable, because the reference needs to be computed only one time.
Example:
$ans = "jordy"; $ans .= " jordy"; print $ans, " (expected jordy jordy)\n";
Name: division Precedence: 7 Associativity: left Type of operands: numeric Number of operands: two (binary) Context: scalar
This operator returns the product of the operands.
Example:
$ans = 10/2; print("$ans (expected 5)\n");
Name: assignment division Precedence: 19 Associativity: right Type of operands: numeric Number of operands: two (binary) Context: scalar
This operator is a combination of the division and assignment operators. This operator is more efficient when a new value is being reassigned to the same variable, because the reference needs to be computed only one time.
Example:
$ans = 10; $ans /= 2; print("$ans (expected 5)\n");
Name: numeric less then Precedence: 11 Associativity: nonassociative Type of operands: numeric Number of operands: two (binary) Context: scalar
This operator returns 1 if the left operand is numerically less than the right operand; otherwise, it returns null.
Example:
$ans = 45 < 36; if ($ans) { print("True.\n");} else { print("False. (expected)\n");}
Name: bitwise shift left Precedence: 9 Associativity: left Type of operands: numeric (integer) Number of operands: two (binary) Context: scalar
This operator shifts the operand left 1 bit in binary representation and returns the result.
Example:
$ans = 1024<<1; print("$ans (Bitwise left shift of 1024 by 1 place)\n");
Name: assignment bitwise shift left Precedence: 19 Associativity: right Type of operands: numeric (integer) Number of operands: two (binary) Context: scalar
This operator is a combination of the bitwise shift left and assignment operators. This operator is more efficient when a new value is being reassigned to the same variable, because the reference needs to be computed only one time.
Example:
$ans = 1024; $ans <<= 1; print("$ans (Bitwise left shift of 1024 by 1 place)\n");
Name: numeric less than or equal to Precedence: 11 Associativity: nonassociative Type of operands: numeric Number of operands: two (binary) Context: scalar
This operator returns 1 (true) if the left operand is numerically less than or equal to the right operand.
Example:
$ans = 345 <= 345; print("Comparing 345 <= 345 yields $ans. (expected 1 for true).\n");
Name: numeric comparison Precedence: 12 Associativity: nonassociative Type of operands: numeric Number of operands: two (binary) Context: scalar
This operator returns 0 if the two numeric operands are equal. The operator returns -1 if the left operand is less than the right operand and +1 if the left operand is greater than the right operand.
Example:
$ans = 345 <=> 347; print("Comparing 345 with 437 yields $ans. (expected -1 for less than).\n");
Name: assignment Precedence: 19 Associativity: right Type of operands: numeric, string Number of operands: two (binary) Context: scalar, list
In a scalar context, the assignment assigns the value of the right side to the variable on the left side. The assignment returns the variable on the left side.
In an array context, the assignment can assign multiple values to an array as the left operand if the right side results in a list.
Example:
$ans = 43; print("Assignment to \$ans: $ans (expected 43)\n");
Name: numeric equality Precedence: 12 Associativity: nonassociative Type of operands: numeric Number of operands: two (binary) Context: scalar
This operator returns 1 (true) if the left and right numeric operands are numerically equal; otherwise, it returns null (false).
Example:
$ans = 345 == 347; print("Comparing 345 with 347 yields +$ans+. (expected null not equal).\n");
Name: comma Precedence: 20 Associativity: left Type of operands: numeric, string Number of operands: two (binary) Context: scalar, list
This operator is an alternative to the comma operator.
Example:
$ans = (1 => 2 => 3); print("$ans (expected 3)\n");
Name: pattern binding Precedence: 6 Associativity: left Type of operands: special Number of operands: two (binary) Context: scalar
The default string matched by pattern-match operations is $_. Any other string can be bound to a pattern-matching operation using the pattern-binding operator. The left operand is a string to be searched. The right operand is a pattern-match operation (search, substitution, translation). The return value is true or false, depending on the success of the operation.
Example:
$tmp = "superduper"; if ($tmp =~ s/duper/dooper/) {print "Did do a substitute, tmp now is: $tmp\n";} else {print "Did not a substitute, tmp still is: $tmp\n";}
Name: numeric greater than Precedence: 11 Associativity: nonassociative Type of operands: numeric Number of operands: two (binary) Context: scalar
This operator returns 1 (true) if the left numeric operand is greater than the right numeric operand; otherwise, it returns null (false).
Example:
$ans = 45 > 36; if ($ans) { print("True.\n");} else { print("False. (expected)\n");}
Name: numeric greater than or equal to Precedence: 11 Associativity: nonassociative Type of operands: numeric Number of operands: two (binary) Context: scalar
This operator returns 1 (true) if the left numeric operand is greater than or equal to the right numeric operand; otherwise, it returns null (false).
Example:
$ans = 345 >= 345; print("Comparing 345 >= 345 yields $ans. (expected 1 for true).\n");
Name: bitwise shift right Precedence: 9 Associativity: left Type of operands: numeric (integer) Number of operands: two (binary) Context: scalar
This operator shifts the operand right 1 bit in binary representation and returns the result.
Example:
$ans = 1024>>1; print("$ans (Bitwise right shift of 1024 by 1 place)\n");
Name: assignment bitwise shift right Precedence: 19 Associativity: left Type of operands: numeric (integer) Number of operands: two (binary) Context: scalar
This operator is a combination of the bitwise shift right and assignment operators. This operator is more efficient when a new value is being reassigned to the same variable, because the reference needs to be computed only one time.
Example:
$ans = 1024; $ans >>= 1; print("$ans (Bitwise right shift of 1024 by 1 place)\n");
Name: conditional operator Precedence: 18 Associativity: right Type of operands: numeric, string Number of operands: three (ternary) Context: scalar, list
This operator is like a symbolic if/then/else clause. If the leftmost operand is true, the center operand is returned; otherwise, the rightmost operand is returned. Either of the operands can return scalar or list values, and these values will be returned if the context allows.
Example:
$ans = (45 == 45) ? "Equal (expected).\n" : "Not equal.\n"; print $ans;
Name: all named list operators Precedence: 1 Associativity: left Type of operands: special Number of operands: list Context: list
Several functions require a list as a parameter. The list can be written with or without the function parentheses. These list functions are in fact operators that behave like functions when their arguments are in parentheses. If they are written with parentheses, everything within the parentheses is taken as the list argument to the function, and they behave as a TERM.
When the function call is written without parentheses, the precedence is slightly more complex. The list operator has a different precedence, depending on whether the comparison is to the left of the list operator (leftward) or to the right of the list operator (rightward). The list operator has higher (or equal) precedence compared with all operators to its left. Thus, in the following example, join is evaluated before print, because print is to the left of join.
Example:
print 'Ones ', 'Twos ', join 'hoho ', 'Threes ', 'Fours ', "\n";
Name: all named list operators Precedence: 21 Associativity: nonassociative Type of operands: special Number of operands: list Context: list
Several functions require a list as a parameter. The list can be written with or without the function parentheses. These functions are in fact operators that behave like functions when their arguments are in parentheses. If they are written with parentheses, everything within the parentheses is taken as the list argument to the function, and they behave as a TERM.
When the function is written without parentheses, the precedence is slightly more complex. The list operator has a different precedence, depending on whether the comparison is to the left of the list operator (leftward) or to the right of the list operator (rightward). The list operator has lower (or equal) precedence compared with all operators to its right. Thus, in the following example, print is evaluated after join, because join is to the right of print.
Example:
print 'Ones ', 'Twos ', join 'hoho ', 'Threes ', 'Fours ', "\n";
Name: all named unary operators Precedence: 10 Associativity: nonassociative Type of operands: special Number of operands: one (unary) Context: scalar
In a similar way to list operators, NAMED unary operators can behave as a TERM by being expressed with a function syntax, with the argument placed in parentheses.
When the function is written without parentheses, the precedence of these operators is lower than arithmetic types of operators, but greater than the symbolic string and numeric comparisons. Thus, in the following example, the first int takes the result of the arithmetic division 7/2 as its argument, so 3 is printed. The second int is a term bound to 7, which returns 7 and then is divided by 2 to yield 3.5.
Example:
print 'Ones ', 'Twos ', int 7/2, (int 7)/2, ' Fours', "\n";
Name: TERMs Precedence: 1 Associativity: left Type of operands: special Number of operands: N/A Context: N/A
A TERM can be any variable, any expression enclosed in parentheses, any function with its arguments in parentheses, and also a quoted expression (using the so-called quote and quotelike operators). TERMs have the highest possible precedence-in other words, they are replaced by their return value when the entire expression is being evaluated before any other operator of lower precedence is evaluated. TERMs appear in this chapter on operators to show where they fall in the order of precedence.
Example:
print 'One ', (1, 2, 3), "(expect One 3)\n";
Name: reference Precedence: 5 Associativity: right Type of operands: one (unary) Number of operands: special Context: scalar
This operator permits the creation of references and the use of complex data types. One example is the capability to create another reference to an existing array variable.
@ans = (100, 200, 300); $ansref = \@ans; $ansref->[2] = 400; print $ans[2], " (expected 400)\n";
The capability to create a reference to a variable is new to Perl
5.
Name: bitwise exclusive or Precedence: 14 Associativity: left Type of operands: two (binary) Number of operands: numeric (integer) Context: scalar
This operator returns the result of a bitwise exclusive or on the two operands.
Example:
$ans = 456 ^ 111; print "Bitwise xor 456 & 111 is: $ans\n";
Name: assignment bitwise exclusive or Precedence: 19 Associativity: right Type of operands: numeric (integer) Number of operands: two (binary) Context: scalar
This operator is a combination of the bitwise exclusive or and assignment operators. This operator is more efficient when a new value is being reassigned to the same variable, because the reference needs to be computed only one time.
Example:
$ans = 456; $ans ^= 111; print "Bitwise xor 456 & 111 is: $ans\n";
Name: and Precedence: 23 Associativity: left Type of operands: numeric, string Number of operands: two (binary) Context: scalar
This operator is the lower-precedence version of symbolic and &&.
Example:
$ans = (1 and 3 || 0); if ($ans) { print "true (expected)\n"; } else { print "false\n"; }
This alternative to the symbolic form is new to Perl 5.
Name: string comparison Precedence: 12 Associativity: nonassociative Type of operands: string Number of operands: two (binary) Context: scalar
This operator compares two string operands and returns -1 if the first is less than the second, 0 if the operands are equal, and 1 if the first operand is greater than the second.
Example:
$ans = "abc" cmp "aba"; print("Comparing (cmp) abc with aba yields $ans (expected +1 aba > abc).\n");
Name: string equality Precedence: 12 Associativity: nonassociative Type of operands: string Number of operands: two (binary) Context: scalar
This operator tests whether two strings are equal, returning 1 (true) if they are and null (false) if they are not.
Example:
$ans = "abc" eq "abc"; print("Comparing (eq) abc with abc yields $ans (expected 1 true).\n");
Name: string greater than or equal to Precedence: 11 Associativity: nonassociative Type of operands: string Number of operands: two (binary) Context: scalar
This operator compares two strings and returns 1 (true) if the first string is greater than or equal to the second; otherwise, it returns null (false).
Example:
$ans = "abc" ge "abc"; print("Comparing (ge) abc with abc yields $ans (expected 1 true).\n");
Name: string greater than Precedence: 11 Associativity: nonassociative Type of operands: string Number of operands: two (binary) Context: scalar
This operator compares two strings and returns 1 (true) if the first is greater than the second; otherwise, it returns null (false).
Example:
$ans = "abc" gt "aba"; print("Comparing (gt) abc with aba yields $ans (expected 1 true).\n");
Name: string less than or equal to Precedence: 11 Associativity: nonassociative Type of operands: string Number of operands: two (binary) Context: scalar
This operator compares two strings and returns 1 (true) if the first is less than or equal to the second; otherwise, it returns null (false).
Example:
$ans = "abc" le "aba"; print("Comparing (le) abc with aba yields +$ans+ (expected null false).\n");
Name: string less than Precedence: 11 Associativity: nonassociative Type of operands: string Number of operands: two (binary) Context: scalar
This operator compares two strings and returns 1 (true) if the first is less than the second; otherwise, it returns null (false).
Example:
$ans = "abc" lt "aba"; print("Comparing (lt) abc with aba yields +$ans+ (expected null false).\n");
Name: string not equal to Precedence: 12 Associativity: nonassociative Type of operands: string Number of operands: two (binary) Context: scalar
This operator compares two strings and returns 1 (true) if they are not equal; otherwise, it returns null (false).
Example:
$ans = "abc" ne "aba"; print("Comparing (ne) abc with aba yields $ans (expected 1 true).\n");
Name: not Precedence: 22 Associativity: right Type of operands: numeric, string Number of operands: one (unary) Context: scalar
This operator is the lower-precedence version of symbolic not !.
Example:
$ans = not 1; print("Not 1 is +$ans+ (expected null)\n");
This alternative to the symbolic form is new to Perl 5.
Name: or Precedence: 24 Associativity: left Type of operands: numeric, string Number of operands: two (binary) Context: scalar
This operator is the lower-precedence version of symbolic or ||.
Example:
open TSTFILE, "<nofile.txt" or print "The file doesn't exist\n";
This alternative to the symbolic form is new to Perl 5.
Name: repetition Precedence: 6 Associativity: left Type of operands: string and numeric (integer) Number of operands: two (binary) Context: scalar
The first operand must be a string, and the second operand must be an integer. The operator returns a string comprising the string operand repeated the specified number of times.
Example:
print "Hello " x 5, "\n";
Name: assignment repetition Precedence: 19 Associativity: right Type of operands: string and numeric (integer) Number of operands: two (binary) Context: scalar
This operator is a combination of the repetition and assignment operators. This operator is more efficient when a new value is being reassigned to the same variable, because the reference needs to be computed only one time.
Example:
$ans = 'Hello '; $ans x= 5; print("$ans\n");
Name: exclusive or Precedence: 24 Associativity: left Type of operands: numeric, string Number of operands: two (binary) Context: scalar
This operator returns 1 (true) or null (false) as an exclusive or of the two operands: the result is true if either, but not both, of the operands is true.
Example:
for (0..1) { $a = $_; for (0..1) { $b = $_; print $a, ,' ', $b, ' ', ($a xor $b) ? 1 : 0, "\n"; } }
The xor operator is new to Perl 5.
Name: bitwise or Precedence: 14 Associativity: left Type of operands: numeric (integer) Number of operands: two (binary) Context: scalar
This operator returns an integer that is the result of a bitwise or between the two integer operands.
Example:
$ans = 2 | 1024; print("2 OR 1204 is $ans\n");
Name: assignment bitwise or Precedence: 19 Associativity: right Type of operands: numeric (integer) Number of operands: two (binary) Context: scalar
This operator is a combination of the bitwise or and assignment operators. This operator is more efficient when a new value is being reassigned to the same variable, because the reference needs to be computed only one time.
Example:
$ans = 2; $ans |= 1024; print("2 OR 1204 is $ans\n");
Name: symbolic or Precedence: 11 Associativity: left Type of operands: numeric, string Number of operands: two (binary) Context: scalar
This operator returns 1 (true) if either of the two operands is true and null (false) otherwise.
Example:
$ans = '' || 'okay'; print("null || okay is $ans (expected okay true)\n");
Name: assignment symbolic or Precedence: 19 Associativity: right Type of operands: numeric, string Number of operands: two (binary) Context: scalar
This operator is a combination of the symbolic or and assignment operators. This operator is more efficient when a new value is being reassigned to the same variable, because the reference needs to be computed only one time.
Example:
$ans = ''; $ans ||= 'okay'; print("null || okay is $ans (expected okay true)\n");
Name: bitwise not Precedence: 5 Associativity: right Type of operands: numeric (integer) Number of operands: one (unary) Context: scalar
This operator returns the bitwise negation of an integer operand. The result of this operation is sometimes known as the one's complement.
Example:
$ans = ~1000000000; print("Bitwise negation of 1000000000 is $ans\n");
This chapter lists only Perl operators; you should consult other reference sections for Perl special variables and Perl functions. (You may easily confuse some variables with some operators, so check the list of operators if the symbol that you require is not covered in this chapter.)
Be aware that all Perl functions can behave as operators and as functions. The difference is in the syntax; functions have parentheses-as in example(). Any named LIST operators/functions and NAMED unary operators/functions, including the file-test operators, are covered in the functions chapter.
For details on the other elements in the Perl reference, see the following chapters: