| 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, 2021-2026 -- leonerd@leonerd.org.uk | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | package Syntax::Operator::Equ 0.11; | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 7 | ▶ | use v5.14; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 7 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 7 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 7 | ▶ | use meta 0.003_002; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 7 | ▶ | no warnings 'meta::experimental'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 7 | require XSLoader; | |||||||||||||||||||
| 17 | 7 | XSLoader::load( __PACKAGE__, our $VERSION ); | |||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | =head1 NAME | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | C<Syntax::Operator::Equ> - equality operators that distinguish C<undef> | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | =head1 SYNOPSIS | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | On Perl v5.38 or later: | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | use v5.38; | ||||||||||||||||||||
| 28 | use Syntax::Operator::Equ; | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | if($x equ $y) { | ||||||||||||||||||||
| 31 | say "x and y are both undef, or both defined and equal strings"; | ||||||||||||||||||||
| 32 | } | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | if($i === $j) { | ||||||||||||||||||||
| 35 | say "i and j are both undef, or both defined and equal numbers"; | ||||||||||||||||||||
| 36 | } | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | Or via L<Syntax::Keyword::Match> on Perl v5.14 or later: | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | use v5.14; | ||||||||||||||||||||
| 41 | use Syntax::Keyword::Match; | ||||||||||||||||||||
| 42 | use Syntax::Operator::Equ; | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | match($str : equ) { | ||||||||||||||||||||
| 45 | case(undef) { say "The variable is not defined" } | ||||||||||||||||||||
| 46 | case("") { say "The variable is defined but is empty" } | ||||||||||||||||||||
| 47 | default { say "The string is non-empty" } | ||||||||||||||||||||
| 48 | } | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | =head1 DESCRIPTION | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | This module provides infix operators that implement equality tests of strings | ||||||||||||||||||||
| 53 | or numbers similar to perl's C<eq> and C<==> operators, except that they | ||||||||||||||||||||
| 54 | consider C<undef> to be a distinct value, separate from the empty string or | ||||||||||||||||||||
| 55 | the number zero. | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | These operators do not warn when either or both operands are C<undef>. They | ||||||||||||||||||||
| 58 | yield true if both operands are C<undef>, false if exactly one operand is, or | ||||||||||||||||||||
| 59 | otherwise behave the same as the regular string or number equality tests if | ||||||||||||||||||||
| 60 | both operands are defined. | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | Support for custom infix operators was added in the Perl 5.37.x development | ||||||||||||||||||||
| 63 | cycle and is available from development release v5.37.7 onwards, and therefore | ||||||||||||||||||||
| 64 | in Perl v5.38 onwards. The documentation of L<XS::Parse::Infix> | ||||||||||||||||||||
| 65 | describes the situation in more detail. | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | While Perl versions before this do not support custom infix operators, they | ||||||||||||||||||||
| 68 | can still be used via C<XS::Parse::Infix> and hence L<XS::Parse::Keyword>. | ||||||||||||||||||||
| 69 | Custom keywords which attempt to parse operator syntax may be able to use | ||||||||||||||||||||
| 70 | these. One such module is L<Syntax::Keyword::Match>; see the SYNOPSIS example | ||||||||||||||||||||
| 71 | given above. | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | =head2 PPC0030 and Perl 5.45.3 | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | The behaviour in this module was turned into a I<Proposed Perl Change> | ||||||||||||||||||||
| 76 | document, L<PPC0030|https://github.com/Perl/PPCs/blob/main/ppcs/ppc0030-undef-aware-equality.md>. | ||||||||||||||||||||
| 77 | This has now been implemented in F<perl> itself, and should become available | ||||||||||||||||||||
| 78 | from version 5.45.3 onwards. It is still possible to use this module on such a | ||||||||||||||||||||
| 79 | version of Perl, but it is unnecessary to do so as core will already provide | ||||||||||||||||||||
| 80 | the same behaviour. | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | =cut | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | sub import | ||||||||||||||||||||
| 85 | 6 | ▶ | { | ||||||||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 86 | 6 | my $pkg = shift; | |||||||||||||||||||
| 87 | 6 | my $caller = caller; | |||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 6 | $pkg->import_into( $caller, @_ ); | |||||||||||||||||||
| 90 | } | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | sub unimport | ||||||||||||||||||||
| 93 | 2 | ▶ | { | ||||||||||||||||||
|
Subroutine
unimport: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 94 | 2 | my $pkg = shift; | |||||||||||||||||||
| 95 | 2 | my $caller = caller; | |||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 2 | $pkg->unimport_into( $caller, @_ ); | |||||||||||||||||||
| 98 | } | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 6 | ▶ | sub import_into { shift->apply( 1, @_ ) } | ||||||||||||||||||
|
Subroutine
import_into: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 101 | 2 | ▶ | sub unimport_into { shift->apply( 0, @_ ) } | ||||||||||||||||||
|
Subroutine
unimport_into: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | sub apply | ||||||||||||||||||||
| 104 | 8 | ▶ | { | ||||||||||||||||||
|
Subroutine
apply: called CC=7 SCAR=19.6
Pod
undocumented
| |||||||||||||||||||||
| 105 | 8 | my $pkg = shift; | |||||||||||||||||||
| 106 | 8 | my ( $on, $caller, @syms ) = @_; | |||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 8 | ▶ | @syms or @syms = qw( equ === ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | 8 | $pkg->XS::Parse::Infix::apply_infix( $on, \@syms, qw( equ === ) ); | |||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | 8 | my %syms = map { $_ => 1 } @syms; | |||||||||||||||||||
| 113 | 8 | my $callerpkg; | |||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | 8 | foreach (qw( is_strequ is_numequ )) { | |||||||||||||||||||
| 116 | 16 | ▶ | next unless delete $syms{$_}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | 5 | ▶ | $callerpkg //= meta::package->get( $caller ); | ||||||||||||||||||
|
Condition66%2/3
$callerpkg //= "meta::package"->get($caller)
MC/DC0%0/2
$callerpkg //= "meta::package"->get($caller)
$callerpkg
"meta::package"->get($caller)
Truth table66%2/3
A //= B
A= $callerpkgB= "meta::package"->get($caller)
| |||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | 5 | ▶ | $on ? $callerpkg->add_symbol( '&'.$_ => \&{$_} ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 121 | : $callerpkg->remove_symbol( '&'.$_ ); | ||||||||||||||||||||
| 122 | } | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | 0 | ▶ | croak "Unrecognised import symbols @{[ keys %syms ]}" if keys %syms; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 125 | } | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | =head1 OPERATORS | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | =head2 equ | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | my $equal = $lhs equ $rhs; | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | Yields true if both operands are C<undef>, or if both are defined and contain | ||||||||||||||||||||
| 134 | equal string values. Yields false if given exactly one C<undef>, or two | ||||||||||||||||||||
| 135 | unequal strings. | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | =head2 === | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | my $equal = $lhs === $rhs; | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | Yields true if both operands are C<undef>, or if both are defined and contain | ||||||||||||||||||||
| 142 | equal numerical values. Yields false if given exactly one C<undef>, or two | ||||||||||||||||||||
| 143 | unequal numbers. | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | Note that while this operator will not cause warnings about uninitialized | ||||||||||||||||||||
| 146 | values, it can still warn if given defined stringy values that are not valid | ||||||||||||||||||||
| 147 | as numbers. | ||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | =cut | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | =head1 FUNCTIONS | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | As a convenience, the following functions may be imported which implement the | ||||||||||||||||||||
| 154 | same behaviour as the infix operators, though are accessed via regular | ||||||||||||||||||||
| 155 | function call syntax. | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | These wrapper functions are implemented using L<XS::Parse::Infix>, and thus | ||||||||||||||||||||
| 158 | have an optimising call-checker attached to them. In most cases, code which | ||||||||||||||||||||
| 159 | calls them should not in fact have the full runtime overhead of a function | ||||||||||||||||||||
| 160 | call because the underlying test operator will get inlined into the calling | ||||||||||||||||||||
| 161 | code at compiletime. In effect, code calling these functions should run with | ||||||||||||||||||||
| 162 | the same performance as code using the infix operators directly. | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | =head2 is_strequ | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | my $equal = is_strequ( $lhs, $rhs ); | ||||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | A function version of the L</equ> stringy operator. | ||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | =head2 is_numequ | ||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | my $equal = is_numequ( $lhs, $rgh ); | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | A function version of the L</===> numerical operator. | ||||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | =cut | ||||||||||||||||||||
| 177 | |||||||||||||||||||||
| 178 | =head1 SEE ALSO | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | =over 4 | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | =item * | ||||||||||||||||||||
| 183 | |||||||||||||||||||||
| 184 | L<Syntax::Operator::Eqr> - string equality and regexp match operator | ||||||||||||||||||||
| 185 | |||||||||||||||||||||
| 186 | =back | ||||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | =head1 AUTHOR | ||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | =cut | ||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | 7 | 0x55AA; | |||||||||||||||||||