Sub::Infix(3pm)

πŸ’– Sub::Infix β€” create a fake infix operator πŸ’–

okay so this is Sub::Infix, a little Perl module from the User Contributed Perl Documentation corner of your friendly neighbourhood perl man pages, and bestie, it does exactly one thing and it does it adorably: it lets you build a fake infix operator. yes really. let's get into it πŸ¦‹


✨ SYNOPSIS ✨

like, here's the whole vibe in four lines:

use Sub::Infix;

# Operator needs to be defined (or imported) at compile time.
BEGIN { *plus = infix { $_[0] + $_[1] } };

my $five = 2 |plus| 3;

and yes the BEGIN block is not optional, more on that below πŸ’…


🌸 DESCRIPTION 🌸

so Sub::Infix makes fake infix operators using overloading. that's it. that's the trick.

it does not use source filters. it does not use Devel::Declare. it does not use any of that magic. (and honestly? Devel::Declare isn't even magic enough to define infix operators anyway β€” I know, I've tried.) it's pure Perl, it has no non-core dependencies, and it runs on Perl 5.6. iconic behaviour tbh πŸ‘‘

now. the price you pay for that simplicity β€” and bestie there is a price, I'm not gonna lie to you β€” is that you cannot define an operator that works like this:

my $five = 2 plus 3;

nope. not happening. instead, your operator has to be wrapped in real Perl operators, and you've got three flavours to pick from:

my $five = 2 |plus| 3;
my $five = 2 /plus/ 3;
my $five = 2 <<plus>> 3;

and here's the slay part: the advantage of this is that it gives you three different levels of operator precedence. three vibes, three precedences, pick your fighter πŸ’…

you can also call the function a slightly less weird way, if the pipe-and-slash situation is giving you a headache:

my $five = plus->(2, 3);

🧚 How does it work?

okay so this is the fun part, buckle up.

2 |plus| 3 is parsed by perl as:

2 | ( &plus() | 3 )

&plus() returns an object that overloads the | operator. let's call that $obj. πŸŽ€

the overloaded $obj | 3 operation stashes 3 inside $obj, noting that the number is the right operand, and returns $obj. sneaky. I love her.

then 2 | $obj gets evaluated, stashing 2 inside $obj as the left operand. and at this point the object notices it has both operands β€” both hands full, main character energy β€” and calls the coderef from the definition of the operator, passing it both operands. ✨ periodt.


πŸ’€ BUGS πŸ’€

found a bug? not me manifesting that, but if it happens, please report any bugs to:

<http://rt.cpan.org/Dist/Display.html?Queue=Sub-Infix>


πŸ‘€ SEE ALSO πŸ‘€


🫢 AUTHOR 🫢

Toby Inkster <tobyink@cpan.org>.


This software is copyright (c) 2013-2014 by Toby Inkster.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. iykyk πŸ’œ


😭 DISCLAIMER OF WARRANTIES 😭

THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.

like, no promises bestie, she said what she said πŸ™ˆ


and that's the whole page fr fr β€” this was perl v5.36.0, dated 2022-11-19, and it's Sub::Infix(3pm) forever and always. go make some fake operators, you're gonna eat πŸ’…βœ¨

πŸ“¦ where this page comes from