github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/docs/parser/null-coalescing.md (about) 1 # `??` Null Coalescing Operator 2 3 > Returns the right operand if the left operand is empty / undefined (expression) 4 5 ## Description 6 7 The Null Coalescing operator is a little like a conditional where the result of the 8 operation is the first non-empty value from left to right. 9 10 An empty value is any of the following: 11 12 * an unset / undefined variable 13 * any value with a `null` data type 14 15 Other "falsy" values such as numerical values of `0`, boolean `false`, zero 16 length strings and strings containing `"null"` are not considered empty by the 17 null coalescing operator. 18 19 20 21 ## Examples 22 23 **Assign a variable with a default value:** 24 25 ``` 26 » $foo = $bar ?? "baz" 27 ``` 28 29 If `$bar` is unset then the value of `$foo` will be **"baz"**. 30 31 **Multiple operators:** 32 33 ``` 34 » $unset_variable ?? null ?? "foobar" 35 foobar 36 ``` 37 38 ## Detail 39 40 The following extract was taken from [Wikipedia](https://en.wikipedia.org/wiki/Null_coalescing_operator): 41 42 > The null coalescing operator (called the Logical Defined-Or operator in Perl) 43 > is a binary operator that is part of the syntax for a basic conditional 44 > expression in several programming languages. While its behavior differs 45 > between implementations, the null coalescing operator generally returns the 46 > result of its left-most operand if it exists and is not null, and otherwise 47 > returns the right-most operand. This behavior allows a default value to be 48 > defined for cases where a more specific value is not available. 49 > 50 > In contrast to the ternary conditional if operator used as `x ? x : y`, but 51 > like the binary Elvis operator used as `x ?: y`, the null coalescing operator 52 > is a binary operator and thus evaluates its operands at most once, which is 53 > significant if the evaluation of `x` has side-effects. 54 55 ## See Also 56 57 * [Pipeline](../user-guide/pipeline.md): 58 Overview of what a "pipeline" is 59 * [Schedulers](../user-guide/schedulers.md): 60 Overview of the different schedulers (or 'run modes') in Murex 61 * [`&&` And Logical Operator](../parser/logical-and.md): 62 Continues next operation if previous operation passes 63 * [`?:` Elvis Operator](../parser/elvis.md): 64 Returns the right operand if the left operand is falsy (expression) 65 * [`?` STDERR Pipe](../parser/pipe-err.md): 66 Pipes STDERR from the left hand command to STDIN of the right hand command (DEPRECATED) 67 * [`err`](../commands/err.md): 68 Print a line to the STDERR 69 * [`expr`](../commands/expr.md): 70 Expressions: mathematical, string comparisons, logical operators 71 * [`is-null`](../commands/is-null.md): 72 Checks if a variable is null or undefined 73 * [`out`](../commands/out.md): 74 Print a string to the STDOUT with a trailing new line character 75 * [`try`](../commands/try.md): 76 Handles non-zero exits inside a block of code 77 * [`trypipe`](../commands/trypipe.md): 78 Checks for non-zero exits of each function in a pipeline 79 * [`||` Or Logical Operator](../parser/logical-or.md): 80 Continues next operation only if previous operation fails 81 * [null](../commands/devnull.md): 82 null function. Similar to /dev/null 83 84 <hr/> 85 86 This document was generated from [gen/expr/null_coalescing_op_doc.yaml](https://github.com/lmorg/murex/blob/master/gen/expr/null_coalescing_op_doc.yaml).