NAME
Syntax::Operator::Elem
- element-of-list operators
SYNOPSIS
On a suitably-patched perl:
use Syntax::Operator::Elem;
if($x elem @some_strings) {
say "x is one of the given strings";
}
Or, on a standard perl:
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.
Current versions of perl do not directly support custom infix operators. The documentation of XS::Parse::Infix describes the situation, with reference to a branch experimenting with this new feature. This module is therefore almost entirely useless on standard perl builds. While the regular parser does not support custom infix operators, they are supported via XS::Parse::Infix
and hence XS::Parse::Keyword, and so custom keywords which attempt to parse operator syntax may be able to use it.
Additionally, standard 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>