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.).
Name Logical negation
Precedence 5
Associativity Right
Type of Operands Numeric, string
Number of Operands One (unary)
Context Scalar
Definition
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";
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.
$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).
$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
Definition
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";
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.
$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; 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";
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.
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.
$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.
$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.
$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.
$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.
$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.
$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.
@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.
$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 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";
Name Assignment addition
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.
$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.
$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.
$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.
$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. 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";
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.
$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
- 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";
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.
$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 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);
}
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.
$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.
$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.
$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.
$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 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");
Compliance
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.
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.
$ans = 1024;
$ans <<= 1;
print("$ans (Bitwise left shift of 1024 by 1 place)\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.
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 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");
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).
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.
$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, 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";}
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).
$ans = 45 > 36;
if ($ans)
{ print("True.\n");}
else
{ print("False. (expected)\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 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.
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).
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.
$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 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.
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 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";
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.
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.
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 to show where they fall in the order of precedence.
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";
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.
$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.
$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 &&.
$ans = (1 and 3 || 0);
if ($ans)
{ print "true (expected)\n"; }
else
{ print "false\n"; }
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.
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.
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).
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).
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).
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).
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).
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 !.
$ans = not 1;
print("Not 1 is +$ans+ (expected null)\n");
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 ||.
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.
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.
$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.
for (0..1) {
$a = $_;
for (0..1) {
$b = $_;
print $a, ,' ', $b, ' ', ($a xor $b) ? 1 : 0, "\n";
}
}
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.
$ans = 2 | 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.
$ans = '' || 'okay';
print("null || okay is $ans (expected okay true)\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.
$ans = 2;
$ans |= 1024;
print("2 OR 1204 is $ans\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.
$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.
$ans = ~1000000000;
print("Bitwise negation of 1000000000 is $ans\n");