【问题标题】:What is this compilation error in perl? I do not see a syntax problemperl 中的这个编译错误是什么?我没有看到语法问题
【发布时间】:2011-08-18 19:58:58
【问题描述】:
#!/usr/bin/perl
use strict;
use warnings;

open(TEST, "leet.txt") or die "Can't open leet.txt: $!\n";
while(my $line = <TEST>) {
    if($line =~ tr/34/ea/)
        print <<EOF;
$line
EOF
}

它产生这个: ./practice.pl 第 11 行的语法错误,靠近 ") 打印” ./practice.pl 的执行由于编译错误而中止。

【问题讨论】:

    标签: perl


    【解决方案1】:

    您必须将if 命令包含在{ } 块中,即使它只有一个命令。与其他语言不同,在 Perl 中,这不是可选的。

        if($line =~ tr/34/ea/) {
            print <<EOF;
    $line
    EOF
        }
    

    【讨论】:

      【解决方案2】:

      我想您只是跳过了在末尾粘贴两个“}”。然后添加“{”:

      if($line =~ tr/34/ea/) {

      【讨论】:

        【解决方案3】:

        要有条件地运行单个语句,请将条件放在语句之后。例如,这两个语句具有相同的行为:

        if ($bar) { print "foo!\n"; }
        print "foo!\n" if ($bar);
        

        对于你的代码,你可以这样写:

        #!/usr/bin/perl
        use strict;
        use warnings;
        
        open(TEST, "leet.txt") or die "Can't open leet.txt: $!\n";
        while(my $line = <TEST>) {
                print <<EOF if ($line =~ tr/34/ea/);
        $line
        EOF
        }
        close TEST;
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-06-19
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多