加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

我的eval是错误的还是我不理解Perl中的eq vs ==

发布时间:2020-12-15 23:30:01 所属栏目:大数据 来源:网络整理
导读:我无法理解eval或者我不理解eq vs ==. 我有这个简短的Perl脚本: [red@tools-dev1 ~]$cat so.pl#!/usr/local/bin/perl -wuse strict;while(DATA) { chomp; my ($arg1,$arg2,$op ) = split /,/; if ( $op eq '=' ) { $op = 'eq'; } my $cmd = "$arg1 $op $arg
我无法理解eval或者我不理解eq vs ==.
我有这个简短的Perl脚本:

[red@tools-dev1 ~]$cat so.pl
#!/usr/local/bin/perl -w
use strict;

while(<DATA>) {
    chomp;
    my ($arg1,$arg2,$op ) = split /,/;
    if ( $op eq '=' ) {
        $op = 'eq';
    }
    my $cmd = "$arg1 $op $arg2";
    print "[$cmd]n";
    my $rc = eval $cmd || 0;
    print "rc is [$rc]n";
}

__DATA__
cat,cat,=

当我执行它时,我得到:

[red@tools-dev1 ~]$./so.pl
[cat eq cat]
rc is [0]

有人会认为你会……

[cat eq cat]
rc is [1]

…因为“猫”等于“猫”,对吧?

解决方法

您在严格模式下使用裸字,这是一个错误:

$perl -e 'use strict; cat eq cat'
Bareword "cat" not allowed while "strict subs" in use at -e line 1.
Bareword "cat" not allowed while "strict subs" in use at -e line 1.
Execution of -e aborted due to compilation errors.

每当你评估一个字符串时,你应该检查$@以查看是否有错误.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读