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

I have the script below that works fine on unix platforms, however, I am struggling to get this to work on the windows platform. I am trying to keep the same format using sprintf. I used "Win32::Console::ANSI;" and "Term::ANSIColor qw(:constants)" in the sprintf format below, and failed. Any Suggestions?

#!/usr/bin/perl $FAILURE=`echo -en "\\033[1;31m"`; $SUCCESS=`echo -en "\\033[1;32m"`; $NORMAL=`echo -en "\\033[0;39m"`; $RESULT="JUST A SIMPLE LINE"; $line=sprintf("%s%s%s %s\n",$SUCCESS,$RESULT,$NORMAL); print "$line\n"; $line=sprintf("%s%s%s %s\n",$FAILURE,$RESULT,$NORMAL); print "$line\n";

Replies are listed 'Best First'.
Re: Color in windows command prompt
by Discipulus (Canon) on Jan 16, 2018 at 22:36 UTC
    Hello g_speran,

    your perl script needs a serious rewrite: first of all always use strict and use warnings

    For the colors problem is not a Perl's one: is the dumb cmd.exe that is unable to understand ANSI sequences.

    Fortunately for me and for you ansicon by Jason Hood is able to render them.

    IIRC you can have it installed via ansicon -i whic hook in the command processor adding a registry key (see readme on github):

    [HKEY_CURRENT_USER\Software\Microsoft\Command Processor] .. other entries here.. "AutoRun"="(if %ANSICON_VER%==^%ANSICON_VER^% \"c:\\path\\to\\ansi166\ +\x86\\ansicon\" -p)"

    the -p option enable the parent process (ie cmd.exe) to use colors

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Color in windows command prompt
by g_speran (Scribe) on Jan 16, 2018 at 22:41 UTC

    I think I have figured this out, however, I have an additional issue directly related. I want to load a Windows module only if the $^O returns MSWIN32. I know to use the construct "use if (condition,module)" however, I cant seem to figure the syntax out for "Term::ANSIColor qw(:constants)". Any assistance is appreciated
    use if $^O eq "WINMS32", Term::ANSIColor qw(:constants); produces: syntax error at C:\temp\gethost3.pl line 4, near "Term::ANSIColor qw(: +constants)" Execution of C:\temp\gethost3.pl aborted due to compilation errors. Windows #!/usr/bin/perl use Win32::Console::ANSI; use Term::ANSIColor qw(:constants); $SUCCESS=GREEN; $NORMAL=RESET; $RESULT="THIS IS THE RESULT LINE"; $line=sprintf("%s%s%s\n",$SUCCESS.$RESULT.$NORMAL); print $line; Unix !/usr/bin/perl $FAILURE=`echo -en "\\033[1;31m"`; $SUCCESS=`echo -en "\\033[1;32m"`; $NORMAL=`echo -en "\\033[0;39m"`; $RESULT="Test Line Test Line Test Line Test Line Test Line Test Line T +est Line Test Line "; $line=sprintf("%s%s%s\n",$SUCCESS.$RESULT.$NORMAL); print $line; $line=sprintf("%s%s%s\n",$FAILURE.$RESULT.$NORMAL); print $line;
      Figured it out
      use if $^O == "WINMS32", Term::ANSIColor => qw(:constants);
        use if $^O == "WINMS32", Term::ANSIColor => qw(:constants);

        That's not quite right.
        The condition you want is  $^O eq 'MSWin32'
        The condition you've used will generally evaluate as true on all systems because, in numeric context, both $^O and "WINMS32" are generally 0.
        If you use warnings you should receive 2 warnings - one about the non-numeric nature of "WINMS32" and one about the non-numeric nature of $^O ("MSWin32").

        Also, on perl-5.26.0 at least, I find that if I use strict then I also need to place quotes around Term::ANSIColor.
        This is contrary to the if documentation which states:

        <quote>
        The use of => above provides necessary quoting of MODULE . If you don't use the fat comma (eg you don't have any ARGUMENTS), then you'll need to quote the MODULE.
        </quote>

        I'll submit a bug report about this oversight in the "if" documentation.

        UPDATE: Bug report submitted.

        I personally prefer to use "require" to load modules in this type of situation:
        if($^O eq 'MSWin32') { require Term::ANSIColor; Term::ANSIColor->import(":constants"); }
        Cheers,
        Rob