4.12.1 Evaluating Terminal Input
4.12.1 Evaluating Terminal Input
Let us write a program that works like a simple calculator. We type in an arithmetic expression and it returns the result after evaluating the expression. In fact, this program can do more. If we type in any Perl expression in a single line, it executes the expression and prints the result.
The program is surprisingly simple and is given below.
Program 4.47
#!/usr/bin/perl while (){ print eval $_, "\n"; }
The program reads a line from the terminal and evaluates it and prints the result. A simple interaction with this program is given below.
1+2*3;
7
2 * log (10)
4.60517018598809
$a = 0;
0
$a=10; $a = $a + 100;
110
for (1..10){print $_ x 3, " "}
111 222 333 444 555 666 777 888 999 101010
The user types in an expression and the program prints out a result. The first line of a pair in the transcript is user input and the second line is the computer’s output. This continues till the user finishes the interaction by typing in control-D (in a Unix environment).
