The following table lists all Qore operators in order of precedence, starting with the highest precedence. The lower the precedence number, the higher the precedence, therefore the operators with precedence level 1 ("{}", "[]", ".") have the highest precedence of all Qore operators. The precedence levels in Qore are roughly equal to the precedence levels of C language operators (however note the important difference with the , operator as noted below). To explicitly specify the precedence for expression evaluation, use parentheses ().
Table 2.10. Operators
Operator | Prec. | Description | Example |
---|---|---|---|
`` | 1 | `ls -l` | |
{} | 1 | hash element or object member expression dereference operator | $hash{"na" + "me"} |
. | 1 |
| |
[] | 1 | $list[1] | |
++ | 2 | ++$a, $a++ | |
-- | 2 | --$a, $a-- | |
new | 3 | new Socket() | |
background | 3 | background mainThread() | |
! | 4 | if (!($a > 10)) ... | |
~ | 5 | $var = ~$var | |
- (unary minus) | 6 | $var = -$var | |
shift | 7 | shift $list | |
pop | 7 | pop $list | |
chomp | 7 | chomp $string | |
trim | 7 | trim $string | |
map | 7 | map $closure($1), $list | |
foldl | 7 | foldl $closure($1 - $2), $list | |
foldr | 7 | foldr $closure($1 - $2), $list | |
select | 7 | select $list, $1 > 1 | |
elements | 8 | elements $list | |
keys | 8 | keys $hash | |
* | 9 | $var = $a * 10 | |
/ | 9 | $var = $a / 10 | |
% | 10 | $var = $a % 10 | |
+ | 11 | plus operator: string, binary, list, and hash concatenation, integer and float addition |
|
- | 11 | $a - 10 | |
>> | 12 | 0xff00 >> 8 | |
<< | 12 | 0xff00 << 8 | |
exists | 13 | exists $var | |
instanceof | 13 | instanceof Qore::Mutex | |
< | 14 | $a < 10 | |
> | 14 | $a > 10 | |
== | 14 | $a == 10 | |
!= | 14 | $a != 10 | |
<= | 14 | $a <= 10 | |
>= | 14 | $a >= 10 | |
<=> | 14 | $a <=> $b | |
=== | 14 | $a === 10 | |
!== | 14 | $a !== 10 | |
=~ // | 14 | $a =~ /text/ | |
!~ // | 14 | $a !~ /text/ | |
=~ s/// | 14 | $a =~ s/text/text/ | |
=~ x// | 14 | $a =~ x/(\w+):(\w+)/ | |
=~ tr | 14 | $a =~ tr/src_chars/targ_chars/ | |
& | 15 | $a & 0xff | |
| | 15 | $a | 0xff | |
^ | 15 | $a ^ 0xff | |
&& | 16 | ($a = 1) && ($b < 10) | |
|| | 16 | ($a = 1) || ($b < 10) | |
? : | 17 | $a == 2 ? "yes" : "no" | |
, | 18 | 1, 2, 3, 4, 5 | |
unshift | 19 | unshift $list, $val | |
push | 19 | push $list, $val | |
splice | 19 | splice $list, 2, 2, (1, 2, 3) | |
= | 20 | $var = 1 | |
+= | 21 | $var += 5 | |
-= | 21 | $var -= 5 | |
&= | 21 | $var &= 0x2000 | |
|= | 21 | $var |= 0x2000 | |
%= | 21 | $var %= 100 | |
*= | 21 | $var *= 10 | |
/= | 21 | $var /= 10 | |
^= | 21 | $var ^= 0x2000 | |
<<= | 21 | $var <<= 0x2000 | |
>>= | 21 | $var >>= 0x2000 |
All Qore operators perform thread-atomic actions with respect to the immediate arguments of the operator. If the operators are used in a complex expression, the entire expression is not thread-atomic unless explicit user-level locking is used. For example: $a += 5
is a thread-atomic action, but $a += $b--
is not atomic, but rather made up of two atomic actions.
When an operator taking more than one argument is used with arguments of different data types, Qore automatically converts one or both data types to a data type supported by the operator in order to evaluate the result, according to the precedence lists in the following section. That is; when an operator operates on mixed types, the types listed first in the following sections have precedence over types listed farther down in the lists. The result type will always be equal to the final operation type after any conversions due to type precedence per operator. If no type of either argument matches a supported data type for the operator, both types will be converted to the highest precedence data type for the operator and then the operator will evaluate the result. For explicit type conversion, please see the boolean(), string(), date(), int(), float(), etc functions.
The Qore comma ',' operator has a higher precendece than the '=' operator, meaning that a statement like $a = 1, 2, 3;
is possible without parentheses. However, it means that a statment like $a = func(1, 2, $b = 3, 4);
will probably not do what you want (it uses $b
as the third argument which is assigned to the list (3, 4)
); you must write $a = func(1, 2, ($b = 3), 4);
to pass 4 arguments to the function func
.
Executes the shell command in a separate process and returns the stdout as a string. To perform the same action using a Qore expression, see the backquote() function.
`shell_command`
$dirlisting = `ls -l`
Argument | Processing |
---|---|
| The shell command will be executed and the stdout is returned as a string. |
Table 2.11. Exceptions Thrown by ``
err | desc |
---|---|
| An error occurred in fork() or creating the output pipe. |
Retrieves the value of hash key or object member by evaulating an expression.
container_expression{expression}
printf("%s\n", $hash{getName()});
Table 2.12. Arguments Processed by {}
Argument | Processing |
---|---|
| This expression must evaluate to a hash or an object. If not, then the operator returns no value. |
| If the expression evaluates to a list, then a slice of the hash or object is returned as a hash containing keys given in the list that are present in the hash or object. If the key as given in the list (converted to a string if necessary) is not present in the hash or object, then it is also not present in the hash returned. |
| This expression is evaluated and converted to a string if necessary. The value of the hash key corresponding to this string will be returned. If the key or member does not exist, then no value is returned. |
Table 2.13. Exceptions Thrown by {}
err | desc |
---|---|
| Attempt to access a private member outside the class. |
Retrieves the value of a hash key or object member using a literal identifier or an expression.
container_expression.identifier
container_expression.method_identifier([arguments...])
container_expression.expression
printf("%s\n", $hash.name);
$obj.method("argument");
Table 2.14. Arguments Processed by .
Argument | Processing |
---|---|
| This expression must evaluate to a hash or an object. If not, then the operator returns no value. |
| The value of the hash key or object member corresponding to this identifier will be returned. If no such key exists, then no value is returned. In order to use hash keys that are not valid Qore identifiers, please use the {} operator. If the member is a private member and access is made outside the class, a run-time exception will be thrown. |
| The container expression must evaluate to an object, or a run-time exception is thrown. If the method does not exist in the class a run-time exception is thrown. Otherwise the method is called with any optional arguments given. |
| If the expression evaluates to a list, then a slice of the hash or object is returned as a hash containing keys given in the list that are present in the hash or object. If the key as given in the list (converted to a string if necessary) is not present in the hash or object, then it is also not present in the hash returned. |
| This expression is evaluated and converted to a string if necessary. The value of the hash key corresponding to this string will be returned. If the key or member does not exist, then no value is returned. |
Table 2.15. Exceptions Thrown by .
err | desc |
---|---|
| Attempt to access a private member outside the class. |
| Attempt to access a method not defined for this class. |
| Attempt to access a private method from outside the class. |
| Attempt to access a method of a privately-inherited base class from outside the class. |
| Attempt to execute a method on a non-object. |
Retrieves the value of a list element, the given character of a string, or the integer value of a byte for a binary object. If the index value is not valid for the argument, NOTHING is returned. Note that this operator only works as a list dereferencing operator in lvalue expressions; you cannot assign a character or a byte value to strings or binaries using this operator.
list_expression[expression]
string_expression[expression]
binary_expression[expression]
printf("%s\n", $list[2]); printf("%s\n", $str[2]); printf("0x%x\n", $binary[2]);
Table 2.16. Arguments Processed By []
Argument | Processing |
---|---|
| If the expression evaluates to a list, then the offset_expression will be used to return the given element from the list. |
| If the expression evaluates to a string, then the offset_expression will be used to return the given character from the list; note that multi-byte characters with UTF-8 are properly respected with this operator. |
| If the expression evaluates to a binary, then the offset_expression will be used to return the integer value of the byte given from the binary object. |
| The expression is evaluated and converted to an integer if necessary. Then the value of the list element given is returned (elements start at position 0). |
This operator does not throw any exceptions; if the first expression does not evaluate to either a list, string, or binary, then no value is returned.
increments an lvalue and returns the incremented value.
++lvalue
++$i;
Table 2.17. Arguments Processed By Pre-Increment ++
Argument | Processing |
---|---|
Integer | First converts the value of |
This operator does not throw any exceptions.
increments an lvalue and returns the value before the increment.
lvalue++
$i++;
Table 2.18. Arguments Processed By Post-Increment ++
Argument | Processing |
---|---|
Integer | First converts the value of |
This operator does not throw any exceptions.
decrements an lvalue and returns the decremented value.
--lvalue
--$i;
Table 2.19. Arguments Processed By Pre-Decrement --
Argument | Processing |
---|---|
Integer | First converts the value of |
This operator does not throw any exceptions.
decrements an lvalue and returns the value before the decrement.
lvalue--
$i--;
Table 2.20. Arguments Processed By Post-Decrement --
Argument | Processing |
---|---|
Integer | First converts the value of |
This operator does not throw any exceptions.
Creates an instance of a class by running the class' constructor on the new class (if any exists) and returns the new object.
new class_identifier(constructor_arguments ...)
$obj = new Qore::Mutex();
Table 2.21. Arguments Processed By new
Argument | Processing |
---|---|
| The class_identifier must be an existing class name; if so, the operator instantiates an object of this class, executes the constructor for the class (if any exists, along with any base class constructors, if applicable) on the new object, and returns the object (for constructor execution order in an inherited class, see Class Inheritance). If an exception is thrown in the constructor, the object is deleted and NOTHING is returned. |
Table 2.22. Exceptions Thrown by new
err | desc |
---|---|
depends on class/constructor | See class documentation for possible exceptions. |
Start a background thread and return the TID (thread ID).
background expression
background startThread();
Table 2.23. Arguments Processed By background
Argument | Processing |
---|---|
|
The expression given as an argument will be executed in a new thread. The TID of the new thread will be returned as the return value of the operator. |
Please note the following when using the background operator:
expressions that have no effect cannot be used with the background operator (a parse exception will be thrown)
it is illegal to make changes to a local variable anywhere in a background expression (a parse exception will be thrown)
local variables and find expressions are evaluated before the new thread is started and the result of the evaluation is used in the expression in the new thread.
it is not possible to pass local variables by reference anywhere in a background expression (a parse exception will be thrown)
Table 2.24. Exceptions Thrown by background
err | desc |
---|---|
| If the thread table is full or if the operating system returns an error while starting the thread, this exception is thrown. |
Reverses the logical sense of an expression (True becomes False and False becomes True).
!expression
if (!exists $error_code) do_something();
Table 2.25. Arguments Processed By !
Argument | Processing |
---|---|
| The expression is evaluated and converted to Boolean, if necessary. Then the value is logically reversed (True becomes False, False becomes True) |
This operator does not throw any exceptions.
The value of each bit in an integer is reversed (0 becomes 1, 1 becomes 0).
~expression
$a = ~$b;
Table 2.26. Arguments Processed By ~
Argument | Processing |
---|---|
| Performs bitwise negation on its argument (ex: 666 & ~27 = 640) |
This operator does not throw any exceptions.
Changes the sign of numeric values.
-expression
$a = -$b;
Table 2.27. Arguments Processed By Unary Minus -
Argument | Processing |
---|---|
| Gives the negative of its argument (ex: -(-1.1) = 1.1, -(1.1) = -1.1 |
| Gives the negative of its argument (ex: -(-1) = 1, -(1) = -1 |
This operator does not throw any exceptions.
Removes the first element from a list and returns that element.
shift lvalue
$a = shift $ARGV;
Table 2.28. Arguments Processed By shift
Argument | Processing |
---|---|
| Returns the first element of the list, and the list is modified by having the first element removed from the list. |
This operator does not throw any exceptions.
Removes the last element from a list and returns that element.
pop lvalue
$a = pop $list;
Table 2.29. Arguments Processed By pop
Argument | Processing |
---|---|
| Returns the last element of the list, and the list is modified, having the last element removed from the list. |
This operator does not throw any exceptions.
Removes the end-of-line marker(s) ('\n' or '\r\n') from a string, or each string element in a list, or each hash key value in a hash (if the value is a string) and returns the number of characters removed.
To perform this operation on a non-lvalue expression, see the chomp() function.
chomp lvalue
chomp $str;
Table 2.30. Arguments Processed By chomp
Argument | Processing |
---|---|
| Removes any EOL characters from a string and returns the number of characters removed. |
| Removes any EOL characters from each string element of the list passed and returns the number of characters removed. |
| Removes any EOL characters from each hash key's value (where the value is a string) and returns the number of characters removed. |
This operator does not throw any exceptions.
Removes whitespace characters from the beginning and end of a string, or each string element in a list, or each hash key value in a hash (if the value is a string) and returns the value processed (string, list, or hash).
To perform this operation on a non-lvalue expression, see the trim() function.
The following whitespace characters are removed from the beginning and end of strings: ' ', '\n', '\r', '\t', '\v' (vertical tab, ASCII 11), and '\0' (null character).
trim lvalue
trim $str;
Table 2.31. Arguments Processed By trim
Argument | Processing |
---|---|
| Removes whitespace characters from the beginning and end of a string and returns the value processed. |
| Removes whitespace characters from the beginning and end of each string element of the list passed and returns the list. |
| Removes whitespace characters from the beginning and end of each string value of the hash passed and returns the hash. |
This operator does not throw any exceptions.
Executes (or maps) an expression on a list and returns the result. An optional select expression can be given to filter elements out from the result list.
If the second argument is not a list, then map_expression
is executed on the single value and the result is returned, and any select expression is ignored.
map map_expression, list, [select_expression]
# returns (2, 4, 6)
map $1 * 2, (1, 2, 3);
Table 2.32. Arguments Processed By map
Argument | Processing |
---|---|
| The expression to map on the list; the implicit argument $1 represents the current element being processed. |
| The list to process. |
| An optional expression than can be used to filter out elements of the list before the map expression is applied; if this expression evaluates to False on an element, then the element will be skipped and the map expression will not be applied on that element. |
This operator does not throw any exceptions (however note that exceptions could be thrown by expressions executed by this operator).
Folds an operation on a list from left to right and returns the result. The result of each individual operation is used as the first argument in the foldl expression for the next element in the list. The first operation of the fold is made by executing the fold expression on the first and second elements of the list, from this point onwards, the result of each successive operation is used as the first argument for the next operation, the second argument being the next element in the list.
If the list expression does not evaluate to a list, then the evaluated argument is returned immediately with no processing by the fold expression.
foldl expression, list
# returns 5
foldl $1 - $2, (10, 4, 1);
Table 2.33. Arguments Processed By foldl
Argument | Processing |
---|---|
| The expression to fold on the list; the implicit argument $1 represents the result of the last operation (or the first element in the list when beginning the fold), and $2 represents the next element of the list. |
| The list to process. |
This operator does not throw any exceptions (however note that exceptions could be thrown by expressions executed by this operator).
Folds an operation on a list from right to left and returns the result. The result of each individual operation is used as the first argument in the foldr expression for the next element in the list in reverse order. The first operation of the right fold is made by executing the fold expression on the last and penultimate elements of the list, from this point onwards, the result of each successive operation is used as the first argument for the next operation, the second argument being the next element in the list in reverse order.
If the list expression does not evaluate to a list, then the evaluated argument is returned immediately with no processing by the fold expression.
foldr expression, list
# returns -13
foldr $1 - $2, (10, 4, 1);
Table 2.34. Arguments Processed By foldr
Argument | Processing |
---|---|
| The expression to fold on the list; the implicit argument $1 represents the result of the last operation (or the last element in the list when beginning the fold), and $2 represents the next element of the list in reverse order. |
| The list to process. |
This operator does not throw any exceptions (however note that exceptions could be thrown by expressions executed by this operator).
Selects elements from a list that meet the given criteria and returns the new list.
If the list expression does not evaluate to a list, then the select expression is evaluated using the value of the list expression as an argument, if it evalutes to true, then the value is returned, otherwise, no value is returned.
select list, expression
# returns (2, 4, 6)
select (1, 2, 3, 4, 5, 6), !($1 % 2);
Table 2.35. Arguments Processed By select
Argument | Processing |
---|---|
| The list to process. |
| The expression will be evaluated on each element of the list, the implicit argument $1 represents current element of the list; only if the expression evaluates to True will the element appear in the result list. |
This operator does not throw any exceptions (however note that exceptions could be thrown by the expression executed by this operator).
Returns the number of elements in a list, the number of keys in a hash, the number of characters (not bytes) in a string, or the number of bytes in a binary object.
elements expression
$size = elements $list;
Table 2.36. Arguments Processed By keys
Argument | Processing |
---|---|
List | Returns the number of elements in the list. |
Hash | Returns the number of keys in the hash. |
String | Returns the number of characters in the string. |
Binary | Returns the number of bytes in the binary object. |
This operator does not throw any exceptions.
Returns a list representing the keys in a hash.
keys hash_expression
foreach my $key in (keys $hash) printf("%s = %s\n", $key, $hash.$key);
Table 2.37. Arguments Processed By keys
Argument | Processing |
---|---|
| Returns a list of strings giving the keys in |
This operator does not throw any exceptions.
Multiplies two arguments.
expression1
*expression2
$value = $x * $y
Table 2.38. Argument Processing and Conversion Priorities for *
Argument | Processing |
---|---|
Float | Gives the result of multiplying its arguments. |
Integer | Gives the result of multiplying its arguments. |
This operator does not throw any exceptions.
Divides a number by another.
expression1
/expression2
$value = $x / $y;
Table 2.39. Argument Processing and Conversion Priorities for /
Argument | Processing |
---|---|
Float | Gives the result of dividing its arguments. |
Integer | Gives the result of dividing its arguments. |
This operator does not throw any exceptions.
Gives the integer remainder after division of one number by another.
expression1
%expression2
$mod = $x % $y;
Table 2.40. Arguments Processed By %
Argument | Processing |
---|---|
Integer | Gives |
This operator does not throw any exceptions.
Numeric addition, list, string, binary, and hash concatenation operator.
expression1
+expression2
$a = 1 + 2;
$string = "hello" + "-there";
$list = (1, 2) + ("three", "four", "five");
$hash = ( "key1" : 1, "key2" : 2) + ( "key3" : "three", "key4": "four");
$bin = $bin1 + $bin2;
Table 2.41. Argument Processing and Conversion Priorities for +
Argument | Processing |
---|---|
List | Gives the result of concatenating its arguments, i.e. (1, 2) + (3, 4) = (1, 2, 3, 4) |
String | Gives the result of concatenating its arguments. |
Date | Gives the result of adding date/time values (see Date/Time Arithmetic) |
Float | Gives the result of adding its arguments. |
Integer | Gives the result of adding its arguments. |
Hash | Gives the result of concatenating/merging its arguments. Any common keys will be overwritten by the values in the second hash ( |
This operator does not throw any exceptions.
With float or integer arguments, subtracts one number from another, however if the left-hand side is a hash, and the right-hand side is a string, then the hash key represented by the string will be removed from the hash.
expression1
-expression2
$num = $x - $y;
$hash = $hash - "key";
Table 2.42. Argument Processing and Conversion Priorities for -
Argument | Processing |
---|---|
Float | arithmetic subtraction: |
Integer | arithmetic subtraction: |
Hash - String | hash key deletion: |
This operator does not throw any exceptions.
Shifts bits in an integer towards zero (divides an integer by a power of 2)
expression1
>>expression2
$a = $x >> $y;
Table 2.43. Arguments Processed By >>
Argument | Processing |
---|---|
Integer | Gives the result of shifting |
This operator does not throw any exceptions.
Shifts bits in an integer towards infinity (multiplies an integer by a power of 2)
expression1
<<expression2
$a = $x << $y;
Table 2.44. Arguments Processed By <<
Argument | Processing |
---|---|
Integer | Gives the result of shifting |
This operator does not throw any exceptions.
Tests if an expression is an instance of a class or not.
expression
instanceofclass_specification
if ($obj instanceof Qore::Mutex) print("object is Mutex\n");
Table 2.45. Arguments Processed By instanceof
Argument | Processing |
---|---|
| If |
This operator does not throw any exceptions.
Tests if an expression represents a value or not.
exists expression
if (exists $a) printf("a = $n\n", $a);
Table 2.46. Arguments Processed By exists
Argument | Processing |
---|---|
| If |
This operator does not throw any exceptions.
Tests if a value is less than another; types are converted if necessary (ex: ("1" < 2) is True).
expression1
<expression2
if ($x < $y)
printf("%n is less than %n\n", $x, $y);
Table 2.47. Argument Processing and Conversion Priorities for <
Argument | Processing |
---|---|
Float | If |
Integer | If |
String | If |
Date | If |
This operator does not throw any exceptions.
Tests if a value is greater than another; types are converted if necessary (ex: ("2" > 1) is True).
expression1
>expression2
if ($x > $y)
printf("%n is less than %n\n", $x, $y);
expression1
>expression2
Table 2.48. Argument Processing and Conversion Priorities for >
Argument | Processing |
---|---|
Float | If |
Integer | If |
String | If |
Date | If |
This operator does not throw any exceptions.
Tests if a value is equal to another; types are converted if necessary (ex: ("1" == 1) is True). For absolute equals, where types must also be equal to return true, see the Absolute Equals Operator (===).
expression1
==expression2
if ($x == $y)
printf("%n is equal to %n\n", $x, $y);
Table 2.49. Argument Processing and Conversion Priorities for ==
Argument | Processing |
---|---|
String | If |
Float | If |
Integer | If |
Date | If |
List | If each element in the each list where order is relevant satisfies this operator, the operator returns True, otherwise it returns False |
Hash | If each hash has the same keys and the value of each equal key in each hash satisfies this operator, the operator returns True, otherwise it returns False |
Binary | If |
Object | If |
NULL | If both expressions are NULL, returns True, otherwise returns False |
NOTHING | If neither expression has a value, returns True, otherwise returns False |
This operator does not throw any exceptions.
Tests if a value is not equal to another; types are converted if necessary (ex: ("1" != 1) is False).
expression1
!=expression2
if ($x != $y)
printf("%n is not equal to %n\n", $x, $y);
Table 2.50. Argument Processing and Conversion Priorities for !=
Argument | Processing |
---|---|
String | If |
Float | If |
Integer | If |
Date | If |
List | If any element in the each list compared where order is relevant satisfies this operator, the operator returns True, otherwise it returns False |
Hash | If the hashes have different key sets, or the values of any equal key in each hash satisfies this operator, the operator returns True, otherwise it returns False |
Binary | If either |
Object | If |
NULL | If one expression is NULL and the other not, returns True, otherwise False. |
NOTHING | If one of the expressions has a value, returns True, otherwise False. |
This operator does not throw any exceptions.
Tests if a value is less than or equals to another value; types are converted if necessary (ex: ("1" <= 2) is True).
expression1
<=expression2
if ($x <= $y)
printf("%n is less than or equal to %n\n", $x, $y);
Table 2.51. Argument Processing and Conversion Priorities for <=
Argument | Processing |
---|---|
Float | If |
Integer | If |
String | If |
Date | If |
This operator does not throw any exceptions.
Tests if a value is greater than or equals to another value; types are converted if necessary (ex: ("2" >= 1) is True).
expression1
>=expression2
if ($x >= $y)
printf("%n is greater than or equal to %n\n", $x, $y);
Table 2.52. Argument Processing and Conversion Priorities for >=
Argument | Processing |
---|---|
Float | If |
Integer | If |
String | If |
Date | If |
This operator does not throw any exceptions.
Tests if the left-hand value is less than, equal, or greater than the right-hand value; types are converted if necessary (ex: ("1" <=> 2) returns -1).
expression1
<=>expression2
switch ($x <=> $y) { case -1: print("$x is less than $y\n"); break; case 0: print("$x is equal to $y\n"); break; case 1: print("$x is greater than $y\n"); break; }
Table 2.53. Argument Processing and Conversion Priorities for <=>
Argument | Processing |
---|---|
String | If |
Float | If |
Integer | If |
Date | If |
This operator does not throw any exceptions.
Checks two values for equality without doing any data type conversions; if the types do not match, then the result is False.
expression1
===expression2
if ($x === $y)
printf("%n is equal to %n and has the same data type as well\n", $x, $y);
Table 2.54. Arguments Processed By ===
Argument | Processing |
---|---|
All | This operator returns True only if the types and values of both sides of the operator are exactly equal, otherwise returns False. No type conversions are done. |
This operator does not throw any exceptions.
Checks two values for inequality without doing any data type conversions. If the data types do not match, then returns True.
expression1
!== expression2
if ($x !== $y)
printf("%n is not equal to %n and may not have the data type as well\n", $x, $y);
Table 2.55. Arguments Processed By !==
Argument | Processing |
---|---|
All | This operator returns True if either the types or the values of the arguments are different, otherwise it retuns False. No type conversions are done. |
This operator does not throw any exceptions.
Checks for a regular expression match; returns True if the expression matches the string, False if not. See Regular Expression Options for the meaning of the i, s, x, and m options after the regular expression.
expression
=~ [m]/regex
/[isxm]
if ($str =~ /hello/)
printf("%s contains 'hello'\n", $str);
Table 2.56. Arguments Processed By =~
Argument | Processing |
---|---|
String | This operator returns True if the regular expression in |
This operator does not throw any exceptions.
Checks for a regular expression non match; returns True if the expression does not match the string, False if it does. See Regular Expression Options for the meaning of the i, s, x, and m options after the regular expression.
expression
!~ [m]/regex
/[isxm]
if ($str !~ /hello/)
printf("%s does not contain 'hello'\n", $str);
Table 2.57. Arguments Processed By !~
Argument | Processing |
---|---|
String | This operator returns True if the regular expression in |
This operator does not throw any exceptions.
Looks for a regular expression match in a string, and, if found, substitutes the matched string with a new string. Subpattern backreferences are supported in the target string, $1=first subpattern, $2=second subpattern, etc... See Regular Expression Options for the meaning of the i, s, x, and m options after the regular expression.
lvalue
=~ s/regex_pattern
/target_string
/[isxmg]
$str =~ s/hello/goodbye/i;
$str =~ s/(\w+) +(\w+)/$2, $1/;
Table 2.58. Arguments Processed By =~ s///
Argument | Processing |
---|---|
String | This operator substitutes text in the |
This operator does not throw any exceptions.
Matches regular expression patterns (enclosed in parentheses) in a string and returns a list giving the text matched for each pattern. If the regular expression does not match, then no value (NOTHING) is returned. See Regular Expression Options for the meaning of the i, s, x, and m options after the regular expression.
string
=~ x/regex_with_patterns
/[isxm]
$list =~ x/(\w+):(\w+)/;
$list =~ x/(.*)\.(.*)/;
Table 2.59. Arguments Processed By =~ x//
Argument | Processing |
---|---|
String | This operator extracts strings from the |
This operator does not throw any exceptions.
Makes character substitutions in an lvalue; character ranges can also be used.
lvalue
=~ tr/source_chars
/target_chars
/
$str =~ tr/a-z/A-Z/;
Table 2.60. Arguments Processed By =~ tr//
Argument | Processing |
---|---|
String | This operator substitutes characters in the |
This operator does not throw any exceptions.
Performs a bitwise (binary) AND operation on two integers.
expression1
&expression2
$a = $x & $y;
Table 2.61. Arguments Processed By &
Argument | Processing |
---|---|
Integer | Gives the result of the binary (bitwise) AND operation between |
This operator does not throw any exceptions.
Performs a bitwise (binary) OR operation on two integers.
expression1
|expression2
$a = $x | $y;
Table 2.62. Arguments Processed By |
Argument | Processing |
---|---|
Integer | Gives the result of the binary (bitwise) OR operation between |
This operator does not throw any exceptions.
Performs a bitwise (binary) XOR operation on two integers.
expression1
^expression2
$a = $x ^ $y;
Table 2.63. Arguments Processed By ^
Argument | Processing |
---|---|
Integer | Gives the result of the binary (bitwise) EXCLUSIVE OR operation between |
This operator does not throw any exceptions.
Checks to see if two expressions are True.
expression1
&&expression2
if ($x && $y)
printf("%n and %n are both True\n", $x, $y);
Table 2.64. Arguments Processed By &&
Argument | Processing |
---|---|
Integer | Returns True if both expressions are True, False if otherwise. Logical short-circuiting is implemented; if |
This operator does not throw any exceptions.
Returns True if either of the arguments are True.
expression1
||expression2
if ($x || $y)
printf("either %n or %n or both are True\n", $x, $y);
Table 2.65. Arguments Processed By ||
Argument | Processing |
---|---|
Integer | Returns True if either or both expressions evaluate to True, False if otherwise. Logical short-circuiting is implemented; if |
This operator does not throw any exceptions.
Evaluates and returns the value of one of two expressions depending on the value of a conditional expression.
expression ? if_true_expression : if_false_expression
$a = ($z > 100 ? "Big" : "Small");
Table 2.66. Arguments Processed By ? :
Argument | Processing |
---|---|
All | If |
This operator does not throw any exceptions.
Makes a list from more than one element.
expression1, expression2
$a = 1, 2, "three";
Table 2.67. Arguments Processed By ,
Argument | Processing |
---|---|
All | The comma operator builds lists of arguments. |
This operator does not throw any exceptions.
Inserts an element into the first position of a list and moves all other elements up one position.
unshift lvalue, expression
unshift $list, "one";
Table 2.68. Arguments Processed By unshift
Argument | Processing |
---|---|
All | Inserts the value of |
Adds one element to the end of a list.
push lvalue, expression
push $list, "last";
Table 2.69. Arguments Processed By push
Argument | Processing |
---|---|
All | Appends the value of the expression as the last element in the list given by |
Removes and optionally inserts elements in lists and strings.
splice lvalue, offset_expression, [length_expression, [substitution_expression]]
splice $list, 2, 2;
splice $string, 2, 2, "-text-";
Works on either strings or lists in a similar way; removes elements from a list or characters from a string and optionally inserts new ones. If no length_expression
is given, splice removes all elements/characters from the list or string starting at offset_expression
) (list and string offsets begin at 0). Otherwise, a number of elements/characters equal to length_expression
is removed (or up to the end of the list/string if applicable). If substitution_expression
is present, then the removed elements/characters are substituted with the elements/string given by this expression.
Note that string splice takes character offsets, which may not be the same as byte offsets for multi-byte character encodings, such as UTF-8
Table 2.70. Arguments Processed By splice
Argument | Processing |
---|---|
| If the lvalue is a list, list elements are processed, otherwise, if it is a string, characters in the string are processed. For any other data type, no action is taken. |
| The start element/character position for removing elements/characters from the list or string. |
| The number of elements/characters to remove. If this expression is not present, then all elements/characters from the offset to the end of the list/string are removed. If this expression is present and evaluates to 0, no characters/elements are removed. |
| For list splice, an optional element or list to substitute for the removed elements (to insert a list in a single element's position, make sure that the list to be inserted is the first and only element of another list used as the argument in this position; in other words, pass a list within a single-element list). For string splice, an optional string to substitute for the removed characters. |
Assigns a value to an lvalue.
lvalue
=expression
$a = 1;
Increments and concatentates an lvalue with the value of an expression depending on the data type of the lvalue, unless the lvalue is NOTHING, in which case this operator acts like the assignment operator (simply assigns the value of the right hand side to the lvalue).
lvalue
+=expression
$a += 10;
Table 2.72. Arguments Processed By +=
Argument | Processing |
---|---|
| the expression will be evaluated and concatenated to the lvalue. If expression is a list, the lists will be concatenated, to ensure adding a single element to a list, use the push operator (see Push Operator). |
| the expression will be evaluated, and, if it is a hash or object, then it's members will be added to the lvalue, any duplicate elements in the lvalue will be overridden by elements in the expression. |
| the expression will be evaluated and converted to a string if necessary and concatenated to the lvalue. |
| the expression will be evaluated and converted to a float if necessary and added to the lvalue. |
| the expression will be evaluated and converted to a string if necessary and added to the lvalue. |
| the lvalue will be assigned to the value of |
| the lvalue's type will be converted to an integer, and the expression will be evaluated and converted to an integer if necessary, and then the result will be added to the lvalue. |
For a float or integer argument, decrements the value of an lvalue by the value of an expression. However if the lvalue is a hash and the expression is a string, removes the key represented by the string from the hash.
lvalue
-=expression
$a -= 10;
Table 2.73. Arguments Processed By -=
Argument | Processing |
---|---|
| the expression will be evaluated and converted to a float if necessary and subtracted from the lvalue |
| the hash key represented by |
| the lvalue will be assigned to - |
| the lvalue's type will be converted to an integer, and the expression will be evaluated and converted to an integer if necessary, and then the result will be subtracted from the lvalue |
Performs a bitwise (binary) AND operation on an lvalue using the value of an expression.
lvalue
&=expression
$a &= 0xfe;
Table 2.74. Arguments Processed By &=
Argument | Processing |
---|---|
All | the lvalue's type will be converted to an integer if necessary, and the expression will be evaluated and converted to an integer as well if necessary, and then the result will be binary and'ed to the lvalue |
Performs a bitwise (binary) OR operation on an lvalue using the value of an expression.
lvalue
|=expression
$a |= 0xba;
Table 2.75. Arguments Processed By |=
Argument | Processing |
---|---|
All | the lvalue's type will be converted to an integer if necessary, and the expression will be evaluated and converted to an integer as well if necessary, and then the result will be binary or'ed to the lvalue |
Performs a modula calculation on an lvalue using the value of an expression.
lvalue
%=expression
$a %= 100;
Table 2.76. Arguments Processed By %=
Argument | Processing |
---|---|
All | the lvalue's type will be converted to an integer if necessary, and the expression will be evaluated and converted to an integer as well if necessary, and then the result will be used to divide the lvalue's value and the remainder will be saved to the lvalue |
Performs a multiplication operation on an lvalue using the value of an expression.
lvalue
*=expression
$a *= 10;
Table 2.77. Arguments Processed By *=
Argument | Processing |
---|---|
All | If either side of the operator is a float, the result will be a float as well. Otherwise the result is an integer value. The expression will be evaluated and multiplied by the lvalue, and the result will be saved to the lvalue. |
Performs a division operation on an lvalue using the value of an expression.
lvalue
/=expression
$a /= 10;
Table 2.78. Arguments Processed By *=
Argument | Processing |
---|---|
All | If either side of the operator is a float, the result will be a float as well. Otherwise the result is an integer value. The expression will be evaluated and used to divide the lvalue, and the result will be saved to the lvalue. |
Table 2.79. Exceptions Thrown by /*
err | desc |
---|---|
| If the divisor expression evaluates to zero, this exception is thrown. |
Performs an exclusive-or operation on an lvalue using the value of an expression.
lvalue
^=expression
$a ^= 0xf9034ba7;
Table 2.80. Arguments Processed By ^=
Argument | Processing |
---|---|
All | Values are converted to integers if necessary. The expression will be evaluated and exclusive-or'ed with the lvalue, and the result will be saved to the lvalue. |
Performs a shift-left operation on an lvalue using the value of an expression.
lvalue
<<=expression
$a <<= 3;
Table 2.81. Arguments Processed By <<=
Argument | Processing |
---|---|
All | Values are converted to integers if necessary. The expression will be evaluated and this value will determine how many bits the lvalue will be shifted left. The result will be saved to the lvalue. |
Performs a shift-right operation on an lvalue using the value of an expression.
lvalue
>>=expression
$a >>= 3;
Table 2.82. Arguments Processed By >>=
Argument | Processing |
---|---|
All | Values are converted to integers if necessary. The expression will be evaluated and this value will determine how many bits the lvalue will be shifted right. The result will be saved to the lvalue. |