这段代码就像我期望的那样死掉:
use strict;
use warnings;
open my $fh, "<", "" or die $!;
Run Code Online (Sandbox Code Playgroud)
但这不是:
use strict;
use warnings;
open my $fh, "<", undef or die $!;
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?
Cha*_*ens 17
在开放的功能有很多的小怪癖的,这就是其中之一:
作为特殊情况,三参数形式具有读/写模式,第三个参数为"undef":
Run Code Online (Sandbox Code Playgroud)open(my $tmp, "+>", undef) or die ...打开一个匿名临时文件的文件句柄.同样使用"+ <"用于对称,但你真的应该考虑先写一些东西到临时文件.你需要寻求()来做阅读.
虽然,正如评论中的ysth注释,文档强烈建议这只应发生在"+"和"> +"模式中.我相信这是实现行为的代码.它不检查模式.我不知道这是不是一个错误,但会在与P5P交谈后报告.
PerlIO *
PerlIO_openn(pTHX_ const char *layers, const char *mode, int fd,
int imode, int perm, PerlIO *f, int narg, SV **args)
{
if (!f && narg == 1 && *args == &PL_sv_undef) {
if ((f = PerlIO_tmpfile())) {
if (!layers || !*layers)
layers = Perl_PerlIO_context_layers(aTHX_ mode);
if (layers && *layers)
PerlIO_apply_layers(aTHX_ f, mode, layers);
}
}
Run Code Online (Sandbox Code Playgroud)
显然,文档在11月的blead perl中得到了修复:
diff --git a/pod/perlfunc.pod b/pod/perlfunc.pod
index 18bb4654e1..1e32cca6dd 100644
--- a/pod/perlfunc.pod
+++ b/pod/perlfunc.pod
@@ -4405,9 +4405,9 @@ argument being L<C<undef>|/undef EXPR>:
open(my $tmp, "+>", undef) or die ...
-opens a filehandle to an anonymous temporary file. Also using C<< +< >>
-works for symmetry, but you really should consider writing something
-to the temporary file first. You will need to
+opens a filehandle to a newly created empty anonymous temporary file.
+(This happens under any mode, which makes C<< +> >> the only useful and
+sensible mode to use.) You will need to
L<C<seek>|/seek FILEHANDLE,POSITION,WHENCE> to do the reading.
Perl is built using PerlIO by default. Unless you've
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
122 次 |
| 最近记录: |