如何在Windows上为Perl脚本输出文本?

时间:2022-11-23 14:05:34

I would like to color format the text printed to the console using the Perl print command.

我想使用Perl打印命令对打印到控制台的文本进行颜色格式化。

In my case the script will only be run under WinXP-DOS Command Line but it would be great if it was OS independent although I would rather tie it to WinXP than have to download a seperate package.

在我的情况下,脚本将只在WinXP-DOS命令行下运行,但如果它是独立于操作系统将是很好的,虽然我宁愿将它绑定到WinXP而不是下载一个单独的包。

4 个解决方案

#1


For any terminal that supports ANSI escape codes you can use the Term::ANSIColor package available on CPAN.

对于任何支持ANSI转义码的终端,您可以使用CPAN上提供的Term :: ANSIColor包。

From the Wikipedia page:

从*页面:

Console windows in Windows versions based on NT (Windows NT 4.0, Windows 2000, Windows XP, Windows Server 2003, Windows Vista and Windows Server 2008) do not natively support ANSI Escape sequences, though some support is possible.

基于NT(Windows NT 4.0,Windows 2000,Windows XP,Windows Server 2003,Windows Vista和Windows Server 2008)的Windows版本中的控制台窗口本身不支持ANSI Escape序列,但可以提供一些支持。

Don't know any more Windows-specific information than that, I'm a POSIX guy. :-)

不知道比Windows更具体的信息,我是一个POSIX人。 :-)

#2


Win32::Console - here's an example

Win32 :: Console - 这是一个例子

use Win32::Console;
my $CONSOLE = Win32::Console->new(STD_OUTPUT_HANDLE);
my $attr = $CONSOLE->Attr(); # Get current console colors
$CONSOLE->Attr($FG_YELLOW | $BG_GREEN); # Yellow text on green

print "This is a test\n";

$CONSOLE->Attr($attr); # Set console colors back to original

#3


Here is what worked best for me after all:

毕竟,这对我来说最有效:

1) Installed Win32::Console::ANSI (note that this is not the same as Win32::Console)

1)安装Win32 :: Console :: ANSI(注意这与Win32 :: Console不一样)

perl -MCPAN -e shell
cpan> install Win32::Console::ANSI

2) If this module is loaded before Term::ANSIColor, you can use the standard Term::ANSIColor API and it actually works (I tried it with Windows 7).

2)如果在Term :: ANSIColor之前加载此模块,您可以使用标准的Term :: ANSIColor API,它实际上可以工作(我在Windows 7中尝试过)。

use Win32::Console::ANSI;
use Term::ANSIColor;

print color("blue"), "blue\n", color("reset");
print "normal\n";

#4


system("color A"); #DOS command, change text color to lime

system("color 7"); #DOS command, change text color to white

However those commands change text color on the whole screen. Type "color ?" in DOS window to see color options

但是,这些命令会在整个屏幕上更改文本颜色。输入“颜色?”在DOS窗口中查看颜色选项

I am using strawberry perl on Windows and I did not have Win32::Console package. To install this package type in console:

我在Windows上使用草莓perl,我没有Win32 :: Console包。要在控制台中安装此包类型:

perl -MCPAN -e shell

perl -MCPAN -e shell

install Win32::Console

exit

#1


For any terminal that supports ANSI escape codes you can use the Term::ANSIColor package available on CPAN.

对于任何支持ANSI转义码的终端,您可以使用CPAN上提供的Term :: ANSIColor包。

From the Wikipedia page:

从*页面:

Console windows in Windows versions based on NT (Windows NT 4.0, Windows 2000, Windows XP, Windows Server 2003, Windows Vista and Windows Server 2008) do not natively support ANSI Escape sequences, though some support is possible.

基于NT(Windows NT 4.0,Windows 2000,Windows XP,Windows Server 2003,Windows Vista和Windows Server 2008)的Windows版本中的控制台窗口本身不支持ANSI Escape序列,但可以提供一些支持。

Don't know any more Windows-specific information than that, I'm a POSIX guy. :-)

不知道比Windows更具体的信息,我是一个POSIX人。 :-)

#2


Win32::Console - here's an example

Win32 :: Console - 这是一个例子

use Win32::Console;
my $CONSOLE = Win32::Console->new(STD_OUTPUT_HANDLE);
my $attr = $CONSOLE->Attr(); # Get current console colors
$CONSOLE->Attr($FG_YELLOW | $BG_GREEN); # Yellow text on green

print "This is a test\n";

$CONSOLE->Attr($attr); # Set console colors back to original

#3


Here is what worked best for me after all:

毕竟,这对我来说最有效:

1) Installed Win32::Console::ANSI (note that this is not the same as Win32::Console)

1)安装Win32 :: Console :: ANSI(注意这与Win32 :: Console不一样)

perl -MCPAN -e shell
cpan> install Win32::Console::ANSI

2) If this module is loaded before Term::ANSIColor, you can use the standard Term::ANSIColor API and it actually works (I tried it with Windows 7).

2)如果在Term :: ANSIColor之前加载此模块,您可以使用标准的Term :: ANSIColor API,它实际上可以工作(我在Windows 7中尝试过)。

use Win32::Console::ANSI;
use Term::ANSIColor;

print color("blue"), "blue\n", color("reset");
print "normal\n";

#4


system("color A"); #DOS command, change text color to lime

system("color 7"); #DOS command, change text color to white

However those commands change text color on the whole screen. Type "color ?" in DOS window to see color options

但是,这些命令会在整个屏幕上更改文本颜色。输入“颜色?”在DOS窗口中查看颜色选项

I am using strawberry perl on Windows and I did not have Win32::Console package. To install this package type in console:

我在Windows上使用草莓perl,我没有Win32 :: Console包。要在控制台中安装此包类型:

perl -MCPAN -e shell

perl -MCPAN -e shell

install Win32::Console

exit