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

User defined infix operators and whitespace

Thread Next
Simon Proctor
August 10, 2017 11:16
User defined infix operators and whitespace
Message ID:
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.


Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About