我正在考虑做
open(*STDIN, "<", "/dev/null" );
open(my $fh, "-|", "/bin/bash", "/tmp/foo");
print for <$fh>;'
但是,我想*STDIN
恢复,后来,所以我尝试了。{
open(local *STDIN, "<", "/dev/null" );
open(my $fh, "-|", "/bin/bash", "/tmp/foo");
print for <$fh>;
}
你可以用 cat
试试这个带和不带 local
像这样的关键字,{
# remove local and it works,
open(local *STDIN, "<", "/dev/null" );
open(my $fh, "-|", "/bin/cat");
print for <$fh>;
}
只有没有 local
将 cat
阅读自 /dev/null
.那么local
有什么用实际上是在一个裸字文件句柄上做的吗?
最佳答案
open ...,'-|'
和 system
从标准输入读取的调用将尝试从文件描述符 0 读取。如果你不搞砸,外部程序将从与 perl 的标准输入相同的输入流中读取。
# reads from standard input
open my $fh, '-|', "/bin/cat";
while (<fh>) { print }
# reads from /tmp/foo
open STDIN, "<", "/tmp/foo"; # replaces fd0 with handle to /tmp/foo
open my $fh, '-|', "/bin/cat";
while (<$fh>) { print }
# reads from standard input
open local *STDIN, "<", "/tmp/foo"; # doesn't close fd0, creates new fd
open my $fh, '-|', "/bin/cat";
while (<$fh>) { print }
# reads from /tmp/foo
close STDIN;
open FOO, "<", "/tmp/foo"; # fileno(FOO) should be 0 now
open my $fh, '-|', "/bin/cat";
while (<$fh>) { print }
关于perl - 本地在标准流/裸字文件句柄 : STDIN? 上做什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63710382/