I dislike using eval myself unless absolutely necessary. Why not just use linked subs:
use strict;
use warnings;
my (%ops, $first, $second, $op, $f);
%ops = (
'+' => { 'label' => 'Sum', 'op' => sub { return $_[0] + $_[1]; } }
+,
'-' => { 'label' => 'Difference', 'op' => sub { return $_[0] - $_[
+1]; } },
'*' => { 'label' => 'Product', 'op' => sub { return $_[0] * $_[1];
+ } },
'/' => { 'label' => 'Quotient', 'op' => sub { return $_[0] / $_[1]
+; } }
);
$ops{'M'} = { 'label' => 'Multiplication', 'table' => 1, 'op' => $ops{
+'*'}{'op'} };
sub getval {
chomp($_ = <STDIN>);
($_) = m/($_[0])/ if $_[0];
return $_;
}
while (1) {
print "Enter the first number: ";
last if !($first = getval('\d+'));
print "Enter the second number: ";
last if !($second = getval('\d+'));
print <<TEXT;
Please enter the operation that you want to perform:
+ - * /
M = Mulitiplication Table
TEXT
last if !$ops{$op = getval()};
print "\n";
if (!$ops{$op}{'table'}) {
print "$ops{$op}{'label'} of $first $op $second is ",
$ops{$op}{'op'}->($first, $second);
}
else {
print ' ', join ' ', 1..$second;
for $f (1..$first) {
print "\n";
print join ' ', $f, map { $ops{$op}{'op'}->($f, $_) } 1..$
+second;
}
}
print "\n\n";
}