http://www.perlmonks.org?node_id=813641


in reply to Hints Towards Writing a Module with New Operators

Sounds like fun!:

# IHeartFortran.pm package IHeartFortran; use strict; use warnings; require Exporter; our @ISA = qw( Exporter ); our @EXPORT = qw/ EQV NEQV /; use overload '.' => \&_do_op; sub _do_op { my ($self, $y, $reverse) = @_; $$self{ $reverse ? 'left' : 'right' } = $y; if (exists($$self{left}) and exists($$self{right})) { return $$self{op}->(@$self{qw/ left right /}); } return $self; } sub new { my ($class, $op) = @_; bless { op => $op }, $class; } sub EQV() { __PACKAGE__->new( sub { $_[0] == $_[1] ? 1 : 0 } ) } sub NEQV() { __PACKAGE__->new( sub { $_[0] != $_[1] ? 1 : 0 } ) }

Then to use it:

#!/usr/bin/perl use strict; use warnings; use 5.010; use IHeartFortran; say 1 .EQV. 1; say 1 .EQV. 2; say 1 .NEQV. 1; say 1 .NEQV. 2;

This doesn't exactly create a .EQV. operator (for instance, spaces would be allowed:  1 . EQV . 2), but it looks good enough.

update: Actually, I think FORTRAN's whitespace rules would allow extra spaces anyway, so perhaps this is a somewhat faithful recreation anyway. Case flexibility on the other hand...

Good Day,
    Dean