Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Hints Towards Writing a Module with New Operators

by duelafn (Vicar)
on Dec 20, 2009 at 16:58 UTC ( #813641=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://813641]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2021-08-04 01:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My primary motivation for participating at PerlMonks is: (Choices in context)








    Results (41 votes). Check out past polls.

    Notices?