9
此代碼爲死去我希望它:爲什麼打開一個undef不會失敗?
use strict;
use warnings;
open my $fh, "<", "" or die $!;
但這並不:
use strict;
use warnings;
open my $fh, "<", undef or die $!;
這到底是怎麼回事?
此代碼爲死去我希望它:爲什麼打開一個undef不會失敗?
use strict;
use warnings;
open my $fh, "<", "" or die $!;
但這並不:
use strict;
use warnings;
open my $fh, "<", undef or die $!;
這到底是怎麼回事?
的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
是的,這是發生了什麼,但文檔似乎是錯的,因爲這顯然不是一個讀/寫模式 – ysth
讀/寫模式是'+>'第三個參數是'undef'。它只是措辭不佳 –
嗯,好點@ysth,我憤怒地掩飾它。我將其作爲讀取或寫入模式讀取,但它非常明確地表示它用於讀取+寫入或寫入+讀取模式。所以,這是一個錯誤或記錄不完整。 –