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:

*Name*Unlike the special variables, no standard long form exists for the name of each operator. You must use the symbolic name.*Precedence*Each operator is categorized with a precedence number, the lowest number being the highest precedence. Higher-precedence operations are evaluated before lower-precedence operations.*Associativity*Each operator may be left, right, or nonassociative. This determines the order in which operands are evaluated.*Type of Operands*This category indicates whether the operator operates on numeric or string arguments, lists, or files.*Number of Operands*Each operator can operate on one (unary), two (binary), or three (ternary) operands. (Some operators operate on a list of operands-the list being of arbitrary length.)*Context*Each operator can expect an array or a scalar context. Some operators have separate behaviors for each context.

The following lists the precedence of the operators:

`, LIST operators (leftward)``-``! ~ - (unary) + (unary)``=~ !~``* / % x``+ (binary) - (binary) .``<< >>``NAMED unary operators``< > <= >= lt gt le ge``== != <=> eq ne cmp``&``| ^``&&``||``..``?:``= += -= *= /= %= |= &= ^= <<= >>= **= ||= &&= .= |= x=``, =>``LIST operators (rightward)``not``and``or xor`

This chapter contains detailed descriptions of these operators.

You may easily confuse some variables with some operators, so check the "Special Variables" chapter if the symbol is not described here.

Be aware that all Perl 5 (and many Perl 4) functions can behave as operators
and as functions. The difference is in the syntax; functions have parentheses-as
in `example()`. Operators which have a name rather than a symbol have
been treated as functions and are covered in the "Functions" chapter
(this includes the file-test operators `-f` and so on and the pattern
matching operators `m//` and so on.).

NameLogical negationPrecedence5AssociativityRightType of OperandsNumeric, stringNumber of OperandsOne (unary)ContextScalar

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.

$one = !1; $two = !22; $three = !0; $four = !'hello'; $five = !''; print "1=$one, 2=$two, 3=$three, 4=$four, 5=$five, \n";

NameRelational not equal toPrecedence12AssociativityNonassociativeType of OperandsStringNumber of OperandsTwo (binary)ContextScalar

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.

$tmp = "aaa "; $ans = "aaa" != $tmp; if ($ans) { print "true\n"; } else { print "false\n"; }

NameBind pattern (with negation of return value)Precedence6AssociativityLeftType of OperandsStringNumber of OperandsTwo (binary)ContextScalar 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).

$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";}

NameModulusPrecedence7AssociativityLeftType of OperandsNumericNumber of OperandsTwo (binary)ContextScalar

The operands are converted to integers, if necessary. The left side is divided by the right side, and the integer remainder is returned.

$ans = 48 % 5; print "48 mod 4 is: $ans\n";

NameModulus assignmentPrecedence18AssociativityRightType of OperandsNumericNumber of OperandsTwo (binary)ContextScalar

This operation, like all the extra assignment operations, is a way to make the evaluation of the arguments more efficient.

$ans = 48; $ans %= 5; print "48 mod 4 is: $ans\n";

NameBitwise andPrecedence13AssociativityLeftType of OperandsNumeric (integer)Number of OperandsTwo (binary)ContextScalar

This operator performs a *bitwise and* on the binary representation of
the two numeric operands; that is, each bit of the two operands are compared
with a logical and operation and the resulting bits form the result.

$ans = 456 & 111; print "Bitwise and 456 & 111 is: $ans\n";

NameSymbolic logical andPrecedence15AssociativityLeftTypeof OperandsNumeric, stringNumber of OperandsTwo (binary)ContextScalar

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.

$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");}

NameAssignment logical andPrecedence19AssociativityRightType of OperandsNumeric, stringNumber of OperandsTwo (binary)ContextScalar

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.

$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.");}

NameAssignment bitwise andPrecedence19AssociativityRightType of OperandsNumeric (integer)Number of OperandsTwo (binary)ContextScalar

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.

$ans = 456; $ans &= 111; print("Bitwise and 456 & 111 is $ans\n");

NameMultiplicationPrecedence7AssociativityLeftType of OperandsNumericNumber of OperandsTwo (binary)ContextScalar

This operator returns the numeric result of multiplying the two numeric operands.

$ans = 7 * 10; print("$ans (expected 70)\n");

NameExponentiationPrecedence4AssociativityRightType of OperandsNumericNumber of OperandsTwo (binary)ContextScalar

The operation `x**y` returns the value of `x` raised to the
power of `y`.

$ans = 2 ** 3; print ("$ans (expected 8)\n");

NameAssignment exponentiationPrecedence19AssociativityRightType of OperandsNumericNumber of OperandsTwo (binary)ContextScalar

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.

$ans = 2; $ans **= 3; print ("$ans (expected 8)\n");

NameAssignment multiplicationPrecedence19AssociativityRightType of OperandsNumericNumber of OperandsTwo (binary)ContextScalar

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.

$ans = 7; $ans *= 10; print ("$ans (expected 70)\n");

NameUnary plusPrecedence5AssociativityRightType of OperandsNumeric, stringNumber of OperandsOne (unary)ContextScalar

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.

@ans = sort +(5 + 5) * 10, -4; print("@ans (expected 100, -4)\n");

NameAdditionPrecedence8AssociativityLeftType of OperandsNumericNumber of OperandsTwo (binary)ContextScalar

This operator returns the sum of the two operands.

$ans = 15 + 5; print("$ans (expected 20)\n");

NameAutoincrementPrecedence3AssociativityNonassociativeType of OperandsNumeric, stringNumber of OperandsOne (unary)ContextScalar

In a numeric context, the autoincrement adds 1 to the operand. If the `syntax`
is used as a prefix, the value before the increment is returned. If the `syntax`
is used as a 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 (that is, the string "19" becomes
"20" automatically just as if it were an integer).

$ans = 45; print $ans, " (expected 45) "; print $ans++, " (expected 45) "; print ++$ans, " (expected 47)\n";

NameAssignment additionPrecedence19AssociativityRightType of OperandsNumericNumber of OperandsTwo (binary)ContextScalar

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.

$ans = 15; $ans += 5; print("$ans (expected 20)\n");

NameCommaPrecedence20AssociativityLeftType of OperandsNumeric, stringNumber of OperandsTwo (binary)ContextScalar, 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.

$ans = ('one', 'two', 'three'); print("$ans (expected three)\n");

NameNegationPrecedence5AssociativityRightType of OperandsNumeric, string, identifierNumber of OperandsOne (unary)ContextScalar

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.

$ans = 45; $ans = -$ans; print("$ans (expected -45)\n");

Name SubtractionPrecedence8AssociativityLeftType of OperandsNumericNumber of OperandsTwo (binary)ContextScalar

This operator returns the first operand minus the second operand.

$ans = 50 - 10; print("$ans (expected 40)\n");

NameAutodecrementPrecedence3AssociativityNonassociativeType of OperandsNumericNumber of OperandsOne (unary)ContextScalar

This operator decrements its operand. It also returns a value, but you have
the choice to return the existing value (before any decrement takes place) or
to return the new value (after the decrement takes place) by using the prefix
notation or the postfix notation. So if `$x` is `56`, -`$x`
returns `56` and `$x`- returns `55`, though in both cases
the new value of `$x` is `55`. This subtle difference is often
important when one wants to both decrement a value and perform a test (for example
with conditions in a loop).

Unlike the autoincrement operator, `++`, this operator does not operate
on strings.

$ans = 45; print $ans, " (expected 45) "; print $ans--, " (expected 45) "; print --$ans, " (expected 43)\n";

NameAssignment subtractionPrecedence19AssociativityRightType of OperandsNumericNumber of OperandsTwo (binary)ContextScalar

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.

$ans = 50; $ans -= 10; print("$ans (expected 40)\n");

NameDereferencePrecedence2AssociativityLeftType of OperandsSpecialNumber of OperandsTwo (binary)ContextScalar, 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

- A right side comprising array brackets or braces (
`[]`or`{}`), and a left side comprising a reference to an array (or hash). - A right side comprising a method name (or a variable with a method name), and a left side of either an object or a class name.

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.

@ans = (100, 200, 300); $ansref = \@ans; $ansref->[2] = 400; print $ans[2], " (expected 400)\n";

NameString concatenationPrecedence8AssociativityLeftType of OperandsStringNumber of OperandsTwo (binary)ContextScalar

This operator joins the two string operands, returning a longer string.

$ans = "jordy" . " jordy"; print $ans, " (expected jordy jordy)\n";

NameRange operatorPrecedence17AssociativityNonassociativeType of OperandsNumeric, stringNumber of OperandsTwo (binary)ContextScalar, 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 one. 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 supressed
from being output).

@ans = 1..5; print("@ans (expected 12345)\n"); open(INFILE,"<infile.tst"); while(<INFILE>) { print unless (1..5); }

NameAssignment concatenationPrecedence19AssociativityRightType of OperandsStringNumber of OperandsTwo (binary)ContextScalar

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.

$ans = "jordy"; $ans .= " jordy"; print $ans, " (expected jordy jordy)\n";

NameDivisionPrecedence7AssociativityLeftType of OperandsNumericNumber of OperandsTwo (binary)ContextScalar

This operator returns the product of the operands.

$ans = 10/2; print("$ans (expected 5)\n");

NameAssignment divisionPrecedence19AssociativityRightType of OperandsNumericNumber of OperandsTwo (binary)ContextScalar

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.

$ans = 10; $ans /= 2; print("$ans (expected 5)\n");

NameNumeric less thenPrecedence11AssociativityNonassociativeType of OperandsNumericNumber of OperandsTwo (binary)ContextScalar

This operator returns `1` if the left operand is numerically less than
the right operand; otherwise, it returns `null`.

$ans = 45 < 36; if ($ans) { print("True.\n");} else { print("False. (expected)\n");}

NameBitwise shift leftPrecedence9AssociativityLeftType of OperandsNumeric (integer)Number of OperandsTwo (binary)ContextScalar

This operator shifts the operand left one bit in binary representation and returns the result. This is usually only used when processing some form of binary data. For example, it may be that a number is a representation of a series of flags (on/off Boolean values). One can use an integer of value 0 to 16 to represent five flags as the binary representation of all possible states ranges from 00000 to 11111 (this is 0 to F in hexedecimal). When processing data in this form, it is often useful to use the binary shift operators to access individual bits. If you find the number modulus 2, this is the value of the least significant bit (1 or 0). If you shift the number to the right by one, you effectively remove the least significant bit. If you shift by one to the left, you effectively add a new least significant bit with a value of zero (doubling the actual value of the variable). See also >> for an example using such flags.

Caution: Bit shift operators depend on the implemention of storage on the machine being used and so may not be portable.

$ans = 1024<<1; print("$ans (Bitwise left shift of 1024 by 1 place)\n");

NameNumeric less than or equal toPrecedence11AssociativityNonassociativeType of OperandsNumericNumber of OperandsTwo (binary)ContextScalar

This operator returns `1` (true) if the left operand is numerically
less than or equal to the right operand.

$ans = 345 <= 345; print("Comparing 345 <= 345 yields $ans. (expected 1 for true).\n");

NameAssignment bitwise shift leftPrecedence19AssociativityRightType of OperandsNumeric (integer)Number of OperandsTwo (binary)ContextScalar

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.

$ans = 1024; $ans <<= 1; print("$ans (Bitwise left shift of 1024 by 1 place)\n");

NameNumeric comparisonPrecedence12AssociativityNonassociativeType of OperandsNumericNumber of OperandsTwo (binary)ContextScalar

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.

$ans = 345 <=> 347; print("Comparing 345 with 437 yields $ans. (expected -1 for less than).\n");

NameAssignmentPrecedence19AssociativityRightType of OperandsNumeric, stringNumber of OperandsTwo (binary)ContextScalar, list

In a scalar context, the assignment operator assigns the right operand's value to the variable specified by the left operand. The assignment operator returns the value of the variable.

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.

$ans = 43; print("Assignment to \$ans: $ans (expected 43)\n");

NameNumeric equalityPrecedence12AssociativityNonassociativeType of OperandsNumericNumber of OperandsTwo (binary)ContextScalar

This operator returns `1` (true) if the left and right numeric operands
are numerically equal; otherwise, it returns `null` (false).

$ans = 345 == 347; print("Comparing 345 with 347 yields +$ans+. (expected null not equal).\n");

NameCommaPrecedence20AssociativityLeftType of OperandsNumeric, stringNumber of OperandsTwo (binary)ContextScalar, list

This operator is an alternative to the `comma` operator.

$ans = (1 => 2 => 3); print("$ans (expected 3)\n");

NamePattern bindingPrecedence6AssociativityLeftType of OperandsSpecialNumber of OperandsTwo (binary)ContextScalar

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, and translation). The return
value is true or false, depending on the success of the operation.

$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";}

NameNumeric greater thanPrecedence11AssociativityNonassociativeType of OperandsNumericNumber of OperandsTwo (binary)ContextScalar

This operator returns `1` (true) if the left numeric operand is greater
than the right numeric operand; otherwise, it returns `null` (false).

$ans = 45 > 36; if ($ans) { print("True.\n");} else { print("False. (expected)\n");}

NameBitwise shift rightPrecedence9AssociativityLeftType of OperandsNumeric (integer)Number of OperandsTwo (binary)ContextScalar

This operator shifts the operand right one bit in binary representation and returns the result. This is usually only used when processing some form of binary data. For example, it may be that a number is a representation of a series of flags (on/off Boolean values). One can use an integer of value 0 to 16 to represent five flags as the binary representation of all possible states ranges from 00000 to 11111 (this is 0 to F in hexedecimal). When processing data in this form, it is often useful to use the binary shift operators to access individual bits. If you find the number modulus 2, this is the value of the least significant bit (1 or 0). If you shift the number to the right by one, you effectively remove the least significant bit. If you shift by one to the left, you effectively add a new least significant bit with a value of zero (doubling the actual value of the variable).

**CAUTION** Bit shift operators depend on the implemention of
storage on the machine being used and so may not be portable.

$flags = 10; # i.e. Binary 01010 list of flags for ($i=0;$i<=4;$i++) { # shift to make bit we want least significant (rightmost) # then find this modulus 2 to test this bit # (NB bit shift operations may not be portable) ($flags>>$i)%2 ? print "$i on\n" : print "$i off\n";

NameNumeric greater than or equal toPrecedence11AssociativityNonassociativeType of OperandsNumericNumber of OperandsTwo (binary)ContextScalar

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

$ans = 345 >= 345; print("Comparing 345 >= 345 yields $ans. (expected 1 for true).\n");

NameAssignment bitwise shift rightPrecedence19AssociativityLeftType of OperandsNumeric (integer)Number of OperandsTwo (binary)ContextScalar

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.

$ans = 1024; $ans >>= 1; print("$ans (Bitwise right shift of 1024 by 1 place)\n");

NameConditional operatorPrecedence18AssociativityRightType of OperandsNumeric, stringNumber of OperandsThree (ternary)ContextScalar, list

This operator is like a symbolic `if...then...else` clause. If the
left operand is true, the center operand is returned; otherwise, the right operand
is returned. Either of the operands can return scalar or list values, and these
values will be returned if the context allows.

$ans = (45 == 45) ? "Equal (expected).\n" : "Not equal.\n"; print $ans;

NameAll named list operatorsPrecedence1AssociativityLeftType of OperandsSpecialNumber of OperandsListContextList

Several functions require a list as a parameter. The list may 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`.

print 'Ones ', 'Twos ', join 'hoho ', 'Threes ', 'Fours ', "\n";

NameAll named list operatorsPrecedence21AssociativityNonassociativeType of OperandsSpecialNumber of OperandsListContextList

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

print 'Ones ', 'Twos ', join 'hoho ', 'Threes ', 'Fours ', "\n";

NameAll named unary operatorsPrecedence10AssociativityNonassociativeType of OperandsSpecialNumber of OperandsOne (unary)ContextScalar

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

print 'Ones ', 'Twos ', int 7/2, (int 7)/2, ' Fours', "\n";

NameTERMsPrecedence1AssociativityLeftType of OperandsSpecialNumber of OperandsN/AContextN/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).
`TERM`s 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. `TERM`s appear
in this chapter to show where they fall in the order of precedence.

print 'One ', (1, 2, 3), "(expect One 3)\n";

NameReferencePrecedence5AssociativityRightType of OperandsOne (unary)Number of OperandsSpecialContextScalar

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";

NameBitwise exclusive orPrecedence14AssociativityLeftType of OperandsTwo (binary)Number of OperandsNumeric (integer)ContextScalar

This operator returns the result of a bitwise exclusive or on the two operands.

$ans = 456 ^ 111; print "Bitwise xor 456 & 111 is: $ans\n";

NameAssignment bitwise exclusive orPrecedence19AssociativityRightType of OperandsNumeric (integer)Number of OperandsTwo (binary)ContextScalar

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.

$ans = 456; $ans ^= 111; print "Bitwise xor 456 & 111 is: $ans\n";

NameAndPrecedence23AssociativityLeftType of OperandsNumeric, stringNumber of OperandsTwo (binary)ContextScalar

This operator is the lower-precedence version of `&&`.

$ans = (1 and 3 || 0); if ($ans) { print "true (expected)\n"; } else { print "false\n"; }

NameString comparisonPrecedence12AssociativityNonassociativeType of OperandsStringNumber of OperandsTwo (binary)ContextScalar

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.

$ans = "abc" cmp "aba"; print("Comparing (cmp) abc with aba yields $ans (expected +1 aba > abc).\n");

NameString equalityPrecedence12AssociativityNonassociativeType of OperandsStringNumber of OperandsTwo (binary)ContextScalar

This operator tests whether two strings are equal, returning `1` (true)
if they are and `null` (false) if they are not.

$ans = "abc" eq "abc"; print("Comparing (eq) abc with abc yields $ans (expected 1 true).\n");

NameString greater than or equal toPrecedence11AssociativityNonassociativeType of OperandsStringNumber of OperandsTwo (binary)ContextScalar

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

$ans = "abc" ge "abc"; print("Comparing (ge) abc with abc yields $ans (expected 1 true).\n");

NameString greater thanPrecedence11AssociativityNonassociativeType of OperandsStringNumber of OperandsTwo (binary)ContextScalar

This operator compares two strings and returns `1` (true) if the first
is greater than the second; otherwise, it returns `null` (false).

$ans = "abc" gt "aba"; print("Comparing (gt) abc with aba yields $ans (expected 1 true).\n");

NameString less than or equal toPrecedence11AssociativityNonassociativeType of OperandsStringNumber of OperandsTwo (binary)ContextScalar

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

$ans = "abc" le "aba"; print("Comparing (le) abc with aba yields +$ans+ (expected null false).\n");

NameString less thanPrecedence11AssociativityNonassociativeType of OperandsStringNumber of OperandsTwo (binary)ContextScalar

This operator compares two strings and returns `1` (true) if the first
is less than the second; otherwise, it returns `null` (false).

$ans = "abc" lt "aba"; print("Comparing (lt) abc with aba yields +$ans+ (expected null false).\n");

NameString not equal toPrecedence12AssociativityNonassociativeType of OperandsStringNumber of OperandsTwo (binary)ContextScalar

This operator compares two strings and returns `1` (true) if they are
not equal; otherwise, it returns `null` (false).

$ans = "abc" ne "aba"; print("Comparing (ne) abc with aba yields $ans (expected 1 true).\n");

NameNotPrecedence22AssociativityRightType of OperandsNumeric, stringNumber of OperandsOne (unary)ContextScalar

This operator is the lower-precedence version of `!`.

$ans = not 1; print("Not 1 is +$ans+ (expected null)\n");

NameOrPrecedence24AssociativityLeftType of OperandsNumeric, stringNumber of OperandsTwo (binary)ContextScalar

This operator is the lower-precedence version of `||`.

open TSTFILE, "<nofile.txt" or print "The file doesn't exist\n";

NameRepetitionPrecedence6AssociativityLeftType of OperandsString and numeric (integer)Number of OperandsTwo (binary)ContextScalar

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.

print "Hello " x 5, "\n";

NameAssignment repetitionPrecedence19AssociativityRightType of OperandsString and numeric (integer)Number of OperandsTwo (binary)ContextScalar

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.

$ans = 'Hello '; $ans x= 5; print("$ans\n");

NameExclusive orPrecedence24AssociativityLeftType of OperandsNumeric, stringNumber of OperandsTwo (binary)ContextScalar

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.

for (0..1) { $a = $_; for (0..1) { $b = $_; print $a, ,' ', $b, ' ', ($a xor $b) ? 1 : 0, "\n"; } }

NameBitwise orPrecedence14AssociativityLeftType of OperandsNumeric (integer)Number of OperandsTwo (binary)ContextScalar

This operator returns an integer that is the result of a bitwise or between the two integer operands.

$ans = 2 | 1024; print("2 OR 1204 is $ans\n");

NameSymbolic orPrecedence11AssociativityLeftType of OperandsNumeric, stringNumber of OperandsTwo (binary)ContextScalar

This operator returns `1` (true) if either of the two operands is true
and `null` (false) otherwise.

$ans = '' || 'okay'; print("null || okay is $ans (expected okay true)\n");

NameAssignment bitwise orPrecedence19AssociativityRightType of OperandsNumeric (integer)Number of OperandsTwo (binary)ContextScalar

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.

$ans = 2; $ans |= 1024; print("2 OR 1204 is $ans\n");

NameAssignment symbolic orPrecedence19AssociativityRightType of OperandsNumeric, stringNumber of OperandsTwo (binary)ContextScalar

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.

$ans = ''; $ans ||= 'okay'; print("null || okay is $ans (expected okay true)\n");

NameBitwise notPrecedence5AssociativityRightType of OperandsNumeric (integer)Number of OperandsOne (unary)ContextScalar

This operator returns the bitwise negation of an integer operand. The result
of this operation is sometimes known as the *one's complement*.

$ans = ~1000000000; print("Bitwise negation of 1000000000 is $ans\n");