File Coverage

blib/lib/Syntax/Operator/Equ.pm
Criterion Covered Total %
statement 31 34 91.1
branch 7 8 87.5
condition n/a
subroutine 8 9 88.8
pod 1 2 50.0
total 47 53 88.6


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, 2021 -- leonerd@leonerd.org.uk
5              
6             package Syntax::Operator::Equ 0.05;
7              
8 6     6   350892 use v5.14;
  6         67  
9 6     6   30 use warnings;
  6         12  
  6         149  
10              
11 6     6   27 use Carp;
  6         12  
  6         1281  
12              
13             require XSLoader;
14             XSLoader::load( __PACKAGE__, our $VERSION );
15              
16             =head1 NAME
17              
18             C - equality operators that distinguish C
19              
20             =head1 SYNOPSIS
21              
22             On a suitable perl version:
23              
24             use Syntax::Operator::Equ;
25              
26             if($x equ $y) {
27             say "x and y are both undef, or both defined and equal strings";
28             }
29              
30             if($i === $j) {
31             say "i and j are both undef, or both defined and equal numbers";
32             }
33              
34             Or, on a standard perl via L:
35              
36             use v5.14;
37             use Syntax::Keyword::Match;
38             use Syntax::Operator::Equ;
39              
40             match($str : equ) {
41             case(undef) { say "The variable is not defined" }
42             case("") { say "The variable is defined but is empty" }
43             default { say "The string is non-empty" }
44             }
45              
46             =head1 DESCRIPTION
47              
48             This module provides infix operators that implement equality tests of strings
49             or numbers similar to perl's C and C<==> operators, except that they
50             consider C to be a distinct value, separate from the empty string or
51             the number zero.
52              
53             These operators do not warn when either or both operands are C. They
54             yield true if both operands are C, false if exactly one operand is, or
55             otherwise behave the same as the regular string or number equality tests if
56             both operands are defined.
57              
58             Current stable versions of perl do not directly support custom infix
59             operators, but the ability was added in the 5.37.x development cycle and is
60             available from perl v5.37.7 onwards. The documentation of L
61             describes the situation in more detail. This module is therefore I
62             entirely useless on stable perl builds. While the regular parser does not
63             support custom infix operators, they are supported via C and
64             hence L, and so custom keywords which attempt to parse
65             operator syntax may be able to use it. One such module is
66             L; see the SYNOPSIS example given above.
67              
68             =cut
69              
70             sub import
71             {
72 7     7   288 my $class = shift;
73 7         16 my $caller = caller;
74              
75 7         21 $class->import_into( $caller, @_ );
76             }
77              
78             sub import_into
79             {
80 7     7 0 12 my $class = shift;
81 7         17 my ( $caller, @syms ) = @_;
82              
83 7 100       57 @syms or @syms = qw( equ );
84              
85 7         22 my %syms = map { $_ => 1 } @syms;
  9         36  
86 7 100       58 $^H{"Syntax::Operator::Equ/equ"}++ if delete $syms{equ};
87              
88 7         18 foreach (qw( is_strequ is_numequ )) {
89 6     6   43 no strict 'refs';
  6         10  
  6         898  
90 14 100       40 *{"${caller}::$_"} = \&{$_} if delete $syms{$_};
  4         30  
  4         10  
91             }
92              
93 7 50       3068 croak "Unrecognised import symbols @{[ keys %syms ]}" if keys %syms;
  0            
94             }
95              
96             =head1 OPERATORS
97              
98             =head2 equ
99              
100             my $equal = $lhs equ $rhs;
101              
102             Yields true if both operands are C, or if both are defined and contain
103             equal string values. Yields false if given exactly one C, or two
104             unequal strings.
105              
106             =head2 ===
107              
108             my $equal = $lhs === $rhs;
109              
110             Yields true if both operands are C, or if both are defined and contain
111             equal numerical values. Yields false if given exactly one C, or two
112             unequal numbers.
113              
114             Note that while this operator will not cause warnings about uninitialized
115             values, it can still warn if given defined stringy values that are not valid
116             as numbers.
117              
118             =cut
119              
120             =head1 FUNCTIONS
121              
122             As a convenience, the following functions may be imported which implement the
123             same behaviour as the infix operators, though are accessed via regular
124             function call syntax.
125              
126             These wrapper functions are implemented using L, and thus
127             have an optimising call-checker attached to them. In most cases, code which
128             calls them should not in fact have the full runtime overhead of a function
129             call because the underlying test operator will get inlined into the calling
130             code at compiletime. In effect, code calling these functions should run with
131             the same performance as code using the infix operators directly.
132              
133             =head2 is_strequ
134              
135             my $equal = is_strequ( $lhs, $rhs );
136              
137             A function version of the L stringy operator.
138              
139             =head2 is_numequ
140              
141             my $equal = is_numequ( $lhs, $rgh );
142              
143             A function version of the L numerical operator.
144              
145             =cut
146              
147             =head1 AUTHOR
148              
149             Paul Evans
150              
151             =cut
152              
153             0x55AA;