ex.pl") or die "cannot open file for reading: $!-6ren">
gpt4 book ai didi

perl - 为什么在我的代码生成程序中出现此语法错误?

转载 作者:行者123 更新时间:2023-12-02 10:50:48 25 4
gpt4 key购买 nike

我想通过使用Perl中的文件处理来生成一些Perl代码行,例如:

open(FILEHANDLE, ">ex.pl") or die "cannot open file for reading: $!";
print FILEHANDLE "use LWP::UserAgent;"
....
.... some code is here
....
print FILEHANDLE "my \$ua = new LWP::UserAgent(agent => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.5) Gecko/20060719 Firefox/1.5.0.5');"

但是,当我编译生成器代码(而不是生成器)时,出现此错误:
syntax error at F:\test\sys.pl line 14, near "print"
Execution of F:\test\sys.pl aborted due to compilation errors.

我该怎么办?

最佳答案

您错过了最后一个打印字符串末尾(分号之前)的结尾' " '(双引号)。

应该:

print FILEHANDLE "my \$ua = new LWP::UserAgent(agent => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.5) Gecko/20060719 Firefox/1.5.0.5')";

... Firefox/1.5.0.5')"; # To show end of that line without scrolling

另外,还有一些小注意事项:
  • 请考虑使用open()3-argument form,而不是2参数;以及词汇文件句柄:

    打开(我的$ fh,'>',“out.txt”)或死于“打开写入错误:$!”;
    打印$ fh“东西\ n”;
  • 您最后没有文件句柄的close()-我想只是因为您提供的代码不完整。
  • 关于perl - 为什么在我的代码生成程序中出现此语法错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3391839/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com