gpt4 book ai didi

perl - 打开文件句柄或分配标准输出

转载 作者:行者123 更新时间:2023-12-04 13:43:24 25 4
gpt4 key购买 nike

我正在一个用户可以通过 -o file 的程序中工作。选项,然后输出应定向到该文件。否则,它应该转到标准输出。

要检索我正在使用模块 getopt long 的选项,这不是问题。问题是我想用那个文件创建一个文件句柄 如果未设置该选项,则为其分配标准输出。

if ($opt) {
open OUTPUT, ">", $file;
} else {
open OUTPUT, # ???
}

那是因为这样,稍后在我的代码中,我可以:
print OUTPUT "...";

不用担心 OUTPUT是标准输出或用户指定的文件。这可能吗?如果我在这里做不好的设计,请告诉我。

最佳答案

这将是如何使用 select 的一个很好的例子。 .

use strict;
use warnings;
use autodie;

my $fh;
if ($opt) {
open $fh, '>', $file;
select $fh;
}

print "This goes to the file if $opt is defined, otherwise to STDOUT."

关于perl - 打开文件句柄或分配标准输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7059120/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com