2017-04-20 64 views

回答

17

open功能有很多小怪癖的,这就是其中之一:

作为一个特殊的情况下,三个参数的形式与读/写模式和 第三个参数是“民主基金”:

open(my $tmp, "+>", undef) or die ... 

打开文件句柄到匿名临时文件。同样使用“+ <” 适用于对称性,但您应该首先考虑将临时文件写入 。您需要寻求()来完成 阅读。

尽管正如注释中提到的那样,但文档强烈建议这只应发生在“+ <”和“> +”模式下。我相信这是实现行为的代码。它不检查模式。我不知道这是否是一个错误,但会在与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); 
     } 
    } 

显然,文档是fixed in blead perl in November

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 
+1

是的,这是发生了什么,但文档似乎是错的,因为这显然不是一个读/写模式 – ysth

+0

读/写模式是'+>'第三个参数是'undef'。它只是措辞不佳 –

+0

嗯,好点@ysth,我愤怒地掩饰它。我将其作为读取或写入模式读取,但它非常明确地表示它用于读取+写入或写入+读取模式。所以,这是一个错误或记录不完整。 –

相关问题