/usr/share/perl5/Sub/Infix.pm is in libsub-infix-perl 0.004-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | use 5.006;
use strict;
use warnings;
package Sub::Infix;
BEGIN {
$Sub::Infix::AUTHORITY = 'cpan:TOBYINK';
$Sub::Infix::VERSION = '0.004';
}
use Exporter ();
our @ISA = qw( Exporter );
our @EXPORT = qw( infix );
sub infix (&)
{
my $code = shift;
sub () { bless +{ code => $code }, "Sub::Infix::PartialApplication" };
}
{
package Sub::Infix::PartialApplication;
use Carp qw(croak);
BEGIN {
eval { require Scalar::Util; }
? 'Scalar::Util'->import(qw/blessed/)
: eval(q{
require B;
sub blessed ($) {
return undef unless length(ref($_[0]));
my $b = B::svref_2object($_[0]);
return undef unless $b->isa('B::PVMG');
my $s = $b->SvSTASH;
return $s->isa('B::HV') ? $s->NAME : undef;
}
});
};
use overload
q(|) => sub { _apply($_[2] ? @_[1,0] : @_[0,1], "|") },
q(/) => sub { _apply($_[2] ? @_[1,0] : @_[0,1], "/") },
q(<<) => sub { _apply($_[2] ? @_[1,0] : @_[0,1], "<<") },
q(>>) => sub { _apply($_[2] ? @_[1,0] : @_[0,1], ">>") },
q(&{}) => sub { $_[0]->{code} },
q("") => sub { !!1 },
q(0+) => sub { !!1 },
q(bool)=> sub { !!1 },
;
sub _apply
{
my ($left, $right, $op) = @_;
my $self;
if (blessed $left and $left->isa(__PACKAGE__))
{
croak ">>infix<< not supported" if $op eq "<<";
($self = $left)->{right} = $right;
}
elsif (blessed $right and $right->isa(__PACKAGE__))
{
croak ">>infix<< not supported" if $op eq ">>";
($self = $right)->{left} = $left;
}
else
{
croak "incorrect usage of infix operator";
}
if (exists $self->{op})
{
my $combo = join "infix", sort $op, $self->{op};
unless ($combo eq '<<infix>>' or $combo eq '/infix/' or $combo eq '|infix|')
{
croak "$combo not supported";
}
}
else
{
$self->{op} = $op;
}
if (exists $self->{left} and exists $self->{right})
{
return $self->{code}->($self->{left}, $self->{right});
}
return $self;
}
}
1;
__END__
=pod
=encoding utf-8
=head1 NAME
Sub::Infix - create a fake infix operator
=head1 SYNOPSIS
use Sub::Infix;
# Operator needs to be defined (or imported) at compile time.
BEGIN { *plus = infix { $_[0] + $_[1] } };
my $five = 2 |plus| 3;
=head1 DESCRIPTION
Sub::Infix creates fake infix operators using overloading. It doesn't
use source filters, or L<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);
=head2 How does it work?
C<< 2 |plus| 3 >> is parsed by perl as: C<< 2 | ( &plus() | 3 ) >>.
C<< &plus() >> returns an object that overloads the C<< | >> operator;
let's call that C<< $obj >>.
The overloaded C<< $obj | 3 >> operation stashes C<< 3 >> inside
C<< $obj >> noting that the number is the right operand, and returns
C<< $obj >>.
Then C<< 2 | $obj >> is evaluated, stashing C<< 2 >> inside C<< $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.
=begin trustme
=item infix
=end trustme
=head1 BUGS
Please report any bugs to
L<http://rt.cpan.org/Dist/Display.html?Queue=Sub-Infix>.
=head1 SEE ALSO
L<http://code.activestate.com/recipes/384122-infix-operators/>.
=head1 AUTHOR
Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
=head1 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.
=head1 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.
|