File Coverage

blib/lib/Syntax/Operator/Divides.pm
Criterion Covered Total %
statement 27 28 96.4
branch 7 8 87.5
condition n/a
subroutine 7 7 100.0
pod 0 1 0.0
total 41 44 93.1


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-2022 -- leonerd@leonerd.org.uk
5              
6             package Syntax::Operator::Divides 0.03;
7              
8 3     3   142766 use v5.14;
  3         29  
9 3     3   16 use warnings;
  3         5  
  3         76  
10              
11 3     3   16 use Carp;
  3         5  
  3         615  
12              
13             require XSLoader;
14             XSLoader::load( __PACKAGE__, our $VERSION );
15              
16             =head1 NAME
17              
18             C - an infix operator for division test
19              
20             =head1 SYNOPSIS
21              
22             On a suitable perl version:
23              
24             use Syntax::Operator::Divides;
25              
26             say "Multiple of 10" if $x %% 10;
27              
28             Or, on a standard perl via L:
29              
30             use v5.14;
31             use Syntax::Keyword::Match;
32             use Syntax::Operator::Divides;
33              
34             foreach ( 1 .. 100 ) {
35             match( $_ : %% ) {
36             case(15) { say "FizzBuzz" }
37             case(3) { say "Fizz" }
38             case(5) { say "Buzz" }
39             default { say $_ }
40             }
41             }
42              
43             =head1 DESCRIPTION
44              
45             This module provides an infix operator that implements an integer divides test
46             which returns true if the lefthand operand is a whole multiple of the
47             righthand.
48              
49             Current stable versions of perl do not directly support custom infix
50             operators, but the ability was added in the 5.37.x development cycle and is
51             available from perl v5.37.7 onwards. The documentation of L
52             describes the situation in more detail. This module is therefore I
53             entirely useless on stable perl builds. While the regular parser does not
54             support custom infix operators, they are supported via C and
55             hence L, and so custom keywords which attempt to parse
56             operator syntax may be able to use it.
57              
58             =cut
59              
60             sub import
61             {
62 3     3   26 my $class = shift;
63 3         7 my $caller = caller;
64              
65 3         13 $class->import_into( $caller, @_ );
66             }
67              
68             sub import_into
69             {
70 3     3 0 4 my $class = shift;
71 3         8 my ( $caller, @syms ) = @_;
72              
73 3 100       14 @syms or @syms = qw( divides );
74              
75 3         6 my %syms = map { $_ => 1 } @syms;
  3         13  
76 3 100       21 $^H{"Syntax::Operator::Divides/divides"}++ if delete $syms{divides};
77              
78             {
79 3     3   31 no strict 'refs';
  3         6  
  3         437  
  3         6  
80 3 100       14 *{"${caller}::is_divisor"} = \&is_divisor if delete $syms{is_divisor};
  1         6  
81             }
82              
83 3 50       1360 croak "Unrecognised import symbols @{[ keys %syms ]}" if keys %syms;
  0            
84             }
85              
86             =head1 OPERATORS
87              
88             =head2 %%
89              
90             my $divides = $numerator %% $denominator;
91              
92             Yields true if the numerator operand is a whole integer multiple of the
93             denominator. This is implemented by using the C<%> modulus operator and
94             testing if the remainder is zero.
95              
96             =cut
97              
98             =head1 FUNCTIONS
99              
100             As a convenience, the following functions may be imported which implement the
101             same behaviour as the infix operators, though are accessed via regular
102             function call syntax.
103              
104             These wrapper functions are implemented using L, and thus
105             have an optimising call-checker attached to them. In most cases, code which
106             calls them should not in fact have the full runtime overhead of a function
107             call because the underlying test operator will get inlined into the calling
108             code at compiletime. In effect, code calling these functions should run with
109             the same performance as code using the infix operators directly.
110              
111             =head2 is_divisor
112              
113             my $divides = is_divisor( $numerator, $denominator );
114              
115             A function version of the L operator.
116              
117             =cut
118              
119             =head1 AUTHOR
120              
121             Paul Evans
122              
123             =cut
124              
125             0x55AA;