use strict; use warnings; my %hash = (a => 1, b => 0, c => 3); while (){ chomp; s/(\w+)/\$hash{$1}/g; my $ans = eval "$_"; if ($@) { print "Eval of $_ failed:\n $@\n"; } else { print "$_ = $ans\n"; } } __DATA__ a/b b+c c*a/(b+a) #### Eval of $hash{a}/$hash{b} failed: Illegal division by zero at (eval 10)[noname.pl:10] line 1, line 1. $hash{b}+$hash{c} = 3 $hash{c}*$hash{a}/($hash{b}+$hash{a}) = 3