Perl: the Markov chain saw PerlMonks

### Comment on

 Need Help??

A dice roll script is generally useful. Minimally (without getting golfy), it could look like this:

```use strict;
use warnings;

while(1) {

print "Enter a dice to roll ('q' to quit): ";
my \$input = <STDIN>;

last if \$input =~ m/^q/;

my( \$rolls, \$die ) = \$input =~ m/(\d+)d(\d+)/i;

my \$result = 0;
\$result += 1 + int rand \$die for 1 .. \$rolls;

print \$result, "\n";

}

If I plan to use it as more than a throwaway script, here's how I would do it:

```use 5.010;
use strict;
use warnings;
use IO::Prompt::Hooked;

^\s*                                        # Start of line, ignore
+whitespace
(?&POS_INT)?                                # How many rolls (option
+al).
\s* d \s*                                   # 'd' (forgiving of whit
+espace)
(?&POS_INT)                                 # Die to roll.
\s*\$                                        # End of input, ignore w
+hitespace.
(?(DEFINE) (?<POS_INT> [1-9][0-9]{0,2}? ) ) # Definition of positive
+ int.
}x;

my \$roll;

while (defined(my \$input = ask(\$roll // \$DEFAULT_ROLL))) {

\$roll = interpret_input(\$input);

print "Result of ( \$roll ) is [ \$result ].\n\n";

}

sub interpret_input {
my \$i = shift;
\$i =~ s/\s+//g;
\$i =~ s/^d/1d/;
return \$i;
}

my \$default = shift;
return prompt(
default  => \$default,
message  => 'Enter a dice roll ("q" to quit): ',
validate => \$RE_GOOD_ROLL,
error    => "Input must be in format of d8, or 2d6.\n\n",
escape   => qr/^ \s* q (?: uit )? /ix,    # q or quit, case insens
+itively.
);
}

The second snippet adds input validation, default input, and allows input like "d6" as synonymous with "1d6".

This example sets a default roll of '1d6'. It then prompts the user to enter a roll, 'q' or 'quit' to quit (case insensitively), or 'enter' to accept the default roll. After the first roll, all future rolls will have their default set to the previous roll, so that a user can enter '2d20', and then just hit enter again to repeat the roll.

User input is validated, and constrained to three digit roll counts, and three digit dice. Upon receiving invalid input, a message is provided to the user explaining valid input.

While it's not difficult to build a dice roll generator, I used Games::Dice::Advanced because it is already more fully developed, has its own reasonable error handling, and is a good fit for our use. IO::Prompt::Hooked handles the prompting, and facilitates validation, defaults, bad input message, and input termination. We also define a regex snippet that matches basic non-zero positive integers of up to three digits, for our roll count and die type.

Here's a sample run:

```\$ ./diceroll.pl
Enter a dice roll ("q" to quit):  [1d6]
Result of ( 1d6 ) is [ 3 ].

Enter a dice roll ("q" to quit):  [1d6] 2d4
Result of ( 2d4 ) is [ 3 ].

Enter a dice roll ("q" to quit):  [2d4] 3d100
Result of ( 3d100 ) is [ 122 ].

Enter a dice roll ("q" to quit):  [3d100]
Result of ( 3d100 ) is [ 153 ].

Enter a dice roll ("q" to quit):  [3d100] 34c
Input must be in format of d8, or 2d6.

Enter a dice roll ("q" to quit):  [3d100] d6
Result of ( 1d6 ) is [ 2 ].

Enter a dice roll ("q" to quit):  [1d6] q

Dave

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

• Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
• Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
• Read Where should I post X? if you're not absolutely sure you're posting in the right place.
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
• You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
 For: Use: & & < < > > [ [ ] ]
• Link using PerlMonks shortcuts! What shortcuts can I use for linking?

Create A New User
Chatterbox?
and the daffodils sway...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (12)
As of 2017-10-23 13:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My fridge is mostly full of:

Results (279 votes). Check out past polls.

Notices?