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

Dice::Simpler with Filter::Simple (Re: Reinventing Dice...)

by osfameron (Hermit)
on Dec 08, 2001 at 23:58 UTC ( #130449=note: print w/ replies, xml ) Need Help??

in reply to Reinventing Dice...

Was thinking about how to improve this, and wondered if it was possible to make 'd' a binary operator, e.g. just write 3d6 in your Perl code and have the d operator get passed 3 and 6 as parameters, but having checked out overload and TheDamian's OO book I think it can't be done.

I then wrote a version that overloaded strings, so you could type $x='3d6' but really I think that's not that much more intuitive. So...

As I've played recently with TheDamian's Filter::Simple I wrote a helper module that allows you to write $x=3d6 in your script. As always comments welcome...
package Dice::Simpler; use strict; use warnings; use Filter::Simple; use Dice::Simple qw(roll); our @ISA=qw(Exporter); our @EXPORT=qw(roll); FILTER_ONLY code => sub { s{((?:\btt\d+\s+)?(?:\d+|\b)d\d+)} # note use of \b. We don't want to filter other expressions # that ending in 'd' and a number {roll('$1')}g; } __DATA__ =head1 NAME Dice::Simpler - an even simpler interface to RPG style dice. =head1 SYNOPSIS use Dice::Simpler; my $str= tt3 6d6; # best 3 of 6 6-sided dice my $dex= 3d8; # 3 8-sided dice my $cha= 4d4+1; # 4 4-sided dice + 1 print <<CHARACTER; Your new character has: Strength: $str Dexterity: $dex Charisma: $cha =============== CHARACTER print "You attack the Orc:\n"; if (3d6 < $str) { print "You killed the Orc"; } else { print "You hit the Orc but it glanced off his armour"; } =head1 DESCRIPTION Uses L<Dice::Simple> and the very funky L<Filter::Simple> to allow you + to use the RPG style '3d6' syntax for dice rolling directly in Perl. See the + Dice::Simple documentation for more details. =head1 AUTHOR, LICENSE Version: 0.01 8th Dec 2001 Untested. No warranty implied. May be distributed under the same terms as Perl itself. (c) /msg osfameron =head1 BUGS =over 4 =item 1 (OPEN 2001/12/8) '3d6' notation isn't filtered in quotelikes like double quoted strings. But it *does* seem to be in <<heredocs. Not s +ure why. B<Update:> but Damian Conway himself is aware of it, and it +is on his looooong todo list, thanks! =back =cut int(rand 6)+1;

Comment on Dice::Simpler with Filter::Simple (Re: Reinventing Dice...)
Select or Download Code
Re: Dice::Simpler with Filter::Simple (Re: Reinventing Dice...)
by TheDamian (Priest) on Dec 09, 2001 at 04:09 UTC
    '3d6' notation isn't filtered in quotelikes like double quoted strings. But it *does* seem to be in heredocs. Not sure why.

    Looks like a deep and mysterious bug in Text::Balanced's parsing of heredocs. I'm working on it but, due to the current number of ToDo's vastly exceeding the available supply of Damians, the fix may be some time coming.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (13)
As of 2014-04-24 12:49 GMT
Find Nodes?
    Voting Booth?

    April first is:

    Results (565 votes), past polls