GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages


Manual Reference Pages  -  SUB::INFIX (3)

.ds Aq ’

NAME

Sub::Infix - create a fake infix operator

CONTENTS

SYNOPSIS



   use Sub::Infix;
  
   # Operator needs to be defined (or imported) at compile time.
   BEGIN { *plus = infix { $_[0] + $_[1] } };
  
   my $five = 2 |plus| 3;



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.

The price you pay for its simplicity is that you cannot define an operator that can be used like this:



   my $five = 2 plus 3;



Instead, the operator needs to be wrapped with real Perl operators in one of three ways:



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



The advantage of this is that it gives you three different levels of operator precedence.

You can also call the function a slightly less weird way:



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



    How does it work?

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.

Then 2 | $obj is evaluated, stashing 2 inside $obj 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.

BUGS

Please report any bugs to <http://rt.cpan.org/Dist/Display.html?Queue=Sub-Infix>.

SEE ALSO

<http://code.activestate.com/recipes/384122-infix-operators/>.

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.

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.
Search for    or go to Top of page |  Section 3 |  Main Index


perl v5.20.3 SUB::INFIX (3) 2014-07-02

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with manServer 1.07.