use Math::Symbolic qw/parse_from_string/; my $ms; eval { $ms = parse_from_string($expression) }; if ($@) {...} # catch parse errors my $value; eval { $value = $ms->value() }; if ($@) {...} # catch execution errors #### # parse... unless ( $ms->is_constant() ) { # complain about variables in the tree } my $value; eval { $value = $ms->value() }; if ($@) {...} # catch execution errors #### # parse... # note the parens for list context my ($coderef) = $ms->to_sub(); my $value = $coderef->();