develooper Front page | perl.perl5.porters | Postings from April 2021

Pluggable infix operators

Thread Next
From:
Paul "LeoNerd" Evans
Date:
April 1, 2021 13:33
Subject:
Pluggable infix operators
Message ID:
20210401143302.03944822@shy.leonerd.org.uk
I have been beavering away all night, and I have now constructed a
mechanism by which CPAN modules can provide syntax for infix operators,
in a similar technique to the way they can currently provide prefix
operators (aka "keywords").

Here are a few example modules build using this mechanism, based on
ideas from my FOSDEM "Perl in 2025" talk:


 * The `equ` operator which distinguishes undef from empty string:

$ perl -E 'use Syntax::Operator::Equ; say "SAME" if "abc" equ "abc"'
SAME

$ perl -E 'use Syntax::Operator::Equ; say "SAME" if undef equ undef'
SAME

$ perl -E 'use Syntax::Operator::Equ; say "SAME" if undef equ ""';


 * Similarly the `===` which distinguishes undef from zero:

$ perl -E 'use Syntax::Operator::Equ; say "SAME" if undef === undef'
SAME

$ perl -E 'use Syntax::Operator::Equ; say "SAME" if 0 === 0'
SAME

$ perl -E 'use Syntax::Operator::Equ; say "SAME" if undef === 0'


 * Additionally the `in` hyper-operator, which takes as an argument
   another operator to govern how it matches. 

$ perl -MSyntax::Operator::In -E 'say "MATCH" if "c" in<eq> ("a".."c")'
MATCH

$ perl -MSyntax::Operator::In -E 'say "MATCH" if "f" in<eq> ("a".."c")'


$ perl -MSyntax::Operator::In -E 'say "MATCH" if "3.0" in<eq> (1..5)'


$ perl -MSyntax::Operator::In -E 'say "MATCH" if "3.0" in<==> (1..5)'
MATCH


I of course can't put these on CPAN yet because they rely on a
mechanism as-yet-unadded to Perl, but I hope that will become possible
very soon.

-- 
Paul "LeoNerd" Evans

leonerd@leonerd.org.uk      |  https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/  |  https://www.tindie.com/stores/leonerd/

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About