line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# You may distribute under the terms of either the GNU General Public License |
2
|
|
|
|
|
|
|
# or the Artistic License (the same terms as Perl itself) |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# (C) Paul Evans, 2023 -- leonerd@leonerd.org.uk |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
package Syntax::Operator::Eqr 0.06; |
7
|
|
|
|
|
|
|
|
8
|
3
|
|
|
3
|
|
238582
|
use v5.14; |
|
3
|
|
|
|
|
21
|
|
9
|
3
|
|
|
3
|
|
25
|
use warnings; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
79
|
|
10
|
|
|
|
|
|
|
|
11
|
3
|
|
|
3
|
|
21
|
use Carp; |
|
3
|
|
|
|
|
9
|
|
|
3
|
|
|
|
|
625
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
# Load the XS code |
14
|
|
|
|
|
|
|
require Syntax::Operator::Equ; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head1 NAME |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
C - string equality and regexp match operator |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
=head1 SYNOPSIS |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
On Perl v5.38 or later: |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
use v5.38; |
25
|
|
|
|
|
|
|
use Syntax::Operator::Eqr; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
if($str eqr $pat) { |
28
|
|
|
|
|
|
|
say "x and y are both undef, or both defined and equal strings, " . |
29
|
|
|
|
|
|
|
"or y is a regexp that matches x"; |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
Or via L on Perl v5.14 or later: |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
use v5.14; |
35
|
|
|
|
|
|
|
use Syntax::Keyword::Match; |
36
|
|
|
|
|
|
|
use Syntax::Operator::Eqr; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
match($str : eqr) { |
39
|
|
|
|
|
|
|
case(undef) { say "The variable is not defined" } |
40
|
|
|
|
|
|
|
case("") { say "The variable is defined but is empty" } |
41
|
|
|
|
|
|
|
case(qr/^.$/) { say "The variable contains exactly one character" } |
42
|
|
|
|
|
|
|
default { say "The string contains more than one" } |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=head1 DESCRIPTION |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
This module provides an infix operators that implements a matching operation |
48
|
|
|
|
|
|
|
whose behaviour depends on whether the right-hand side operand is undef, a |
49
|
|
|
|
|
|
|
quoted regexp object, or some other value. If undef, it is true only if the |
50
|
|
|
|
|
|
|
lefthand operand is also undef. If a quoted regexp object, it behaves like |
51
|
|
|
|
|
|
|
Perl's C<=~> pattern-matching operator. If neither, it behaves like the C |
52
|
|
|
|
|
|
|
operator. |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
This operator does not warn when either or both operands are C. |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
Support for custom infix operators was added in the Perl 5.37.x development |
57
|
|
|
|
|
|
|
cycle and is available from development release v5.37.7 onwards, and therefore |
58
|
|
|
|
|
|
|
in Perl v5.38 onwards. The documentation of L |
59
|
|
|
|
|
|
|
describes the situation in more detail. |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
While Perl versions before this do not support custom infix operators, they |
62
|
|
|
|
|
|
|
can still be used via C and hence L. |
63
|
|
|
|
|
|
|
Custom keywords which attempt to parse operator syntax may be able to use |
64
|
|
|
|
|
|
|
these. One such module is L; see the SYNOPSIS example |
65
|
|
|
|
|
|
|
given above. |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=head2 Comparison With Smartmatch |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
At first glance it would appear a little similar to core perl's ill-fated |
70
|
|
|
|
|
|
|
smartmatch operator (C<~~>), but this version is much simpler. It does not try |
71
|
|
|
|
|
|
|
to determine if stringy or numerical match is preferred, nor does it attempt |
72
|
|
|
|
|
|
|
to make sense of any C, C, C or other complicated container |
73
|
|
|
|
|
|
|
values on either side. Its behaviour is in effect entirely determined by the |
74
|
|
|
|
|
|
|
value on its righthand side - the three cases of C, some C |
75
|
|
|
|
|
|
|
object, or anything else. |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
This in particular makes it behave sensibly with the C syntax |
78
|
|
|
|
|
|
|
provided by L. |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=cut |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub import |
83
|
|
|
|
|
|
|
{ |
84
|
2
|
|
|
2
|
|
15
|
my $class = shift; |
85
|
2
|
|
|
|
|
11
|
my $caller = caller; |
86
|
|
|
|
|
|
|
|
87
|
2
|
|
|
|
|
11
|
$class->import_into( $caller, @_ ); |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub import_into |
91
|
|
|
|
|
|
|
{ |
92
|
2
|
|
|
2
|
0
|
4
|
my $class = shift; |
93
|
2
|
|
|
|
|
7
|
my ( $caller, @syms ) = @_; |
94
|
|
|
|
|
|
|
|
95
|
2
|
100
|
|
|
|
10
|
@syms or @syms = qw( eqr ); |
96
|
|
|
|
|
|
|
|
97
|
2
|
|
|
|
|
6
|
my %syms = map { $_ => 1 } @syms; |
|
2
|
|
|
|
|
8
|
|
98
|
2
|
100
|
|
|
|
21
|
$^H{"Syntax::Operator::Eqr/eqr"}++ if delete $syms{eqr}; |
99
|
|
|
|
|
|
|
|
100
|
2
|
|
|
|
|
6
|
foreach (qw( is_eqr )) { |
101
|
3
|
|
|
3
|
|
24
|
no strict 'refs'; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
475
|
|
102
|
2
|
100
|
|
|
|
15
|
*{"${caller}::$_"} = \&{$_} if delete $syms{$_}; |
|
1
|
|
|
|
|
8
|
|
|
1
|
|
|
|
|
3
|
|
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
2
|
50
|
|
|
|
2070
|
croak "Unrecognised import symbols @{[ keys %syms ]}" if keys %syms; |
|
0
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=head1 OPERATORS |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=head2 eqr |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
my $matches = $lhs eqr $rhs; |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
Yields true if both operands are C, or if the right-hand side is a |
115
|
|
|
|
|
|
|
quoted regexp value that matches the left-hand side, or if both are defined |
116
|
|
|
|
|
|
|
and contain equal string values. Yields false if given exactly one C, |
117
|
|
|
|
|
|
|
two unequal strings, or a string that does not match the pattern. |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=cut |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=head1 FUNCTIONS |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
As a convenience, the following functions may be imported which implement the |
124
|
|
|
|
|
|
|
same behaviour as the infix operators, though are accessed via regular |
125
|
|
|
|
|
|
|
function call syntax. |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
These wrapper functions are implemented using L, and thus |
128
|
|
|
|
|
|
|
have an optimising call-checker attached to them. In most cases, code which |
129
|
|
|
|
|
|
|
calls them should not in fact have the full runtime overhead of a function |
130
|
|
|
|
|
|
|
call because the underlying test operator will get inlined into the calling |
131
|
|
|
|
|
|
|
code at compiletime. In effect, code calling these functions should run with |
132
|
|
|
|
|
|
|
the same performance as code using the infix operators directly. |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=head2 is_eqr |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
my $matches = is_eqr( $lhs, $rhs ); |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
A function version of the L stringy operator. |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=cut |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=head1 SEE ALSO |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=over 4 |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=item * |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
L - equality operators that distinguish C |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=back |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=head1 AUTHOR |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
Paul Evans |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=cut |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
0x55AA; |