< UNIX 中的运算符,传递给 Perl 脚本

发布于 2024-12-29 08:43:52 字数 187 浏览 0 评论 0 原文

计算 if(-t STDIN) 时, UNIX 运算符算作 STDIN 吗?如果没有,我如何获取该数据?

所以有人输入 perl example.pl <测试.txt。这与通过 ls | 管道输入的数据不同。 ./example.pl。我怎样才能得到这种行为?

When evaluating if(-t STDIN), does the < UNIX operator count as STDIN? If not, how do I get that data?

So someone types perl example.pl < testing.txt. This doesn't behave like data piped in via ls | ./example.pl. How can I get that behavior?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟花易冷人易散 2025-01-05 08:43:52

测试 -p STDIN,检查文件句柄 STDIN 是否附加到管道。

touch foo
perl -e 'print -p STDIN' < foo            # nothing
cat foo | perl -e 'print -p STDIN'        # 1

但我不确定我是否理解你的问题。在所有这三种情况下,

1.  perl -e 'print $_=<STDIN>' < <(echo foo)
2.  echo foo | perl -e 'print $_=<STDIN>'
3.  perl -e 'print $_=<STDIN>'          # then type "foo\n" to the console

输入都是相同的,并且都可以通过 STDIN 文件句柄访问。在前两种情况下,-t STDIN 的计算结果为 false,在第二种情况下,-p STDIN 将为 true。

这三种情况之间的行为差​​异很微妙,而且通常并不重要。显然,第三种情况将等待至少一行输入(以“\n”或 EOF 结尾)。前两种情况之间的差异更加微妙。当程序的输入从另一个进程的输出通过管道传输时,您在延迟或该程序是否缓冲其输出方面会受到第一个进程的支配。

时,也许你可以扩展一下你的意思

perl example.pl < testing.txt

当你说“行为不像”

ls | ./example.pl

Test -p STDIN, which checks if the filehandle STDIN is attached to a pipe.

touch foo
perl -e 'print -p STDIN' < foo            # nothing
cat foo | perl -e 'print -p STDIN'        # 1

But I'm not sure I understand your question. In all three of these cases

1.  perl -e 'print $_=<STDIN>' < <(echo foo)
2.  echo foo | perl -e 'print $_=<STDIN>'
3.  perl -e 'print $_=<STDIN>'          # then type "foo\n" to the console

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

perl example.pl < testing.txt

doesn't behave like

ls | ./example.pl
放飞的风筝 2025-01-05 08:43:52

-t 测试 STDIN 是否附加到 tty。

当您通过管道将数据传输到 perl 时,它不会附加到 tty。这不应该取决于您用于管道传输的机制(即,是否使用 | 管道传输命令或使用 < 管道传输文件。)但是,您将有一个直接运行程序时会附加 tty。给出以下示例:

#!/usr/bin/perl
print ((-t STDIN) ? "is a tty\n" : "is not a tty\n");

您会期望以下输出:

% perl ./ttytest.pl
is a tty
% perl ./ttytest.pl < somefile
is not a tty
% ls | perl ./ttytest.pl
is not a tty

-t tests whether or not STDIN 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:

#!/usr/bin/perl
print ((-t STDIN) ? "is a tty\n" : "is not a tty\n");

You would expect the following output:

% perl ./ttytest.pl
is a tty
% perl ./ttytest.pl < somefile
is not a tty
% ls | perl ./ttytest.pl
is not a tty
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文