I started to really like C#'s ?? operator. And I am quite used to the fact, that where there is something handy in some language, it's most probably in Perl too.
However, I cannot find ?? equivalent in Perl. Is there any?
The null-coalescing operator ?? returns the value of its left-hand operand if it isn't null ; otherwise, it evaluates the right-hand operand and returns its result.
Introduction. The ?? operator is also known as the null-coalescing operator. It returns the left side operand if the operand is not null else it returns the right side operand.
operator is known as Null-coalescing operator. It will return the value of its left-hand operand if it is not null. If it is null, then it will evaluate the right-hand operand and returns its result. Or if the left-hand operand evaluates to non-null, then it does not evaluate its right-hand operand.
In computer science, coalescing is a part of memory management in which two adjacent free blocks of computer memory are merged. When a program no longer requires certain blocks of memory, these blocks of memory can be freed.
As of 5.10 there is the //
operator, which is semantically equivalent if you consider the concept of undef
in Perl to be equivalent to the concept of null
in C#.
Example A:
my $a = undef; my $b = $a // 5; # $b = 5;
Example B:
my $a = 0; my $b = $a // 5; # $b = 0;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With