in reply to Color in windows command prompt

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;

Replies are listed 'Best First'.
Re^2: Color in windows command prompt
by g_speran (Scribe) on Jan 16, 2018 at 22:50 UTC
    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