develooper Front page | perl.perl6.users | Postings from August 2017

User defined infix operators and whitespace

Thread Next
From:
Simon Proctor
Date:
August 10, 2017 11:16
Subject:
User defined infix operators and whitespace
Message ID:
CAGwa-jjTARUDs3oF9MO9v8z6Fhj2Vd5CmTGxm-P39-NN7u+=0w@mail.gmail.com
So I had a crazy little idea. I've played the odd bit of roleplaying in my
time and wanted to created a 'd' operator.

Quite simple really.

sub infix:<d> ( Int $num, Int $size ) { [+] (1..$size).roll($num) };

sub prefix:<d> ( Int $size ) { 1 d $size }

Gives us 3 d 6 to roll 3 six sided dice or a prefix d 10 for a single 10
sided dice.

Except... I'd really like to write 3d6 or d10 but the parser barfs.

Am I going to just have to live with that? Or did I miss something obvious?

Obviously it's possible to have operators that ignore whitespace (1+1 works
just fine) but is it possibly for user defined ones?

Possibly more serious ones.

Simon

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