![](/img/trans.png)
[英]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.