【发布时间】:2011-03-24 10:33:07
【问题描述】:
我想通过在 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.
我要做什么?
【问题讨论】:
-
第一行是矛盾的:你打开写,但错误信息说“读”,这是误导。
-
@Svante:复制和粘贴.. 宇宙中 54.8% 的错误的来源。
-
你为什么要编写生成代码的代码?这是一件不寻常的事情,尤其是在基本脚本中。
-
由于行长不容易看到:在问题中它是 Firefox/1.5.0.5');" ----在 DVK 的回答中是 Firefox/1.5.0.5')";
标签: perl dynamic code-generation compiler-errors perl-io