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

Re: User defined infix operators and whitespace

Thread Previous
Brock Wilcox
August 10, 2017 11:28
Re: User defined infix operators and whitespace
Message ID:
I think there are a couple answers. The simple one is yes -- embrace the
whitespace. Maybe wrap them in ()', like (2 d 6).

Another line of ideas is wrapping things with some other operators. Maybe a
special quoting operator or a converter.

  [[2d6]] # double for dice!
  "2d6":dice # postfix. This used to work but maybe doesn't now
  dice[ 2d6 ] # circumfix

And then the third thing that comes to mind is adding a slang, though that
is, I believe, the least stable language-level approach. Dunno.


On Thu, Aug 10, 2017 at 7:16 AM, Simon Proctor <>

> 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 Previous Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About