Not knowing Perl, I think the thing you're looking for is probably eval, or some variant thereof. What you want to do is treat the read-in string as a little Perl program to be executed.
Cases 1 and 2 above behave identically: they run the code
contained in the variable $x. (Although case 2 has misleading
double quotes making the reader wonder what else might be
happening (nothing is).) Cases 3 and 4 likewise behave in the
same way: they run the code '$x', which does nothing but return
the value of $x. (Case 4 is preferred for purely visual reasons,
but it also has the advantage of compiling at compile-time
instead of at run-time.) Case 5 is a place where normally you
*would* like to use double quotes, except that in this
particular situation, you can just use symbolic references
instead, as in case 6.
It's not really putting quotation marks around the input. Perl is a dynamically typed language, which means the type of your variable is determined by what you assign to it. Perl, and pretty much every other programming language, reads data from a file as a string, so the variable you assign the data to will be a string. In order to evaluate the expression you will have to parse the string into a form that is possible for an algorithm to evaluate. This involves tokenizing the string, converting it to reverse Polish notation, and evaluating then equation using a stack. This is not a trivial task, and it's beyond my ability to explain since I haven't done it in some time, but looking up equation parser in google should help you out.
Edit: Or maybe there's a library function that does it for you. Guess I don't know Perl very well.
# convert to numbers
my ($var1, $var2, $var3) = split (/\s{0,}\+\s{0,}/, $testExpression);
$testExpression = $var1 + $var2 + $var3;
So you could do that, but eval would be way more flexible if you want to be able to do something other than addition (I'm not actually sure what the easiest way to do that would be)
Posts
blah2 outputs 1.5
I've never used eval before, but it looks like it does exactly what you want
Edit: Or maybe there's a library function that does it for you. Guess I don't know Perl very well.
So you could do that, but eval would be way more flexible if you want to be able to do something other than addition (I'm not actually sure what the easiest way to do that would be)
Here's an example of a quick and dirty command-line calculator that I wrote that basically just uses Perl's parser to calculate by using eval:
You would type something like:
perl calc.pl 1 + 2 - 8 / 2