1Syntax::Operator::Eqr(3U)ser Contributed Perl DocumentatiSoynntax::Operator::Eqr(3)
2
3
4

NAME

6       "Syntax::Operator::Eqr" - string equality and regexp match operator
7

SYNOPSIS

9       On Perl v5.38 or later:
10
11          use v5.38;
12          use Syntax::Operator::Eqr;
13
14          if($str eqr $pat) {
15             say "x and y are both undef, or both defined and equal strings, " .
16                 "or y is a regexp that matches x";
17          }
18
19       Or via Syntax::Keyword::Match on Perl v5.14 or later:
20
21          use v5.14;
22          use Syntax::Keyword::Match;
23          use Syntax::Operator::Eqr;
24
25          match($str : eqr) {
26             case(undef)   { say "The variable is not defined" }
27             case("")      { say "The variable is defined but is empty" }
28             case(qr/^.$/) { say "The variable contains exactly one character" }
29             default       { say "The string contains more than one" }
30          }
31

DESCRIPTION

33       This module provides an infix operators that implements a matching
34       operation whose behaviour depends on whether the right-hand side
35       operand is undef, a quoted regexp object, or some other value. If
36       undef, it is true only if the lefthand operand is also undef. If a
37       quoted regexp object, it behaves like Perl's "=~" pattern-matching
38       operator. If neither, it behaves like the "eq" operator.
39
40       This operator does not warn when either or both operands are "undef".
41
42       Support for custom infix operators was added in the Perl 5.37.x
43       development cycle and is available from development release v5.37.7
44       onwards, and therefore in Perl v5.38 onwards. The documentation of
45       XS::Parse::Infix describes the situation in more detail.
46
47       While Perl versions before this do not support custom infix operators,
48       they can still be used via "XS::Parse::Infix" and hence
49       XS::Parse::Keyword.  Custom keywords which attempt to parse operator
50       syntax may be able to use these. One such module is
51       Syntax::Keyword::Match; see the SYNOPSIS example given above.
52
53   Comparison With Smartmatch
54       At first glance it would appear a little similar to core perl's ill-
55       fated smartmatch operator ("~~"), but this version is much simpler. It
56       does not try to determine if stringy or numerical match is preferred,
57       nor does it attempt to make sense of any "ARRAY", "HASH", "CODE" or
58       other complicated container values on either side. Its behaviour is in
59       effect entirely determined by the value on its righthand side - the
60       three cases of "undef", some "qr/.../" object, or anything else.
61
62       This in particular makes it behave sensibly with the "match/case"
63       syntax provided by Syntax::Keyword::Match.
64

OPERATORS

66   eqr
67          my $matches = $lhs eqr $rhs;
68
69       Yields true if both operands are "undef", or if the right-hand side is
70       a quoted regexp value that matches the left-hand side, or if both are
71       defined and contain equal string values. Yields false if given exactly
72       one "undef", two unequal strings, or a string that does not match the
73       pattern.
74

FUNCTIONS

76       As a convenience, the following functions may be imported which
77       implement the same behaviour as the infix operators, though are
78       accessed via regular function call syntax.
79
80       These wrapper functions are implemented using XS::Parse::Infix, and
81       thus have an optimising call-checker attached to them. In most cases,
82       code which calls them should not in fact have the full runtime overhead
83       of a function call because the underlying test operator will get
84       inlined into the calling code at compiletime. In effect, code calling
85       these functions should run with the same performance as code using the
86       infix operators directly.
87
88   is_eqr
89          my $matches = is_eqr( $lhs, $rhs );
90
91       A function version of the "eqr" stringy operator.
92

SEE ALSO

94       •   Syntax::Operator::Equ - equality operators that distinguish "undef"
95

AUTHOR

97       Paul Evans <leonerd@leonerd.org.uk>
98
99
100
101perl v5.36.1                      2023-07-10          Syntax::Operator::Eqr(3)
Impressum