< UNIX 中的运算符,传递给 Perl 脚本
计算 if(-t STDIN)
时, UNIX 运算符算作 STDIN 吗?如果没有,我如何获取该数据?
所以有人输入 perl example.pl <测试.txt
。这与通过 ls | 管道输入的数据不同。 ./example.pl。我怎样才能得到这种行为?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
测试
-p STDIN
,检查文件句柄STDIN
是否附加到管道。但我不确定我是否理解你的问题。在所有这三种情况下,
输入都是相同的,并且都可以通过 STDIN 文件句柄访问。在前两种情况下,
-t STDIN
的计算结果为 false,在第二种情况下,-p STDIN
将为 true。这三种情况之间的行为差异很微妙,而且通常并不重要。显然,第三种情况将等待至少一行输入(以“\n”或 EOF 结尾)。前两种情况之间的差异更加微妙。当程序的输入从另一个进程的输出通过管道传输时,您在延迟或该程序是否缓冲其输出方面会受到第一个进程的支配。
时,也许你可以扩展一下你的意思
当你说“行为不像”
Test
-p STDIN
, which checks if the filehandleSTDIN
is attached to a pipe.But I'm not sure I understand your question. In all three of these cases
the inputs are the same and all accessible through the
STDIN
filehandle. In the first two cases,-t STDIN
will evaluate to false, and in the second case,-p STDIN
will be true.The differences in behavior between these three cases are subtle, and usually not important. The third case, obviously, will wait until at least one line of input (terminated with "\n" or EOF) is received. The difference between the first two cases is even more subtle. When the input to your program is piped from the output of another process, you are somewhat at the mercy of that first process with respect to latency or whether that program buffers its output.
Maybe you could expand on what you mean when you say
doesn't behave like
-t
测试STDIN
是否附加到 tty。当您通过管道将数据传输到 perl 时,它不会附加到 tty。这不应该取决于您用于管道传输的机制(即,是否使用
|
管道传输命令或使用<
管道传输文件。)但是,您将有一个直接运行程序时会附加 tty。给出以下示例:您会期望以下输出:
-t
tests whether or notSTDIN
is attached to a tty.When you pipe data to perl, it will not be attached to a tty. This should not depend on the mechanism you use to pipe (ie, whether you pipe a command using
|
or pipe a file using<
.) However, you will have a tty attached when you run the program directly. Given the following example:You would expect the following output: