drwxrwxrwx has asked for the wisdom of the Perl Monks concerning the following question:

HI, i would like to use colors with print() function... can i do it?

Replies are listed 'Best First'.
Re: using colors with print()
by tachyon (Chancellor) on Jun 19, 2004 at 10:08 UTC

    In what context - terminal, GUI, CGI, printer? On what OS? How you do it depends on these factors. Color::Output or Curses::UI::Color may be what you are looking for but who knows? We need more information to point you in the right direction.

    cheers

    tachyon

      ok i apologize i'm using print() on a terminal (linux)
        Term::ANSIColor will do what you want in the easiest possible way. The docs and source explain how printing ANSI escape sequences causes your terminal to do (color) stuff
        $cat color.pl
        #!/usr/bin/perl
        use Term::ANSIColor qw(:constants);
        print BOLD, BLUE, "This text is in bold blue.\n", RESET;
        print RED, "This text is red.\n", RESET;
        
        $ ./color.pl
        This text is in bold blue.
        This text is red.
        $
        

        To set the AUTORESET feature so you can loose the RESET tokens set $Term::ANSIColor::AUTORESET = 1 just after the use. RTFS to see the available constants. Don't use BLINK :o)

        cheers

        tachyon

        A reply falls below the community's threshold of quality. You may see it by logging in.
Re: using colors with print()
by gri6507 (Deacon) on Jun 19, 2004 at 19:39 UTC
    here's a C program which does exactly that. I'm sure you could adapt it to be perlish.

    #define RESET 0 #define BRIGHT 1 #define DIM 2 #define UNDERLINE 3 #define BLINK 4 #define REVERSE 7 #define HIDDEN 8 #define BLACK 0 #define RED 1 #define GREEN 2 #define YELLOW 3 #define BLUE 4 #define MAGENTA 5 #define CYAN 6 #define WHITE 7 void textcolor(int attr, int fg, int bg); int main() { textcolor(RESET, RED, BLACK); printf("In color"); textcolor(RESET, WHITE, BLACK); return 0; } void textcolor(int attr, int fg, int bg) { char command[13]; /* Command is the control command to the terminal */ sprintf(command, "%c[%d;%d;%dm", 0x1B, attr, fg + 30, bg + 40); printf("%s", command); }
      very nice, here's the same program in perl
      use constant RESET => 0; use constant BRIGHT => 1; use constant DIM => 2; use constant UNDERLINE => 3; use constant BLINK => 4; use constant REVERSE => 7; use constant HIDDEN => 8; use constant BLACK => 0; use constant RED => 1; use constant GREEN => 2; use constant YELLOW => 3; use constant BLUE => 4; use constant MAGENTA => 5; use constant CYAN => 6; use constant WHITE => 7; sub textcolor($$$); print textcolor( RESET, RED, BLACK ), "In color\n", textcolor( RESET, WHITE, BLACK ); sub textcolor($$$) { my ( $attr, $fg, $bg ) = @_; #Command is the control command to the terminal my $command = sprintf( "%c[%d;%d;%dm", 0x1B, $attr, $fg + 30, $bg ++ 40 ); return sprintf( "%s", $command ); }

        I believe there is a need to print the newline character after the reset, not before, or in other words, do not let color attributes span unnecessary lines, and those last lines dealing with $command are redundant.

        sub textcolor ($$$); print textcolor (RESET, RED, BLACK), 'In color', textcolor (RESET, WHITE, BLACK), "\n"; sub textcolor ($$$) { my ($attr, $fg, $bg) = @_; sprintf "\e[%d;%d;%dm", $attr, $fg + 30, $bg + 40; }