0

I have a text(example.txt) file which has numbers and arithmetic operations e.g.
10+5*6
8+(4*3)
...
I want to handle these operations and generate output like below in another text file (output.txt)
10 + 5 * 6 = 40
8 + (4 * 3) = 20
...
I'm new to perl, so your help would be appreciated.

Raghav2580
  • 256
  • 2
  • 10

2 Answers2

3
perl -MSafe -lnE 'say "$_ = ", Safe->new()->reval($_) //"failed"' example.txt

perldoc Safe - Compile and execute code in restricted compartments

mpapec
  • 50,217
  • 8
  • 67
  • 127
0

This ignores any security concerns

perl -ln -e '$a=eval; print "$_ = $a";' < examples.txt 
Vorsprung
  • 32,923
  • 5
  • 39
  • 63