Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Perl equivalent to the null coalescing operator (??) in C#?

Tags:

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?

like image 387
Karel Bílek Avatar asked Dec 12 '09 01:12

Karel Bílek


People also ask

What is null-coalescing operator in C#?

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.

Which of the following is the null-coalescing operator?

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.

Why we use null-coalescing operator?

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.

What is coalescing in programming?

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.


1 Answers

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; 
like image 179
Adam Bellaire Avatar answered Sep 18 '22 18:09

Adam Bellaire