| 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 | ▶ | use v5.14; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 3 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 3 | require XSLoader; | |||||||||
| 14 | 3 | XSLoader::load( __PACKAGE__, our $VERSION ); | |||||||||
| 15 | |||||||||||
| 16 | =head1 NAME | ||||||||||
| 17 | |||||||||||
| 18 | C<Syntax::Operator::Is> - match operator using L<Data::Checks> 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<Syntax::Keyword::Match> 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<Data::Checks>. | ||||||||||
| 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<XS::Parse::Infix> | ||||||||||
| 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<XS::Parse::Infix> and hence L<XS::Parse::Keyword>. | ||||||||||
| 65 | Custom keywords which attempt to parse operator syntax may be able to use | ||||||||||
| 66 | these. One such module is L<Syntax::Keyword::Match>; see the SYNOPSIS example | ||||||||||
| 67 | given above. | ||||||||||
| 68 | |||||||||||
| 69 | =head2 Operator Name vs C<Test::More> / C<Test2::V0> | ||||||||||
| 70 | |||||||||||
| 71 | This module provides a named operator called C<is>. The same name is used by | ||||||||||
| 72 | both L<Test::More> and L<Test2::V0> 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<is> 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<is()> 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<XS::Parse::Infix> 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 | 2 | ▶ | { | ||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||
| 100 | 2 | my $pkg = shift; | |||||||||
| 101 | 2 | $pkg->apply( 1, @_ ); | |||||||||
| 102 | } | ||||||||||
| 103 | |||||||||||
| 104 | sub unimport | ||||||||||
| 105 | 0 | ▶ | { | ||||||||
|
Subroutine
unimport: not called CC=1 SCAR=6.9
| |||||||||||
| 106 | 0 | my $pkg = shift; | |||||||||
| 107 | 0 | $pkg->apply( 0, @_ ); | |||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | sub apply | ||||||||||
| 111 | 2 | ▶ | { | ||||||||
|
Subroutine
apply: called CC=3 SCAR=11.3
Pod
undocumented
| |||||||||||
| 112 | 2 | my $pkg = shift; | |||||||||
| 113 | 2 | my ( $on, @syms ) = @_; | |||||||||
| 114 | |||||||||||
| 115 | 2 | ▶ | @syms or @syms = qw( is ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 116 | |||||||||||
| 117 | 2 | $pkg->XS::Parse::Infix::apply_infix( $on, \@syms, qw( is ) ); | |||||||||
| 118 | |||||||||||
| 119 | 2 | ▶ | croak "Unrecognised import symbols @syms" if @syms; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 <leonerd@leonerd.org.uk> | ||||||||||
| 137 | |||||||||||
| 138 | =cut | ||||||||||
| 139 | |||||||||||
| 140 | 3 | 0x55AA; | |||||||||