Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果在主脚本中重定向到标准输出,如何在perl模块中打印到标准输出_Perl_Stdout - Fatal编程技术网

如果在主脚本中重定向到标准输出,如何在perl模块中打印到标准输出

如果在主脚本中重定向到标准输出,如何在perl模块中打印到标准输出,perl,stdout,Perl,Stdout,我已经在Perl脚本中重定向了STDOUT。我在模块中打印的所有内容都被重定向到一个文件。有没有办法在Perl模块中恢复标准输出 这是我的例子 require my_module; open(STDOUT, ">$outlog") || die "Error stdout: $!"; open(STDERR, ">>$outlog") || die "Error stderr: $!"; my_module::my_func(); 所以我想在my_module::my_f

我已经在Perl脚本中重定向了
STDOUT
。我在模块中打印的所有内容都被重定向到一个文件。有没有办法在Perl模块中恢复标准输出

这是我的例子

require my_module;

open(STDOUT, ">$outlog") || die "Error stdout: $!";
open(STDERR, ">>$outlog") || die "Error stderr: $!";

my_module::my_func();

所以我想在
my_module::my_func()
函数中的
STDOUT
上打印一条消息并退出。

似乎我找到了解决方案。首先我在主脚本中保存了
STDOUT
,然后在模块中使用它

require my_module;
open(SAVEOUT, ">&STDOUT") || die "Unable to save STDOUT: $!";
open(STDOUT, ">$outlog") || die "Error stdout: $!";

open(STDERR, ">>$outlog") || die "Error stderr: $!";

my_module::my_func();
my_模块::my_func()
中,我在退出之前添加了以下行

open (STDOUT, ">&main::SAVEOUT") or die "Unable to restore STDOUT : $!";
print "a_module!!!\n";

我打印的消息已发送到STDOUT

实际上,除非将STDOUT保存到其他位置,否则无法还原它

您可以执行以下操作:

        # Save current STDOUT handle in OLDOUT
        open (OLDOUT, ">&STDOUT") or die "Can't open OLDOUT: $!";   

        # Set STDOUT to a your output file
        open (STDOUT, ">$youroutputfile") or die "Can't open STDOUT: $!";

        # Do whatever you want to do here.......
        # ...........

        # Close STDOUT output stream
        close (STDOUT);

        # Reset STDOUT stream to previous state
        open (STDOUT, ">&OLDOUT") or die "Can't open STDOUT: $!";

        # Close OLDOUT handle
        close (OLDOUT);

        # Here your preview STDOUT is restored....
:)