Perl 为什么打开undef不会失败?
正如我所期望的那样,此代码将消亡:Perl 为什么打开undef不会失败?,perl,undef,Perl,Undef,正如我所期望的那样,此代码将消亡: use strict; use warnings; open my $fh, "<", "" or die $!; 使用严格; 使用警告; 打开我的$fh,“该函数有许多小怪癖,这是其中之一: 作为一种特殊情况,三参数形式具有读/写模式和 第三个参数是“unde”: 打开匿名临时文件的文件句柄。也可以使用“+”,undef)或die。。。 -打开匿名临时文件的文件句柄。也可以使用C> 工作是对称的,但你确实应该考虑写些东西。 -首先,您需要创建临时文
use strict;
use warnings;
open my $fh, "<", "" or die $!;
使用严格;
使用警告;
打开我的$fh,“该函数有许多小怪癖,这是其中之一:
作为一种特殊情况,三参数形式具有读/写模式和
第三个参数是“unde”:
打开匿名临时文件的文件句柄。也可以使用“+”,undef)或die。。。
-打开匿名临时文件的文件句柄。也可以使用C>
工作是对称的,但你确实应该考虑写些东西。
-首先,您需要创建临时文件
+打开新创建的空匿名临时文件的文件句柄。
+(这在任何模式下都会发生,这使得C>>成为唯一有用且
+要使用的合理模式。)您需要
我来做阅读。
Perl默认使用PerlIO构建
是的,这就是正在发生的事情,但是doc似乎是错的,因为这显然不是读/写模式读/写模式是+>
,第三个参数是undef
。它只是用了poorlyHmm,很好的一点@ysth,我愤怒地掩饰了它。我读它是读或写模式,但很明显它是r读+写或写+读模式。因此,这要么是一个bug,要么是文档记录不充分。
use strict;
use warnings;
open my $fh, "<", undef or die $!;
open(my $tmp, "+>", undef) or die ...
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);
}
}
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