Skip to content

Instantly share code, notes, and snippets.

@seankelly
Created July 20, 2011 03:48
Show Gist options
  • Save seankelly/1094297 to your computer and use it in GitHub Desktop.
Save seankelly/1094297 to your computer and use it in GitHub Desktop.
diff -u a/librodney.pm b/librodney.pm
--- a/librodney.pm 2011-06-08 11:28:00.000000000 -0500
+++ b/librodney.pm 2011-07-19 22:46:56.565164456 -0500
@@ -3,6 +3,8 @@
use diagnostics; # For development
+use Math::Expression::Evaluator;
+use Try::Tiny;
use POSIX qw( strftime );
@@ -75,17 +77,10 @@
# paramstr_math("3+(2*4)/2")
sub paramstr_math {
- my $str = shift || "";
+ my $expr = shift || "";
- my $retval = 0;
-
- return "err" if ($str =~ m/[^-+*\/0-9() ]$/);
-
- $str = "\$retval = (".$str.");";
-
- eval $str or $retval = "$@";
- $retval =~ s/\n$//;
- return $retval;
+ return unless $expr =~ /^[-\d\+\*\/\(\)]+$/;
+ return try { Math::Expression::Evaluator->new->parse($expr)->val };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment