File Coverage

blib/lib/Syntax/Operator/Is.pm
Criterion Covered Total %
statement 15 17 88.2
branch 2 4 50.0
condition n/a
subroutine 5 6 83.3
pod 0 1 0.0
total 22 28 78.5


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, 2024 -- leonerd@leonerd.org.uk
5              
6             package Syntax::Operator::Is 0.02;
7              
8 3     3   961106 use v5.14;
  3         15  
9 3     3   26 use warnings;
  3         5  
  3         213  
10              
11 3     3   20 use Carp;
  3         6  
  3         1127  
12              
13             require XSLoader;
14             XSLoader::load( __PACKAGE__, our $VERSION );
15              
16             =head1 NAME
17              
18             C - match operator using L constraints
19              
20             =head1 SYNOPSIS
21              
22             On Perl v5.38 or later:
23              
24             use v5.38;
25             use Syntax::Operator::Is;
26              
27             use Data::Checks qw( Num Object );
28              
29             my $x = ...;
30              
31             if($x is Num) {
32             say "x can be used as a number";
33             }
34             elsif($x is Object) {
35             say "x can be used as an object";
36             }
37              
38             Or via L on Perl v5.14 or later:
39              
40             use v5.14;
41             use Syntax::Operator::Is;
42             use Syntax::Keyword::Match;
43              
44             use Data::Checks qw( Num Object );
45              
46             my $x = ...;
47              
48             match($x : is) {
49             case(Num) { say "x can be used as a number"; }
50             case(Object) { say "x can be used as an object"; }
51             }
52              
53             =head1 DESCRIPTION
54              
55             This module provides an infix operator that checks if a given value matches a
56             value constraint provided by L.
57              
58             Support for custom infix operators was added in the Perl 5.37.x development
59             cycle and is available from development release v5.37.7 onwards, and therefore
60             in Perl v5.38 onwards. The documentation of L
61             describes the situation in more detail.
62              
63             While Perl versions before this do not support custom infix operators, they
64             can still be used via C and hence L.
65             Custom keywords which attempt to parse operator syntax may be able to use
66             these. One such module is L; see the SYNOPSIS example
67             given above.
68              
69             =head2 Operator Name vs C / C
70              
71             This module provides a named operator called C. The same name is used by
72             both L and L as a unit test assertion function. Since
73             each has to be imported by request, this does not cause issues for code that
74             does not try to use both of them at once. Most real use-cases will not be unit
75             test scripts, and most unit test scripts will not need to use this operator.
76              
77             In situations where you need to use this C operator and one of the testing
78             modules at the same time (for example, during a unit test of some
79             check-related code), note that because of the way infix operator plugins work,
80             the named operator will always take precedence and thus you will need to call
81             the C testing function by its fully-qualified name:
82              
83             use Test2::V0;
84             use Syntax::Operator::Is;
85              
86             Test2::V0::is( 1 is Num, builtin::true, '1 is Num' );
87              
88             Alternatively, use the ability of L to import the operator
89             with a different name and avoid the collision.
90              
91             use Test2::V0;
92             use Syntax::Operator::Is is => { -as => "is_checked" };
93              
94             is( 1 is_checked Num, builtin::true, '1 is Num' );
95              
96             =cut
97              
98             sub import
99             {
100 2     2   30 my $pkg = shift;
101 2         8 $pkg->apply( 1, @_ );
102             }
103              
104             sub unimport
105             {
106 0     0   0 my $pkg = shift;
107 0         0 $pkg->apply( 0, @_ );
108             }
109              
110             sub apply
111             {
112 2     2 0 4 my $pkg = shift;
113 2         7 my ( $on, @syms ) = @_;
114              
115 2 50       12 @syms or @syms = qw( is );
116              
117 2         16 $pkg->XS::Parse::Infix::apply_infix( $on, \@syms, qw( is ) );
118              
119 2 50       311 croak "Unrecognised import symbols @syms" if @syms;
120             }
121              
122             =head1 OPERATORS
123              
124             =head2 is
125              
126             my $ok = $value is $Constraint;
127              
128             Yields true if the given value is accepted by the given constraint checker.
129             Yields false but should not otherwise throw an exception if the value is
130             rejected.
131              
132             =cut
133              
134             =head1 AUTHOR
135              
136             Paul Evans
137              
138             =cut
139              
140             0x55AA;