如何知道perl程序是否有管道输入
我可以使用 <>
将管道输入循环到 Perl 程序。但是我如何确定是否有管道输入,如果没有管道输入我将使用环境变量来加载文件。我正在尝试使用:
my @lines = (<>);
if ($#lines == -1) {
use setenv;
open FILE, "$ENV{'ART_FILE_LIST'}" or die $!;
@lines = <FILE>;
}
显然它不起作用,因为程序将在第一行等待
I can use <>
to loop there the pipeline input to a perl program. However how can I decide whether there are pipelined input, if there is no pipelined input I will use environment variable to load a file. I am trying to use:
my @lines = (<>);
if ($#lines == -1) {
use setenv;
open FILE, "$ENV{'ART_FILE_LIST'}" or die $!;
@lines = <FILE>;
}
Obviously it doesn't work, because the program will waiting at the first line
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 -t 运算符来查看您是否是终端,即不是管道:
You can use the -t operator to see if you are a terminal, i.e., not a pipeline:
使用
Getopt::Long
的其中一件事UNIX 管道的特点是它们通过不关心它们之前或之后的内容来实现其有用性。他们只是有工作要做,而且他们也这么做了。它们简单地做一件事,但它们都有开关来完成简单的工作,并进行更多的定制。
Use
Getopt::Long
One of the things about UNIX pipelines is that they achieve their usefulness by not caring what's before them or after them. They just have a job to do and they do it. They do one thing, simply, but they all have switches to do their simple job with a little more customization.