Syntax...Divides(3pm)

πŸ“¦
version 0.08 Β· 2 versions
, in fedora 43

πŸ’– Syntax::Operator::Divides(3pm) πŸ’–

okay bestie, sit down, hydrate, because we're about to talk about one of the cutest little Perl modules in the whole User Contributed Perl Documentation section, and it's literally called Syntax::Operator::Divides. no cap, she's tiny and she's iconic.


✨ NAME ✨

"Syntax::Operator::Divides" β€” an infix operator for division test πŸŽ€

like. an operator. in Perl. that just checks if one number divides another. she ate and left no crumbs.


🌸 SYNOPSIS 🌸

so here's the tea. on Perl v5.38 or later, you can just do this:

use Syntax::Operator::Divides;

say "Multiple of 10" if $x %% 10;

look at that %%. that's the whole vibe. that's it. that's the module. 🫢

Or β€” and this is the part that makes her lowkey a legend β€” you can use her via Syntax::Keyword::Match on Perl v5.14 or later:

use v5.14;
use Syntax::Keyword::Match;
use Syntax::Operator::Divides;

foreach ( 1 .. 100 ) {
   match( $_ : %% ) {
      case(15) { say "FizzBuzz" }
      case(3)  { say "Fizz" }
      case(5)  { say "Buzz" }
      default  { say $_ }
   }
}

FizzBuzz. in a match block. using %%. the way that the case(15) has to come first because 15 is a multiple of both 3 and 5?? main character ordering, honestly. πŸ’…


πŸ¦‹ DESCRIPTION πŸ¦‹

okay so what is she actually doing?

This module provides an infix operator that implements an integer divides test which returns true if the lefthand operand is a whole multiple of the righthand.

translation: $a %% $b is basically asking "hey, is $a a whole multiple of $b?" and she'll tell you yes or no. no drama, no decimals, just whole multiples. ✨

Now, support for custom infix operators was added in the Perl 5.37.x development cycle, and is available from development release v5.37.7 onwards, and therefore in Perl v5.38 onwards. The documentation of XS::Parse::Infix describes the situation in more detail (she's the source of truth, go read her).

While Perl versions before this do not support custom infix operators, they can still be used via "XS::Parse::Infix" and hence XS::Parse::Keyword. Custom keywords which attempt to parse operator syntax may be able to use these. One such module is Syntax::Keyword::Match β€” see the SYNOPSIS example given above πŸ‘† iykyk.

so like, even if your Perl is vintage, you're not locked out of the party. you just gotta come in through the keyword door instead of the operator door. πŸšͺ✨

πŸ‘‘ OPERATORS πŸ‘‘

🌈 %%

my $divides = $numerator %% $denominator;

Yields true if the numerator operand is a whole integer multiple of the denominator.

how's she doing it under the hood? ngl, it's giving humble: This is implemented by using the "%" modulus operator and testing if the remainder is zero.

that's it. % and a zero check. no black magic, just math being math. πŸ’€


🧚 FUNCTIONS 🧚

As a convenience, the following functions may be imported which implement the same behaviour as the infix operators, though are accessed via regular function call syntax. so if you're more of a foo(...) girlie than an operator girlie, she's got you. 🫢

These wrapper functions are implemented using XS::Parse::Infix, and thus have an optimising call-checker attached to them. In most cases, code which calls them should not in fact have the full runtime overhead of a function call, because the underlying test operator will get inlined into the calling code at compiletime. In effect, code calling these functions should run with the same performance as code using the infix operators directly.

not me being a "function call" that isn't even really a function call at runtime. inlined at compiletime. the AUDACITY. πŸ”₯

⭐ is_divisor

my $divides = is_divisor( $numerator, $denominator );

A function version of the "%%" operator. same energy, different outfit. πŸ’œ


🌸 AUTHOR 🌸

Paul Evans <leonerd@leonerd.org.uk> β€” the icon, the moment, the whole entire legend behind this πŸŽ€


and that's the page bestie!! this one shipped as part of perl v5.42.0, dated 2025-07-25, so if you're reading this you're reading the real deal from the User Contributed Perl Documentation. go forth and %% responsibly πŸ’–βœ¨πŸŽ€πŸ¦‹πŸŒΈπŸ’…πŸ«Ά

πŸ“¦ where this page comes from