Skip to main content

?? Null Coalescing Operator

Laurence MorganAbout 2 min

?? Null Coalescing Operator

Returns the right operand if the left operand is empty / undefined (expression)

Description

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:

  • an unset / undefined variable
  • any value with a null data type

Other "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.

Examples

Assign a variable with a default value:

» $foo = $bar ?? "baz"

If $bar is unset then the value of $foo will be "baz".

Multiple operators:

» $unset_variable ?? null ?? "foobar"
foobar

Detail

The following extract was taken from Wikipediaopen in new window:

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 as x ?: y, the null coalescing operator is a binary operator and thus evaluates its operands at most once, which is significant if the evaluation of x has side-effects.

See Also

  • Pipeline: Overview of what a "pipeline" is
  • Schedulers: Overview of the different schedulers (or 'run modes') in Murex
  • && 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)
  • err: Print a line to the STDERR
  • expr: Expressions: mathematical, string comparisons, logical operators
  • is-null: Checks if a variable is null or undefined
  • out: Print a string to the STDOUT with a trailing new line character
  • try: Handles non-zero exits inside a block of code
  • trypipe: Checks for non-zero exits of each function in a pipeline
  • || Or Logical Operator: Continues next operation only if previous operation fails
  • null: null function. Similar to /dev/null

This document was generated from gen/expr/null_coalescing_op_doc.yamlopen in new window.

Last update:
Contributors: Laurence Morgan