我得到了这个 perl 示例,它应该演示sysopen
and printf
,但到目前为止它只演示了 die。
#! /usr/bin/perl
$filepath = 'myhtml.html';
sysopen (HTML, $filepath, O_RDWR|O_EXCL|O_CREAT, 0755)
or die "$filepath cannot be opened.";
printf HTML "<html>\n";
但是当我执行代码时它只是die
s。
myhtml.html cannot be opened. at file_handle.pl line 7.
myhtml.html
不存在,但它应该是由O_CREAT
标志创建的。不应该吗?
编辑
我已经编辑了代码以包含关于use strict
和的建议$!
。下面是新代码及其结果。
#! /usr/bin/perl
use strict;
$filepath = "myhtml.html";
sysopen (HTML, '$filepath', O_RDWR|O_EXCL|O_CREAT, 0755)
or die "$filepath cannot be opened. $!";
printf HTML "<html>\n";
输出,由于use strict
,给了我们一大堆错误:
Global symbol "$filepath" requires explicit package name at file_handle.pl line 3.
Global symbol "$filepath" requires explicit package name at file_handle.pl line 5.
Bareword "O_RDWR" not allowed while "strict subs" in use at file_handle.pl line 5.
Bareword "O_EXCL" not allowed while "strict subs" in use at file_handle.pl line 5.
Bareword "O_CREAT" not allowed while "strict subs" in use at file_handle.pl line 5.
Execution of file_handle.pl aborted due to compilation errors.
编辑 2
根据大家的建议和帮助,这里是最终的工作代码:
#! /usr/bin/perl
use strict;
use Fcntl;
my $filepath = "myhtml.html";
sysopen (HTML, $filepath, O_RDWR|O_EXCL|O_CREAT, 0755)
or die "$filepath cannot be opened. $!";
printf HTML "<html>\n";
....