??
Null Coalescing
OperatorReturns the right operand if the left operand is empty / undefined (expression)
The Null Coalescing operator is a little like a conditional where the result of the operation is the first non-empty value from left to right.
An empty value is any of the following:
null
data typeOther “falsy” values such as numerical values of 0
,
boolean false
, zero length strings and strings containing
"null"
are not considered empty by the null coalescing
operator.
» $foo = $bar ?? "baz"
If $bar
is unset then the value of $foo
will be “baz”.
» $unset_variable ?? null ?? "foobar"
foobar
The following extract was taken from Wikipedia:
The null coalescing operator (called the Logical Defined-Or operator in Perl) is a binary operator that is part of the syntax for a basic conditional expression in several programming languages. While its behavior differs between implementations, the null coalescing operator generally returns the result of its left-most operand if it exists and is not null, and otherwise returns the right-most operand. This behavior allows a default value to be defined for cases where a more specific value is not available.
In contrast to the ternary conditional if operator used as
x ? x : y
, but like the binary Elvis operator used asx ?: y
, the null coalescing operator is a binary operator and thus evaluates its operands at most once, which is significant if the evaluation ofx
has side-effects.
err
):
Print a line to the stderrexpr
):
Expressions: mathematical, string comparisons, logical operatorsis-null
): Checks if a variable is null or
undefinedout
):
Print a string to the stdout with a trailing new line charactertrypipe
): Checks for non-zero exits of each function
in a pipelinetry
):
Handles non-zero exits inside a block of code&&
And
Logical Operator: Continues next operation if previous operation
passes?:
Elvis Operator:
Returns the right operand if the left operand is falsy (expression)?
stderr Pipe:
Pipes stderr from the left hand command to stdin of the right hand
command (DEPRECATED)||
Or Logical
Operator: Continues next operation only if previous operation
failsThis document was generated from gen/expr/null-coalescing-op_doc.yaml.
This site's content is rebuilt automatically from murex's source code after each merge to the master
branch. Downloadable murex binaries are also built with the website.
Last built on Wed Jan 15 23:07:50 UTC 2025 against commit b4c4296b4c429617fd41527ea0efef33c52c15ef2b64972.
Current version is 6.4.2063 (develop) which has been verified against tests cases.