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>.
π COPYRIGHT AND LICENCE π
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
- debian bookworm: libsub-infix-perl 0.004-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libsub-infix-perl 0.004-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms