Syntax::Operator::ExistsOr(3pm)

NAME
"Syntax::Operator::ExistsOr" - an infix operator sensitive to hash element existence

SYNOPSIS
On a suitable perl version:

   use Syntax::Operator::ExistsOr;

   sub func ( %args ) {
      my $count = $args{count} \\ 10;

      say "Count is ", $count // "<undef>";
   }

   func( count => 20 );
   func();

   func( count => undef );

DESCRIPTION
This module provides an infix operator that similar to the defined-or "//" core perl operator, but which cares about hash element existence rather than definedness.

Support for custom infix operators was added in the Perl 5.37.x development cycle and is available from development release v5.37.7 onwards, and therefore in Perl v5.38 onwards. The documentation of XS::Parse::Infix describes the situation in more detail.

While Perl versions before this do not support custom infix operators, they can still be used via "XS::Parse::Infix" and hence XS::Parse::Keyword. Custom keywords which attempt to parse operator syntax may be able to use these.

This module does not provide wrapper functions for the operators, as their inherent short-circuiting behaviour would appear confusing when expressed in function-like syntax.

OPERATORS

  \\
   my $value = $hash{$key} \\ EXPR;

The lefthand operand must be a hash element access (i.e. $hash{$key} or "$href->{$key}" for some expressions yielding a key and a hashref).

If the hash contains the given key then the operator yields its value (even if that value is "undef"). If the key does not exist in the hash, then the righthand operand will be evaluated in scalar context and its value returned.

This is a short-circuiting operator; if the hash does contain the key then the righthand side expression is not evaluated at all.

This operator parses at the same precedence level as the logical-or operators ("||" and "//").

  existsor
   do {
      $hash{$key} existsor EXPR;
   };

Similar to the "\\" operator but parses at the same level as the low-precedence or operator ("or"). This is unlikely to be very useful, as normally "or" would be used for value-less control flow. Such a potential use for this operator would be neater written

   exists $hash{$key} or EXPR;

It is included largely for completeness.

AUTHOR
Paul Evans <leonerd@leonerd.org.uk>