NAME

Syntax::Operator::Elem - element-of-list operators

SYNOPSIS

On Perl v5.38 or later:

use Syntax::Operator::Elem;

if($x elem @some_strings) {
   say "x is one of the given strings";
}

Or on Perl v5.14 or later:

use v5.14;
use Syntax::Operator::Elem qw( elem_str );

if(elem_str $x, @some_strings) {
   say "x is one of the given strings";
}

DESCRIPTION

This module provides infix operators that implement element-of-list tests for strings and numbers.

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.

Additionally, earlier versions of perl can still use the function-like wrapper versions of these operators. Even though the syntax appears like a regular function call, the code is compiled internally into the same more efficient operator internally, so will run without the function-call overhead of a regular function.

OPERATORS

elem

my $present = $lhs elem @rhs;

Yields true if the string on the lefthand side is equal to any of the strings in the list on the right.

Note that it is specifically not guaranteed that this test will be performed in any particular order. Nor is it guaranteed that any eq operator overloading present on any of the elements is respected. These conditions may allow an implementation at least partially based on a hash, balanced binary tree, or other techniques.

my $present = $lhs ∈ @rhs;

Yields true if the number on the lefthand side is equal to any of the numbers in the list on the right.

Note that it is specifically not guaranteed that this test will be performed in any particular order. Nor is it guaranteed that any == operator overloading present on any of the elements is respected. These conditions may allow an implementation at least partially based on a hash, balanced binary tree, or other techniques.

FUNCTIONS

As a convenience, the following functions may be imported which implement the same behaviour as the infix operators, though are accessed via regular function call syntax.

elem_str

my $present = elem_str( $lhs, @rhs );

A function version of the "elem" stringy operator.

elem_num

my $present = elem_num( $lhs, @rhs );

A function version of the "∈" numerical operator.

AUTHOR

Paul Evans <leonerd@leonerd.org.uk>