Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: Using user input to return variable typed.

by davido (Archbishop)
on Nov 13, 2012 at 18:17 UTC ( #1003684=note: print w/replies, xml ) Need Help??

in reply to Using user input to return variable typed.

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 Readonly; use Games::Dice::Advanced; use IO::Prompt::Hooked; Readonly my $RE_GOOD_ROLL => qr{ ^\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; Readonly my $DEFAULT_ROLL => '1d6'; my $roll; while (defined(my $input = ask($roll // $DEFAULT_ROLL))) { $roll = interpret_input($input); my $result = Games::Dice::Advanced->roll($roll); print "Result of ( $roll ) is [ $result ].\n\n"; } sub interpret_input { my $i = shift; $i =~ s/\s+//g; $i =~ s/^d/1d/; return $i; } sub ask { 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:

$ ./ 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


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1003684]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2018-05-22 01:06 GMT
Find Nodes?
    Voting Booth?