我怎么能用Term :: Screen打印到STDERR?

时间:2012-02-11 08:16:31

标签: perl printing terminal screen stderr

我想在此处将粗体hi!打印到STDERR。这可能是Term::Screen吗?

#!/usr/bin/env perl 
use warnings;
use 5.12.0;
use utf8;
binmode STDOUT, ':utf8';
binmode STDERR, ':utf8';
use Term::Screen;

my $scr = new Term::Screen;
unless ( $scr ) { die " Something's wrong \n"; }
$scr->clrscr();
$scr->at(5,10)->bold()->puts("hi!")->normal();
$scr->at(11,0);

1 个答案:

答案 0 :(得分:4)

Term :: Screen,查看其源代码,硬编码为*STDOUT

E.g。您在来源中呼叫的sub at{}具有以下内容:

$this->term()->Tgoto( 'cm', $c, $r, *STDOUT );

因此,您需要明确地将所有STDOUT重定向到STDERR:

open(my $backup_stdout, ">&STDOUT");
close(STDOUT);
open(STDOUT, ">&STDERR"); # This affects ALL of spawned child processes!
# *STDOUT = *STDERR; # This does the same but ONLY affects your process