[英]How to redirect the Perl script output from command issued in windows command prompt to text file
[英]Coloring Perl output on windows command prompt
这个问题与以下问题有关: 如何从 Windows 上的 Perl 脚本中为 output 文本着色?
但是更具体一点。 在某种程度上,我已经获得了跨平台着色工作:
use Term::ANSIColor;
use Win32::Console;
if (!(-f STDOUT)) {
if ($^O =~ /win/) {
our $FG_BLUE;
our $FG_YELLOW;
our $FG_RED;
our $BG_GREEN;
my $CONSOLE = Win32::Console->new(STD_OUTPUT_HANDLE);
my $attr = $CONSOLE->Attr(); # Get current console colors
$blue = sub {$CONSOLE->Attr($FG_BLUE);return};
$reset = sub {$CONSOLE->Attr($attr);return};
$yellow = sub {$CONSOLE->Attr($FG_YELLOW);return};
$red = sub {$CONSOLE->Attr($FG_RED);return};
} else {
$blue = sub {return color('bold blue')};
$reset = sub {return color('reset')};
$yellow = sub {return color('yellow')};
$red = sub {return color('red')};
}
}
但是从内部字符串调用函数时,终端 colors 不会立即更改,因此:
print "${\$blue->()} this is blue\n";
print "${\$blue->()}This is... not blue${\$reset->()}\n";
print "this is Blue ${\$blue->()}\n";
print "this is reset${\$reset->()}\n";
我想知道是否可以执行以下操作:
my $print_help = <<PRINT_HELP;
Usage: $toolname [-Options] [-fields name1,[name2],...]
${\$red->()} toolname version VERSION ${\$reset->()}
${\$blue->()} options: ${\$reset->()}
PRINT_HELP
print $print_help;
打印没有 colors。 我试过设置 $| = 1 没有运气。
我没有在相关系统上安装 Win32::Console::ANSI 的选项,因此我无法使任何使用该模块的解决方案工作。
这种 hack 可能符合您的需要。
#!/usr/bin/perl
use warnings;
use strict;
my $alice = sub { return 'ALICE'; };
my $bob = sub { return 'BOB'; };
my $test = <<'ENDTEST';
lineone
line2 ${\$alice->()} endline
line3 startline ${\$bob->()}
linefour
linefive
ENDTEST
# Add spaces around newline, split on horizontal whitespace
$test =~ s/\n/ \n /g;
my @testtokens = split /\h/, $test;
# Print '%s ' for each of the testtokens
# Print newlines, evaluate all testtokens beginning with '$', otherwise print
map { /\n/ ? print : printf '%s ', /^\$/ ? eval $_ : $_} @testtokens;
获取 ENDTEST heredoc 并在最后一行打印:
$ heretest.pl
lineone
line2 ALICE endline
line3 startline BOB
linefour
linefive
也许这会按顺序评估事情。
在开始打印之前,您正在调用red
、 reset
、 blue
和reset
。 您可以使用模板。 这是一个强大的实现:
use FindBin qw( $RealBin );
use lib "$RealBin/lib";
use My::Console qw( );
my $console = My::Console->new;
my $print_help = <<'__END_OF_HELP__';
Usage: $toolname [-Options] [-fields name1,[name2],...]
{{red}}toolname version VERSION{{reset}}
{{blue}}options:{{reset}}
__END_OF_HELP__
$console->print_with_color($print_help);
lib/My/Console.pm
:
package My::Console;
use strict;
use warnings;
my $console;
BEGIN {
if (!-t STDOUT) {
require My::Console::Dumb;
$console = My::Console::Dumb::;
}
elsif ($^O eq 'Win32') {
require My::Console::Win32;
$console = My::Console::Win32::;
}
else {
require My::Console::ANSI;
$console = My::Console::ANSI::;
}
}
sub new { $console }
1;
lib/My/Console/Base.pm
:
package My::Console::Base;
use strict;
use warnings;
use Carp qw( croak );
my %allowed_cmds = map { $_ => 1 } qw( red blue reset );
sub red { }
sub blue { }
sub reset { }
sub print { print(STDOUT @_); }
sub print_with_color {
my $self = shift;
for (@_) {
/\G ( (?: [^{] | \{(?!\{) )+ ) /xgc
and $self->print($1);
/\G \z /xgc
and next;
/\G \{\{ /xgc;
/\G ( (?: [^}] | \}(?!\}) )* ) \}\} /xgc
or croak("Bad template");
my $cmd = $1;
if ($cmd eq "") {
# An escape mechanism. Use "{{}}" to output "{{".
$self->print("{{");
redo;
}
$allowed_cmds{$cmd}
or croak("Unrecognized command \"$cmd\"");
$self->$cmd();
redo;
}
}
1;
lib/My/Console/Win32.pm
:
package My::Console::Win32;
use strict;
use warnings;
use My::Console::Base qw( );
use Win32::Console;
our @ISA = My::Console::Base::;
my $CONSOLE = Win32::Console->new(STD_OUTPUT_HANDLE);
my $initial_console_attr = $CONSOLE->Attr();
sub red { STDOUT->flush; $CONSOLE->Attr($FG_RED); }
sub blue { STDOUT->flush; $CONSOLE->Attr($FG_BLUE); }
sub reset { STDOUT->flush; $CONSOLE->Attr($initial_console_attr); }
1;
lib/My/Console/ANSI.pm
:
package My::Console::ANSI;
use strict;
use warnings;
use My::Console::Base qw( );
use Term::ANSIColor qw( );
our @ISA = My::Console::Base::;
sub red { print(Term::ANSIColor::red()); }
sub blue { print(Term::ANSIColor::blue()); }
sub reset { print(Term::ANSIColor::reset()); }
1;
lib/My/Console/Dumb.pm
:
package My::Console::Dumb;
use strict;
use warnings;
use My::Console::Base qw( );
our @ISA = My::Console::Base::;
1;
我的控制台不支持 colors 但我可以看到支持 colors 的控制台上的 ESCAPE 代码应该可以工作。 我想知道它对你有用吗?
#!/usr/bin/perl
use Term::ANSIColor;
use Win32::Console;
if (!(-f STDOUT)) {
if ($^O =~ /win/) {
our $FG_BLUE;
our $FG_YELLOW;
our $FG_RED;
our $BG_GREEN;
my $CONSOLE = Win32::Console->new(STD_OUTPUT_HANDLE);
my $attr = $CONSOLE->Attr(); # Get current console colors
$blue = sub {$CONSOLE->Attr($FG_BLUE);return};
$reset = sub {$CONSOLE->Attr($attr);return};
$yellow = sub {$CONSOLE->Attr($FG_YELLOW);return};
$red = sub {$CONSOLE->Attr($FG_RED);return};
} else {
$blue = sub {return color('bold blue')};
$reset = sub {return color('reset')};
$yellow = sub {return color('yellow')};
$red = sub {return color('red')};
}
}
help();
sub help {
print
"
Usage: $toolname [-Options] [-fields name1,[name2],...]
${\$red->()} toolname version VERSION ${\$reset->()}
${\$blue->()} options: ${\$reset->()}
";
}
问题:为什么不为此目的使用POD ?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.