README
1NAME
2 Sub::Infix - create a fake infix operator
3
4SYNOPSIS
5 use Sub::Infix;
6
7 # Operator needs to be defined (or imported) at compile time.
8 BEGIN { *plus = infix { $_[0] + $_[1] } };
9
10 my $five = 2 |plus| 3;
11
12DESCRIPTION
13 Sub::Infix creates fake infix operators using overloading. It doesn't use
14 source filters, or Devel::Declare, or any of that magic. (Though
15 Devel::Declare isn't magic enough to define infix operators anyway; I
16 know; I've tried.) It's pure Perl, has no non-core dependencies, and runs
17 on Perl 5.6.
18
19 The price you pay for its simplicity is that you cannot define an operator
20 that can be used like this:
21
22 my $five = 2 plus 3;
23
24 Instead, the operator needs to be wrapped with real Perl operators in one
25 of three ways:
26
27 my $five = 2 |plus| 3;
28 my $five = 2 /plus/ 3;
29 my $five = 2 <<plus>> 3;
30
31 The advantage of this is that it gives you three different levels of
32 operator precedence.
33
34 You can also call the function a slightly less weird way:
35
36 my $five = plus->(2, 3);
37
38 How does it work?
39 `2 |plus| 3` is parsed by perl as: `2 | ( &plus() | 3 )`.
40
41 `&plus()` returns an object that overloads the `|` operator; let's call
42 that $obj.
43
44 The overloaded `$obj | 3` operation stashes 3 inside $obj noting that the
45 number is the right operand, and returns $obj.
46
47 Then `2 | $obj` is evaluated, stashing 2 inside $obj as the left operand.
48 At this point, the object notices that it has both operands, and calls the
49 coderef from the definition of the operator, passing it both operands.
50
51BUGS
52 Please report any bugs to
53 <http://rt.cpan.org/Dist/Display.html?Queue=Sub-Infix>.
54
55SEE ALSO
56 <http://code.activestate.com/recipes/384122-infix-operators/>.
57
58AUTHOR
59 Toby Inkster <tobyink@cpan.org>.
60
61COPYRIGHT AND LICENCE
62 This software is copyright (c) 2013-2014 by Toby Inkster.
63
64 This is free software; you can redistribute it and/or modify it under the
65 same terms as the Perl 5 programming language system itself.
66
67DISCLAIMER OF WARRANTIES
68 THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
69 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
70 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
71
72