Show me those Perly whites
October 18, 2007 8:41 PM   Subscribe

How do I color output in Perl?

I don't mean in HTML pages, just in regular old shell output from Perl scripts. (I've installed the Win32::Console mod, am calling it at the beginning, and using the right syntax, but am getting an error about undefined values and such...)

Any advice for that mod, plus pointers on the various other ways to get color (and maybe other console functionality like CLS, etc. - but colors are the most important) I know exist would be great. Thanks!
posted by Slam I Am to Computers & Internet (9 answers total) 1 user marked this as a favorite
 
You need to know what terminal type you're using and emit the appropriate control codes for it. They're usually an escape (0x1B) followed by some numbers and then a closing character.

Google says...
posted by polyglot at 8:47 PM on October 18, 2007


Alright, thanks...
Honestly I don't know exactly what what terminal type you're using means. Currently I'm just running Perl through the WinXP shell (not a real shell, I know)... what does that mean?
posted by Slam I Am at 9:26 PM on October 18, 2007


For more general context on terminals, see here.

If your project isn't particularly mission critical, you may want to give Win32::Console::ANSI a shot.
posted by zamboni at 10:13 PM on October 18, 2007


FYI: Shells and terminals are two different things. One runs commands for you, and the other displays the output from those commands.
posted by philomathoholic at 11:37 PM on October 18, 2007


FYI: When he's asking about using Win32::Console, the distinction between shells and terminals is pretty academic. In any case:

use Win32::Console;
$CONSOLE= new Win32::Console(STD_OUTPUT_HANDLE);
$CONSOLE->Attr($FG_WHITE);
print "Hello...\n";
$CONSOLE->Attr($FG_RED|$BG_LIGHTGREEN);
print " Sailor\n";
$CONSOLE->Attr($ATTR_NORMAL);

WFM, good luck dude.
posted by IvyMike at 12:33 AM on October 19, 2007


Shell = program that interprets your commands and executes other programs for you. cmd.exe is a real shell, just not a particularly good one.

Terminal = program (or physical device!) which displays streams of text visually for you.

The shell and terminal could be on completely different computers but in the case of windows, they are the same program - cmd.exe does both, so the difference is academic as IvyMike says.

Also it turns out that the win32 console doesn't support ansi control codes since about Windows 2000, so you'll have to use the Win32::Console module or similar to control it. Ignore all that crap I posted up top about escapes because it will only work on older copies of windows with ansi.sys running.

zamboni's link will emulate the ansi behaviour using a new windows console, so you could send the control codes and it would translate them to whatever windows does now. Much simpler to just use Win32::Console directly instead.
posted by polyglot at 1:05 AM on October 19, 2007


The shell and terminal could be on completely different computers but in the case of windows, they are the same program - cmd.exe does both

No. Cmd.exe is just the shell. The Win32 console is separate, but used by cmd.exe. It is completely possible to have a Windows console-mode application without using cmd.exe. Your Perl interpreter is probably one such application.
posted by grouse at 1:23 AM on October 19, 2007


Thanks, IvyMike... it was the second line of your example I was missing.

Out of interest, are there other ways to get color in Perl, or would every answer involve a console module? If the latter, then the current answer works fine. Thank you, folks.
posted by Slam I Am at 4:59 PM on October 19, 2007


There's a great perl module you can install from CPAN that makes coloring output quite easy. Term::ScreenColor

If you want to see an example, I used it to color output in a message-of-the-day script I hacked together a few years back.
posted by namewithoutwords at 8:40 AM on October 20, 2007


« Older Where should I travel in order to burn a week's...   |   Cute-casual-comfortable wardrobe for pear-shaped... Newer »
This thread is closed to new comments.