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 -- leonerd@leonerd.org.uk
5              
6             package Syntax::Operator::Divides 0.02;
7              
8 3     3   135213 use v5.14;
  3         32  
9 3     3   19 use warnings;
  3         7  
  3         84  
10              
11 3     3   16 use Carp;
  3         6  
  3         609  
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 suitably-patched perl:
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 versions of perl do not directly support custom infix operators. The
50             documentation of L describes the situation, with reference
51             to a branch experimenting with this new feature. This module is therefore
52             I entirely useless on standard perl builds. While the regular parser
53             does not support custom infix operators, they are supported via
54             C and hence L, and so custom keywords
55             which attempt to parse operator syntax may be able to use it. One such module
56             is L; see the SYNOPSIS example given above.
57              
58             =cut
59              
60             sub import
61             {
62 3     3   26 my $class = shift;
63 3         8 my $caller = caller;
64              
65 3         12 $class->import_into( $caller, @_ );
66             }
67              
68             sub import_into
69             {
70 3     3 0 7 my $class = shift;
71 3         6 my ( $caller, @syms ) = @_;
72              
73 3 100       14 @syms or @syms = qw( divides );
74              
75 3         7 my %syms = map { $_ => 1 } @syms;
  3         12  
76 3 100       22 $^H{"Syntax::Operator::Divides/divides"}++ if delete $syms{divides};
77              
78             {
79 3     3   24 no strict 'refs';
  3         6  
  3         478  
  3         6  
80 3 100       16 *{"${caller}::is_divisor"} = \&is_divisor if delete $syms{is_divisor};
  1         6  
81             }
82              
83 3 50       1389 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;