Search code examples
c#perlnull-coalescing-operator

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


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?


Solution

  • 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;