#! /usr/bin/perl -w use CGI qw (:standard); use CGI::Carp qw(warningsToBrowser fatalsToBrowser); $| = 1; sub roll { my @rolls = (); my $rolled = ''; my $dice = shift || ''; if($dice =~ m!(\d+)[Dd ]+(\d+)!) { $rolls[$_] = 1 + int(rand($2)) for (0..$1-1); my $tot = 0; $tot+=$_ foreach @rolls; $rolled = "$1d$2 (sum $tot)"; } elsif ($dice =~ m!(\d+)!) { $rolls[0] = 1 + int rand $1; $rolled = "[1, $1]"; } else { return "ERROR: Could not roll '$dice'. Text back \"dice NUMBER\" or \"dice NUM d NUM\""; } return "$rolled: " . join(', ', @rolls); } my $result = roll((param("dice") || '')); print <