Skip to main content.

Web Based Programming Tutorials

Homepage | Forum - Join the forum to discuss anything related to programming! | Programming Resources

Perl Quick Reference

Operators

Operators


CONTENTS



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 lists the precedence of the operators:

  1. , LIST operators (leftward)
  2. -
  3. ! ~ - (unary) + (unary)
  4. =~ !~
  5. * / % x
  6. + (binary) - (binary) .
  7. << >>
  8. NAMED unary operators
  9. < > <= >= lt gt le ge
  10. == != <=> eq ne cmp
  11. &
  12. | ^
  13. &&
  14. ||
  15. ..
  16. ?:
  17. = += -= *= /= %= |= &= ^= <<= >>= **= ||= &&= .= |= x=
  18. , =>
  19. LIST operators (rightward)
  20. not
  21. and
  22. 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.).


!

Compliance

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.

Example

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

!=

Compliance

Name  Relational not equal to
Precedence  12
Associativity  Nonassociative
Type of Operands  String
Number of Operands  Two (binary)
Context  Scalar

Definition

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

!~

Compliance

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:  =~

Definition

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

%

Compliance

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.

Example

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

%=

Compliance

Name  Modulus assignment
Precedence  18
Associativity  Right
Type of Operands  Numeric
Number of Operands  Two (binary)
Context  Scalar

Definition

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

&

Compliance

Name  Bitwise and
Precedence  13
Associativity  Left
Type of Operands  Numeric (integer)
Number of Operands  Two (binary)
Context  Scalar

Definition

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.

Example

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

&&

Compliance

Name  Symbolic logical and
Precedence  15
Associativity  Left
Type of Operands  Numeric, string
Number of Operands  Two (binary)
Context  Scalar

Definition

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

&&=

Compliance

Name  Assignment logical and
Precedence  19
Associativity  Right
Type of Operands  Numeric, string
Number of Operands  Two (binary)
Context  Scalar

Definition

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

&=

Compliance

Name  Assignment bitwise and
Precedence  19
Associativity  Right
Type of Operands  Numeric (integer)
Number of Operands  Two (binary)
Context  Scalar

Definition

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

*

Compliance

Name  Multiplication
Precedence  7
Associativity  Left
Type of Operands  Numeric
Number of Operands  Two (binary)
Context  Scalar

Definition

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

Example

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

**

Compliance

Name  Exponentiation
Precedence  4
Associativity  Right
Type of Operands  Numeric
Number of Operands  Two (binary)
Context  Scalar

Definition

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

Example

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

**=

Compliance

Name  Assignment exponentiation
Precedence  19
Associativity  Right
Type of Operands  Numeric
Number of Operands  Two (binary)
Context  Scalar

Definition

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

*=

Compliance

Name  Assignment multiplication
Precedence  19
Associativity  Right
Type of Operands  Numeric
Number of Operands  Two (binary)
Context  Scalar

Definition

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

+ (Unary)

Compliance

Name  Unary plus
Precedence  5
Associativity  Right
Type of Operands  Numeric, string
Number of Operands  One (unary)
Context  Scalar

Definition

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

+ (Binary)

Compliance

Name  Addition
Precedence  8
Associativity  Left
Type of Operands  Numeric
Number of Operands  Two (binary)
Context  Scalar

Definition

This operator returns the sum of the two operands.

Example

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

++

Compliance

Name  Autoincrement
Precedence  3
Associativity  Nonassociative
Type of Operands  Numeric, string
Number of Operands  One (unary)
Context  Scalar

Definition

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

Example

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

+=

Compliance

Name  Assignment addition
Precedence  19
Associativity  Right
Type of Operands  Numeric
Number of Operands  Two (binary)
Context  Scalar

Definition

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

,

Compliance

Name  Comma
Precedence  20
Associativity  Left
Type of Operands  Numeric, string
Number of Operands  Two (binary)
Context  Scalar, list

Definition

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

- (Unary)

Compliance

Name  Negation
Precedence  5
Associativity  Right
Type of Operands  Numeric, string, identifier
Number of Operands  One (unary)
Context  Scalar

Definition

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

- (Binary)

Compliance

Name  Subtraction
Precedence  8
Associativity  Left
Type of Operands  Numeric
Number of Operands  Two (binary)
Context  Scalar

Definition

This operator returns the first operand minus the second operand.

Example

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

--

Compliance

Name  Autodecrement
Precedence  3
Associativity  Nonassociative
Type of Operands  Numeric
Number of Operands  One (unary)
Context  Scalar

Definition

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.

Example

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

-=

Compliance

Name  Assignment subtraction
Precedence  19
Associativity  Right
Type of Operands  Numeric
Number of Operands  Two (binary)
Context  Scalar

Definition

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

->

Compliance

Name  Dereference
Precedence  2
Associativity  Left
Type of Operands  Special
Number of Operands  Two (binary)
Context  Scalar, array

Definition

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

.

Compliance

Name  String concatenation
Precedence  8
Associativity  Left
Type of Operands  String
Number of Operands  Two (binary)
Context  Scalar

Definition

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

Example

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

..

Compliance

Name  Range operator
Precedence  17
Associativity  Nonassociative
Type of Operands  Numeric, string
Number of Operands  Two (binary)
Context  Scalar, list

Definition

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

Example

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

.=

Compliance

Name  Assignment concatenation
Precedence  19
Associativity  Right
Type of Operands  String
Number of Operands  Two (binary)
Context  Scalar

Definition

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

/

Compliance

Name  Division
Precedence  7
Associativity  Left
Type of Operands  Numeric
Number of Operands  Two (binary)
Context  Scalar

Definition

This operator returns the product of the operands.

Example

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

/=

Compliance

Name  Assignment division
Precedence  19
Associativity  Right
Type of Operands  Numeric
Number of Operands  Two (binary)
Context  Scalar

Definition

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

<

Compliance

Name  Numeric less then
Precedence  11
Associativity  Nonassociative
Type of Operands  Numeric
Number of Operands  Two (binary)
Context  Scalar

Definition

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

<<

Compliance

Name  Bitwise shift left
Precedence  9
Associativity  Left
Type of Operands  Numeric (integer)
Number of Operands  Two (binary)
Context  Scalar

Definition

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.

Example

$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

Definition

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

<<=

Compliance

Name  Assignment bitwise shift left
Precedence  19
Associativity  Right
Type of Operands  Numeric (integer)
Number of Operands  Two (binary)
Context  Scalar

Definition

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

<=>

Compliance

Name  Numeric comparison
Precedence  12
Associativity  Nonassociative
Type of Operands  Numeric
Number of Operands  Two (binary)
Context  Scalar

Definition

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

=

Compliance

Name  Assignment
Precedence  19
Associativity  Right
Type of Operands  Numeric, string
Number of Operands  Two (binary)
Context  Scalar, list

Definition

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.

Example

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

==

Compliance

Name  Numeric equality
Precedence  12
Associativity  Nonassociative
Type of Operands  Numeric
Number of Operands  Two (binary)
Context  Scalar

Definition

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

=>

Compliance

Name  Comma
Precedence  20
Associativity  Left
Type of Operands  Numeric, string
Number of Operands  Two (binary)
Context  Scalar, list

Definition

This operator is an alternative to the comma operator.

Example

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

=~

Compliance

Name  Pattern binding
Precedence  6
Associativity  Left
Type of Operands  Special
Number of Operands  Two (binary)
Context  Scalar

Definition

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.

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

>

Compliance

Name  Numeric greater than
Precedence  11
Associativity           Nonassociative
Type of Operands  Numeric
Number of Operands      Two (binary)
Context  Scalar

Definition

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

>>

Compliance

Name  Bitwise shift right
Precedence  9
Associativity  Left
Type of Operands  Numeric (integer)
Number of Operands  Two (binary)
Context  Scalar

Definition

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.

Example

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

>=

Compliance

Name  Numeric greater than or equal to
Precedence  11
Associativity  Nonassociative
Type of Operands  Numeric
Number of Operands      Two (binary)
Context   Scalar

Definition

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

>>=

Compliance

Name  Assignment bitwise shift right
Precedence  19
Associativity  Left
Type of Operands  Numeric (integer)
Number of Operands  Two (binary)
Context  Scalar

Definition

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

?:

Compliance

Name  Conditional operator
Precedence  18
Associativity  Right
Type of Operands  Numeric, string
Number of Operands  Three (ternary)
Context  Scalar, list

Definition

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.

Example

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

LIST Operators (Leftward)

Compliance

Name  All named list operators
Precedence  1
Associativity  Left
Type of Operands  Special
Number of Operands  List
Context  List

Definition

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.

Example

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

LIST Operators (Rightward)

Compliance

Name  All named list operators
Precedence  21
Associativity  Nonassociative
Type of Operands  Special
Number of Operands  List
Context  List

Definition

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

NAMED Unary Operators

Compliance

Name  All named unary operators
Precedence  10
Associativity  Nonassociative
Type of Operands  Special
Number of Operands  One (unary)
Context  Scalar

Definition

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

TERMs

Compliance

Name  TERMs
Precedence  1
Associativity  Left
Type of Operands  Special
Number of Operands  N/A
Context  N/A

Definition

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.

Example

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

\

Compliance

Name  Reference
Precedence  5
Associativity  Right
Type of Operands  One (unary)
Number of Operands  Special
Context  Scalar

Definition

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

^

Compliance

Name  Bitwise exclusive or
Precedence  14
Associativity  Left
Type of Operands  Two (binary)
Number of Operands  Numeric (integer)
Context  Scalar

Definition

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

^=

Compliance

Name  Assignment bitwise exclusive or
Precedence  19
Associativity  Right
Type of Operands  Numeric (integer)
Number of Operands  Two (binary)
Context  Scalar

Definition

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

and

Compliance

Name  And
Precedence  23
Associativity  Left
Type of Operands  Numeric, string
Number of Operands  Two (binary)
Context  Scalar

Definition

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

Example

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

cmp

Compliance

Name  String comparison
Precedence  12
Associativity  Nonassociative
Type of Operands  String
Number of Operands  Two (binary)
Context  Scalar

Definition

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

eq

Compliance

Name  String equality
Precedence  12
Associativity  Nonassociative
Type of Operands  String
Number of Operands  Two (binary)
Context  Scalar

Definition

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

ge

Compliance

Name  String greater than or equal to
Precedence  11
Associativity  Nonassociative
Type of Operands  String
Number of Operands  Two (binary)
Context  Scalar

Definition

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

gt

Compliance

Name  String greater than
Precedence  11
Associativity  Nonassociative
Type of Operands  String
Number of Operands  Two (binary)
Context  Scalar

Definition

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

le

Compliance

Name  String less than or equal to
Precedence  11
Associativity  Nonassociative
Type of Operands  String
Number of Operands  Two (binary)
Context  Scalar

Definition

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

lt

Compliance

Name  String less than
Precedence  11
Associativity  Nonassociative
Type of Operands  String
Number of Operands  Two (binary)
Context  Scalar

Definition

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

ne

Compliance

Name  String not equal to
Precedence  12
Associativity  Nonassociative
Type of Operands  String
Number of Operands  Two (binary)
Context  Scalar

Definition

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

not

Compliance

Name  Not
Precedence  22
Associativity  Right
Type of Operands  Numeric, string
Number of Operands  One (unary)
Context  Scalar

Definition

This operator is the lower-precedence version of !.

Example

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

or

Compliance

Name  Or
Precedence  24
Associativity  Left
Type of Operands  Numeric, string
Number of Operands  Two (binary)
Context  Scalar

Definition

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

Example

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

x

Compliance

Name  Repetition
Precedence  6
Associativity  Left
Type of Operands  String and numeric (integer)
Number of Operands  Two (binary)
Context  Scalar

Definition

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

x=

Compliance

Name  Assignment repetition
Precedence  19
Associativity  Right
Type of Operands  String and numeric (integer)
Number of Operands  Two (binary)
Context  Scalar

Definition

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

xor

Compliance

Name  Exclusive or
Precedence  24
Associativity            Left
Type of Operands  Numeric, string
Number of Operands  Two (binary)
Context  Scalar

Definition

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

|

Compliance

Name  Bitwise or
Precedence  14
Associativity  Left
Type of Operands  Numeric (integer)
Number of Operands  Two (binary)
Context  Scalar

Definition

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

||

Compliance

Name  Symbolic or
Precedence  11
Associativity  Left
Type of Operands  Numeric, string
Number of Operands  Two (binary)
Context  Scalar

Definition

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

|=

Compliance

Name  Assignment bitwise or
Precedence  19
Associativity  Right
Type of Operands  Numeric (integer)
Number of Operands  Two (binary)
Context  Scalar

Definition

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

||=

Compliance

Name  Assignment symbolic or
Precedence  19
Associativity  Right
Type of Operands  Numeric, string
Number of Operands  Two (binary)
Context  Scalar

Definition

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

~

Compliance

Name  Bitwise not
Precedence  5
Associativity  Right
Type of Operands  Numeric (integer)
Number of Operands  One (unary)
Context  Scalar

Definition

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