我得到了我们假设来证明这一点perl的例子sysopen和printf,但到目前为止,它只表明死亡.
#! /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";
Run Code Online (Sandbox Code Playgroud)
但是当我执行代码时,它就是dies.
myhtml.html cannot be opened. at file_handle.pl line 7.
Run Code Online (Sandbox Code Playgroud)
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";
Run Code Online (Sandbox Code Playgroud)
输出,由于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.
Run Code Online (Sandbox Code Playgroud)
编辑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";
....
Run Code Online (Sandbox Code Playgroud)
mob*_*mob 11
O_RWDR,O_EXCL和,O_CREAT都是Fcntl模块中定义的常量.放线
use Fcntl;
Run Code Online (Sandbox Code Playgroud)
靠近脚本顶部.
这里有很多问题:
use strict;程序的顶部.这将提供一个线索.sysopen失败的原因在于$!变量.您通常应将其包含在任何die消息中.sysopen条目所man perlfunc解释的那样,各种O_*常量都由Fcntl模块导出.use如果要定义那些常量,则需要该模块.正因为如此,你性格明智的or-ing串在一起"O_RDWR","O_EXCL"和"O_CREAT",导致另一个字符串sysopen不知道该怎么做. use strict会阻止这种情况发生.| 归档时间: |
|
| 查看次数: |
5838 次 |
| 最近记录: |