[英]how to read arithmetic data from text file and handle operations in perl
I have a text(example.txt) file which has numbers and arithmetic operations eg 我有一个包含数字和算术运算符的text(example.txt)文件,例如
10+5*6
8+(4*3)
...
I want to handle these operations and generate output like below in another text file (output.txt) 我想处理这些操作并在另一个文本文件(output.txt)中生成以下输出
10 + 5 * 6 = 40
8 + (4 * 3) = 20
...
I'm new to perl, so your help would be appreciated. 我是perl的新手,所以您的帮助将不胜感激。
perl -MSafe -lnE 'say "$_ = ", Safe->new()->reval($_) //"failed"' example.txt
perldoc Safe-在受限的隔离专区中编译和执行代码
这忽略了任何安全问题
perl -ln -e '$a=eval; print "$_ = $a";' < examples.txt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.