gpt4 book ai didi

perl - 暂时将 STDOUT 重定向到/dev/null - 一直不工作

转载 作者:行者123 更新时间:2023-12-04 23:11:02 26 4
gpt4 key购买 nike

我在用

Server version: Apache/1.3.34 (Debian)
mod_perl - 1.29

引用 STDIN, STDOUT, and STDERR Streams
#!/usr/bin/perl5
package main;

use strict 'vars';

{
# Our mighty holy legacy code love to print out message in the middle of operation. Shihh....
# Let's quietly redirect those message to /dev/null.
my $nullfh = Apache::gensym( );
open $nullfh, '>/dev/null' or warn "Can't open /dev/null: $!";
local *STDOUT = $nullfh;
print "BYE BYE WORLD"; # Shouldn't show in webpage.
close $nullfh;
}

print "X BEGIN HELLO WORLD"; # Should show in webpage.

我意识到它并不总是有效。例如,我刷新页面 10 次。 x 次它会打印出“X BEGIN HELLO WORLD”。 (10-x) 时间它只是不打印任何内容。

我找不到任何原因为什么它会这样。我可以知道你们中有人遇到过和我类似的问题吗?

最佳答案

我需要明确存储和恢复。它适用于我的情况。但我不确定为什么。

# Take copies of the file descriptors
open OLDOUT, '>&STDOUT';
my $returned_values = 0;
{
# Our mighty holy legacy code love to print out message in the middle of operation. Shihh....
# Let's quietly redirect those message to /dev/null.
local *STDOUT;
open STDOUT, '>/dev/null' or warn "Can't open /dev/null: $!";
print "BYE BYE WORLD"; # Shouldn't show in webpage.
close STDOUT;
}
# Restore stdout.
open STDOUT, '>&OLDOUT' or die "Can't restore stdout: $!";
# Avoid leaks by closing the independent copies.
close OLDOUT or die "Can't close OLDOUT: $!";

关于perl - 暂时将 STDOUT 重定向到/dev/null - 一直不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1054579/

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