1Syntax::Operator::Equ(3U)ser Contributed Perl DocumentatiSoynntax::Operator::Equ(3)
2
3
4
6 "Syntax::Operator::Equ" - equality operators that distinguish "undef"
7
9 On a suitably-patched perl:
10
11 use Syntax::Operator::Equ;
12
13 if($x equ $y) {
14 say "x and y are both undef, or both defined and equal strings";
15 }
16
17 if($i === $j) {
18 say "i and j are both undef, or both defined and equal numbers";
19 }
20
21 Or, on a standard perl via Syntax::Keyword::Match:
22
23 use v5.14;
24 use Syntax::Keyword::Match;
25 use Syntax::Operator::Equ;
26
27 match($str : equ) {
28 case(undef) { say "The variable is not defined" }
29 case("") { say "The variable is defined but is empty" }
30 default { say "The string is non-empty" }
31 }
32
34 This module provides infix operators that implement equality tests of
35 strings or numbers similar to perl's "eq" and "==" operators, except
36 that they consider "undef" to be a distinct value, separate from the
37 empty string or the number zero.
38
39 These operators do not warn when either or both operands are "undef".
40 They yield true if both operands are "undef", false if exactly one
41 operand is, or otherwise behave the same as the regular string or
42 number equality tests if both operands are defined.
43
44 Current versions of perl do not directly support custom infix
45 operators. The documentation of XS::Parse::Infix describes the
46 situation, with reference to a branch experimenting with this new
47 feature. This module is therefore almost entirely useless on standard
48 perl builds. While the regular parser does not support custom infix
49 operators, they are supported via "XS::Parse::Infix" and hence
50 XS::Parse::Keyword, and so custom keywords which attempt to parse
51 operator syntax may be able to use it. One such module is
52 Syntax::Keyword::Match; see the SYNOPSIS example given above.
53
55 equ
56 my $equal = $lhs equ $rhs;
57
58 Yields true if both operands are "undef", or if both are defined and
59 contain equal string values. Yields false if given exactly one "undef",
60 or two unequal strings.
61
62 ===
63 my $equal = $lhs === $rhs;
64
65 Yields true if both operands are "undef", or if both are defined and
66 contain equal numerical values. Yields false if given exactly one
67 "undef", or two unequal numbers.
68
69 Note that while this operator will not cause warnings about
70 uninitialized values, it can still warn if given defined stringy values
71 that are not valid as numbers.
72
74 As a convenience, the following functions may be imported which
75 implement the same behaviour as the infix operators, though are
76 accessed via regular function call syntax.
77
78 These wrapper functions are implemented using XS::Parse::Infix, and
79 thus have an optimising call-checker attached to them. In most cases,
80 code which calls them should not in fact have the full runtime overhead
81 of a function call because the underlying test operator will get
82 inlined into the calling code at compiletime. In effect, code calling
83 these functions should run with the same performance as code using the
84 infix operators directly.
85
86 is_strequ
87 my $equal = is_strequ( $lhs, $rhs );
88
89 A function version of the "equ" stringy operator.
90
91 is_numequ
92 my $equal = is_numequ( $lhs, $rgh );
93
94 A function version of the "===" numerical operator.
95
97 Paul Evans <leonerd@leonerd.org.uk>
98
99
100
101perl v5.34.0 2021-11-12 Syntax::Operator::Equ(3)