.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Sub::Infix 3pm" .TH Sub::Infix 3pm 2024-03-08 "perl v5.38.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME Sub::Infix \- create a fake infix operator .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 1 \& use Sub::Infix; \& \& # Operator needs to be defined (or imported) at compile time. \& BEGIN { *plus = infix { $_[0] + $_[1] } }; \& \& my $five = 2 |plus| 3; .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" Sub::Infix creates fake infix operators using overloading. It doesn't use source filters, or Devel::Declare, or any of that magic. (Though Devel::Declare isn't magic enough to define infix operators anyway; I know; I've tried.) It's pure Perl, has no non-core dependencies, and runs on Perl 5.6. .PP The price you pay for its simplicity is that you cannot define an operator that can be used like this: .PP .Vb 1 \& my $five = 2 plus 3; .Ve .PP Instead, the operator needs to be wrapped with real Perl operators in one of three ways: .PP .Vb 3 \& my $five = 2 |plus| 3; \& my $five = 2 /plus/ 3; \& my $five = 2 <> 3; .Ve .PP The advantage of this is that it gives you three different levels of operator precedence. .PP You can also call the function a slightly less weird way: .PP .Vb 1 \& my $five = plus\->(2, 3); .Ve .SS "How does it work?" .IX Subsection "How does it work?" \&\f(CW\*(C`2 |plus| 3\*(C'\fR is parsed by perl as: \f(CW\*(C`2 | ( &plus() | 3 )\*(C'\fR. .PP \&\f(CW&plus()\fR returns an object that overloads the \f(CW\*(C`|\*(C'\fR operator; let's call that \f(CW$obj\fR. .PP The overloaded \f(CW\*(C`$obj | 3\*(C'\fR operation stashes \f(CW3\fR inside \&\f(CW$obj\fR noting that the number is the right operand, and returns \&\f(CW$obj\fR. .PP Then \f(CW\*(C`2 | $obj\*(C'\fR is evaluated, stashing \f(CW2\fR inside \f(CW$obj\fR as the left operand. At this point, the object notices that it has both operands, and calls the coderef from the definition of the operator, passing it both operands. .SH BUGS .IX Header "BUGS" Please report any bugs to . .SH "SEE ALSO" .IX Header "SEE ALSO" . .SH AUTHOR .IX Header "AUTHOR" Toby Inkster . .SH "COPYRIGHT AND LICENCE" .IX Header "COPYRIGHT AND LICENCE" This software is copyright (c) 2013\-2014 by Toby Inkster. .PP This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. .SH "DISCLAIMER OF WARRANTIES" .IX Header "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.