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.04;
7              
8 6     6   346100 use v5.14;
  6         72  
9 6     6   33 use warnings;
  6         12  
  6         164  
10              
11 6     6   30 use Carp;
  6         11  
  6         1336  
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 suitably-patched perl:
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 versions of perl do not directly support custom infix operators. The
59             documentation of L describes the situation, with reference
60             to a branch experimenting with this new feature. This module is therefore
61             I entirely useless on standard perl builds. While the regular parser
62             does not support custom infix operators, they are supported via
63             C and hence L, and so custom keywords
64             which attempt to parse operator syntax may be able to use it. One such module
65             is L; see the SYNOPSIS example given above.
66              
67             =cut
68              
69             sub import
70             {
71 7     7   285 my $class = shift;
72 7         16 my $caller = caller;
73              
74 7         25 $class->import_into( $caller, @_ );
75             }
76              
77             sub import_into
78             {
79 7     7 0 13 my $class = shift;
80 7         21 my ( $caller, @syms ) = @_;
81              
82 7 100       30 @syms or @syms = qw( equ );
83              
84 7         22 my %syms = map { $_ => 1 } @syms;
  9         36  
85 7 100       52 $^H{"Syntax::Operator::Equ/equ"}++ if delete $syms{equ};
86              
87 7         20 foreach (qw( is_strequ is_numequ )) {
88 6     6   45 no strict 'refs';
  6         11  
  6         908  
89 14 100       45 *{"${caller}::$_"} = \&{$_} if delete $syms{$_};
  4         19  
  4         8  
90             }
91              
92 7 50       3105 croak "Unrecognised import symbols @{[ keys %syms ]}" if keys %syms;
  0            
93             }
94              
95             =head1 OPERATORS
96              
97             =head2 equ
98              
99             my $equal = $lhs equ $rhs;
100              
101             Yields true if both operands are C, or if both are defined and contain
102             equal string values. Yields false if given exactly one C, or two
103             unequal strings.
104              
105             =head2 ===
106              
107             my $equal = $lhs === $rhs;
108              
109             Yields true if both operands are C, or if both are defined and contain
110             equal numerical values. Yields false if given exactly one C, or two
111             unequal numbers.
112              
113             Note that while this operator will not cause warnings about uninitialized
114             values, it can still warn if given defined stringy values that are not valid
115             as numbers.
116              
117             =cut
118              
119             =head1 FUNCTIONS
120              
121             As a convenience, the following functions may be imported which implement the
122             same behaviour as the infix operators, though are accessed via regular
123             function call syntax.
124              
125             These wrapper functions are implemented using L, and thus
126             have an optimising call-checker attached to them. In most cases, code which
127             calls them should not in fact have the full runtime overhead of a function
128             call because the underlying test operator will get inlined into the calling
129             code at compiletime. In effect, code calling these functions should run with
130             the same performance as code using the infix operators directly.
131              
132             =head2 is_strequ
133              
134             my $equal = is_strequ( $lhs, $rhs );
135              
136             A function version of the L stringy operator.
137              
138             =head2 is_numequ
139              
140             my $equal = is_numequ( $lhs, $rgh );
141              
142             A function version of the L numerical operator.
143              
144             =cut
145              
146             =head1 AUTHOR
147              
148             Paul Evans
149              
150             =cut
151              
152             0x55AA;