pt directories are: @INC\n";



Chapter 9 -- Perl Operators

Chapter 9

Perl Operators


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

This chapter contains detailed descriptions of these operators.

You may easily confuse some variables with some operators, so check Chapter 8, "Perl Special Variables," 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 so are covered in Chapter 10, "Perl Functions," (this includes the file-test operators -f etc. and the pattern matching operators m// etc.).


!

Compliance
    
Syntax
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
    
Syntax
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
    
Syntax
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

    

Syntax
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
    
Syntax
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
    
Syntax
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; for example, 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
    
Syntax
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
    
Syntax
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
    
Syntax
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
    
Syntax
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
    
Syntax
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
    
Syntax
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
    
Syntax
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
    
Syntax
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
    
Syntax
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
    
Syntax
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 prefix, the value before the increment is returned. If the syntax is postfix, the value after the increment is returned.

With a string operand (that has never been used in a numeric context), the autoincrement has a "magic" behavior. If the string is an alphanumeric expression, such as /^[a-zA-Z]*[0-9]*$/, the increment is carried out on the string, including a carry (i.e. 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
    
Syntax
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
    
Syntax
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
    
Syntax
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
    
Syntax
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
    
Syntax
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
    
Syntax
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
  
Syntax
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
    
Syntax
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
    
Syntax
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 surpressed from being output).

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

.=

Compliance
    
Syntax
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
    
Syntax
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
    
Syntax
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
    
Syntax
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
    
Syntax
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 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
    
Syntax
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
    
Syntax
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
    
Syntax
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
    
Syntax
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
    
Syntax
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
    
Syntax
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
    
Syntax
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
    
Syntax
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
     rint("False. (expected)\n");}

>>

Compliance
    
Syntax
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 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
    
Syntax
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
    
Syntax
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
    
Syntax
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 leftmost operand is true, the center operand is returned; otherwise, the rightmost operand is returned. Either of the operands can return scalar or list values, and these values will be returned if the context allows.

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

List Operators (Leftward)

Compliance
    
Syntax
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
    
Syntax
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
    
Syntax
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
    
Syntax
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 on operators to show where they fall in the order of precedence.

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

\

Compliance
  
Syntax
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
    
Syntax
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
    
Syntax
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
  
Syntax
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 symbolic and &&.

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

cmp

Compliance
    
Syntax
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
    
Syntax
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
    
Syntax
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
    
Syntax
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
    
Syntax
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
    
Syntax
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
    
Syntax
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
    
Syntax
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 symbolic not !.

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

or

Compliance
  
Syntax
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 symbolic or ||.

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

x

Compliance
    
Syntax
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
    
Syntax
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
  
Syntax
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) {
      = $_;
     r (0..1) {
          b = $_;
          rint $a, ,' ', $b, ' ', ($a xor $b) ? 1 : 0, "\n";
          
     

|

Compliance
    
Syntax
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
    
Syntax
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
    
Syntax
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
    
Syntax
Name:                   Assignment symbolic or
Precedence:             19
Associativity:          Right
Type of Operands:       Numeric, string
Number of Operands:  &nbs