Sui*_*uic 12
最简单的方法:
print eval('5+2-1');
Run Code Online (Sandbox Code Playgroud)
但这不安全:
print eval('print "You are hacked"');
Run Code Online (Sandbox Code Playgroud)
你需要在eval
它之前检查字符串.您还可以使用Math :: Expression模块或cpan中的许多其他模块:
#!/usr/bin/perl
use strict;
use warnings;
use Math::Expression;
my $env = Math::Expression->new;
my $res = $env->Parse( '5+2-1' );
# my $res = $env->Parse( 'print you are hacked' ); # no math expression here
print $env->Eval( $res );
Run Code Online (Sandbox Code Playgroud)
如果您确定该字符串不包含任何恶意代码,则可以使用eval将其内容视为perl代码.
#!/usr/bin/perl
use strict;
use warnings;
my $string = "5+2-1";
print eval($string);
#print 6
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
947 次 |
最近记录: |