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

Hi again monks

I would like ask for help on how to output colored text in windows command line? And is it possible to do it without using a module (Win32::Console::ANSI)? Because I'm having problems using that every time I execute the command it displays "Can't locate loadable object for module Win32::Console::ANSI in @INC (@INC contains:"

So I would like to know if it's possible to do it even if I don't use that module... Like I'll just add some elements on the print or printf line.. (I'm thinking that way.. Hope that there's a solution like that hehehe!) If can. Can you give some simple examples..

thanks :)

Replies are listed 'Best First'.
Re: Colored text output in Windows cmd
by zwon (Abbot) on May 12, 2012 at 05:44 UTC

      I already did.. but still the error appears..

      I'm currently using the Strawberry Perl although I have ActivePerl also installed..

      is it some kind of a conflict?
Re: Colored text output in Windows cmd
by roboticus (Chancellor) on May 12, 2012 at 15:35 UTC

    astronogun:

    It's certainly possible, though I would advise using a module to do it (I use Term::ANSIColor). Use google to find the escape codes supported by the windows console, and then write the appropriate functions. I found a reference to ANSI.SYS, which looks like a reasonable reference. Under the "Set Attribute" escape code, after a description of the escape sequence, they list a few examples:

    {ESC}[1m High intensity. {ESC}[1;5m High intensity and blink. {ESC}[30;46m Black foreground, cyan background. {ESC}[m Turn off all attributes. {ESC}[0m Turn off all attributes.

    So you should be able to see if they're the correct escape codes with a quickie like so:

    use strict; use warnings; print "\x1b[30;46m This should be black on cyan\n"; print "\x1b[0m And this should be back to normal\n";

    Update: Added link for Term::ANSIColor.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      I found a reference to ANSI.SYS,

      Ansi.sys is an ancient 16-bit driver that hasn't worked with anything since Windows ME.

      The OP has already found the correct solution(s) -- Win32::Console::Ansi -- all he needs to do is install it/them correctly.

      Pointing him at ancient 16-bit device drivers is not helpful. If you were a windows users, you'd presumably know that!


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      The start of some sanity?

        BrowserUk:

        I certainly didn't intend to imply that he should use ANSI.sys, I just thought it was a good reference to the escape codes that (I think) the command console uses. (The tiny example was to let the OP find out if those escape sequences are the correct ones--I suspect so, but am not sure.) I agree that actually trying to use the ANSI.sys device driver would be terrible.

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

Re: Colored text output in Windows cmd
by astronogun (Sexton) on May 13, 2012 at 03:18 UTC

    @roboticus

    Thanks for the help. I've tried the code that you've just gave and it didn't worked in Perl hehehe! However when I tried that on creating a batch file it worked :)

    @BrowserUk

    Right now I'm trying to figure out what is the problem.. I installed the Win32::Console::ANSI and the status is done.. But when I run the code still the error persist.. When I checked my library on Perl Strawberry, there is no Win32::Console::ANSI folder there (even the library of vender and site folders, anyway I just checked them if in case if it was installed there).. Anyway to add some information regarding this it seems the installation progress is installed at the ActivePErl not on the Strawberry Perl... To add up here's the exact message of the problem..

    "Can't locate loadable object for module Win32::Console::ANSI in @INC +(@INC contains: C:/strawberry/perl/site/lib C:/strawberry/perl/vendor +/lib C:/strawberry/perl/lib .) C:\Users\willy\Documents\Perl\colortes +t.pl line 1 Compilation failed in require at C:\Users\willy\Documents\Perl\colorte +st.pl line 1 BEGIN failed--compilation aborted at C:\Users\willy\Documents\Perl\col +ortest.pl line 1."

    I followed the steps on installing the module (referring to the previous post) but still the problem persist. I'm running Windows 7 Professional 64-bit, and im running perl either command line or the strawberry command line at run as administrator..

    Hope you could help me troubleshoot this with this information given.. Thanks to all for the help.

      When you built the module, did you notice this:

      Warning: prerequisite Win32::Pipe 0 not found.

      Or this:

      t/02_CursorFunc.t ...... Can't locate Win32/Pipe.pm in @INC (@INC cont +ains: C:\s trawberry\perl\lib\Win32-Console-ANSI-1.04\blib\lib C:\strawberry\perl +\lib\Win32 -Console-ANSI-1.04\blib\arch C:/strawberry/perl/site/lib C:/strawberry +/perl/vend or/lib C:/strawberry/perl/lib .) at t/02_CursorFunc.t line 3.

      I don't use Strawberry Perl, so I cannot help you with that.

      As far as I remember, Win32::Console::ANSI may have come with my AS distribution -- the Win32::Pipe dependancy certainly did -- but even if it didn't and I needed to install it, I'd only have to do:

      C:\test>ppm-shell ppm 4.10 (ActivePerl 1007) Copyright (C) 2010 ActiveState Software Inc. All rights reserved. ppm> search Win32::Console::ANSI 1: Win32-Console-ANSI Perl extension to emulate ANSI console on Win32 system. Version: 1.04 Released: 2009-03-22 Author: Jean-Louis Morel <jl_morel@bribes.org> Provide: Win32::Console::ANSI version 1.04 Require: Win32::Pipe Require: Win32::Process Repo: ActiveState Package Repository CPAN: http://search.cpan.org/dist/Win32-Console-ANSI-1.04/ Installed: 1.04 (site) ppm> install 1 Downloading Win32-Console-ANSI-1.04...done Unpacking Win32-Console-ANSI-1.04...done Generating HTML for Win32-Console-ANSI-1.04...done Updating files in site area...done 6 files unchanged

      which took about 12 seconds.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      The start of some sanity?

      I followed the steps on installing the module (referring to the previous post)

      It has been my experience, that whenever perlmonks posters say "I've followed the steps", without showing they've done so (copy/paste of shell session), they missed something simple

      If you follow Can't locate loadable object for module you can learn from pervious posters why this error happens (copying .pm files) and why you shoudn't do that

        ok here's what I've done so far.... and it's already OK

        I've downloaded the Win32-Console-ANSI-1.04.tar.gz file.. then at the command line I typed this:

        Makefile.pl:

        C:\strawberry\perl\lib\Win32-Console-ANSI-1.04>Makefile.PL Checking if your kit is complete... Looks good Warning: prerequisite Win32::Pipe 0 not found. Writing Makefile for Win32::Console::ANSI Writing MYMETA.yml and MYMETA.json

        dmake:

        C:\strawberry\perl\lib\Win32-Console-ANSI-1.04>dmake cp lib/Win32/Console/ANSI.pm blib\lib\Win32\Console\ANSI.pm C:\strawberry\perl\bin\perl.exe C:\strawberry\perl\lib\ExtUtils\xsubpp + -typemap C:\strawberry\perl\lib\ExtUtils\typemap -typemap typemap ANSI.xs > A +NSI.xsc && C:\strawberry\perl\bin\perl.exe -MExtUtils::Command -e mv -- ANSI.xsc + ANSI.c gcc -c -I. -s -O2 -DWIN32 -DWIN64 -DCONSERVATIVE -DPERL_TEXTMODE_ +SCRIPTS -D USE_SITECUSTOMIZE -DPERL_IMPLICIT_CONTEXT -DPERL_IMPLICIT_SYS -fno-str +ict-aliasi ng -mms-bitfields -s -O2 -DVERSION=\"1.04\" -DXS_VERSION=\ +"1.04\" " -IC:\strawberry\perl\lib\CORE" ANSI.c Running Mkbootstrap for Win32::Console::ANSI () C:\strawberry\perl\bin\perl.exe -MExtUtils::Command -e chmod -- 644 AN +SI.bs C:\strawberry\perl\bin\perl.exe -MExtUtils::Mksymlists \ -e "Mksymlists('NAME'=>\"Win32::Console::ANSI\", 'DLBASE' => 'ANS +I', 'DL_FU NCS' => { }, 'FUNCLIST' => [], 'IMPORTS' => { }, 'DL_VARS' => []);" dlltool --def ANSI.def --output-exp dll.exp g++ -o blib\arch\auto\Win32\Console\ANSI\ANSI.dll -Wl,--base-file -Wl, +dll.base - mdll -s -L"C:\strawberry\perl\lib\CORE" -L"C:\strawberry\c\lib" ANSI.o + C:\stra wberry\perl\lib\CORE\libperl514.a -lmoldname -lkernel32 -luser32 -lgdi +32 -lwinsp ool -lcomdlg32 -ladvapi32 -lshell32 -lole32 -loleaut32 -lnetapi32 -luu +id -lws2_3 2 -lmpr -lwinmm -lversion -lodbc32 -lodbccp32 -lcomctl32 dll.exp dlltool --def ANSI.def --base-file dll.base --output-exp dll.exp g++ -o blib\arch\auto\Win32\Console\ANSI\ANSI.dll -mdll -s -L"C:\straw +berry\perl \lib\CORE" -L"C:\strawberry\c\lib" ANSI.o C:\strawberry\perl\lib\COR +E\libperl5 14.a -lmoldname -lkernel32 -luser32 -lgdi32 -lwinspool -lcomdlg32 -lad +vapi32 -ls hell32 -lole32 -loleaut32 -lnetapi32 -luuid -lws2_32 -lmpr -lwinmm -lv +ersion -lo dbc32 -lodbccp32 -lcomctl32 dll.exp C:\strawberry\perl\bin\perl.exe -MExtUtils::Command -e chmod -- 755 bl +ib\arch\au to\Win32\Console\ANSI\ANSI.dll C:\strawberry\perl\bin\perl.exe -MExtUtils::Command -e cp -- ANSI.bs b +lib\arch\a uto\Win32\Console\ANSI\ANSI.bs C:\strawberry\perl\bin\perl.exe -MExtUtils::Command -e chmod -- 644 bl +ib\arch\au to\Win32\Console\ANSI\ANSI.bs

        dmake test:

        C:\strawberry\perl\lib\Win32-Console-ANSI-1.04>dmake test C:\strawberry\perl\bin\perl.exe "-MExtUtils::Command::MM" "-e" "test_h +arness(0, 'blib\lib', 'blib\arch')" t/*.t t/00_Pod.t ............. skipped: Test::Pod 1.00 required for testing +POD t/01_PodCoverage.t ..... skipped: Test::Pod::Coverage 1.00 required fo +r testing POD coverage t/02_CursorFunc.t ...... Can't locate Win32/Pipe.pm in @INC (@INC cont +ains: C:\s trawberry\perl\lib\Win32-Console-ANSI-1.04\blib\lib C:\strawberry\perl +\lib\Win32 -Console-ANSI-1.04\blib\arch C:/strawberry/perl/site/lib C:/strawberry +/perl/vend or/lib C:/strawberry/perl/lib .) at t/02_CursorFunc.t line 3. BEGIN failed--compilation aborted at t/02_CursorFunc.t line 3. t/02_CursorFunc.t ...... Dubious, test returned 2 (wstat 512, 0x200) No subtests run t/03_CursorMove.t ...... Can't locate Win32/Pipe.pm in @INC (@INC cont +ains: C:\s trawberry\perl\lib\Win32-Console-ANSI-1.04\blib\lib C:\strawberry\perl +\lib\Win32 -Console-ANSI-1.04\blib\arch C:/strawberry/perl/site/lib C:/strawberry +/perl/vend or/lib C:/strawberry/perl/lib .) at t/03_CursorMove.t line 3. BEGIN failed--compilation aborted at t/03_CursorMove.t line 3. t/03_CursorMove.t ...... Dubious, test returned 2 (wstat 512, 0x200) No subtests run t/04_DisplayEdition.t .. Can't locate Win32/Pipe.pm in @INC (@INC cont +ains: C:\s trawberry\perl\lib\Win32-Console-ANSI-1.04\blib\lib C:\strawberry\perl +\lib\Win32 -Console-ANSI-1.04\blib\arch C:/strawberry/perl/site/lib C:/strawberry +/perl/vend or/lib C:/strawberry/perl/lib .) at t/04_DisplayEdition.t line 3. BEGIN failed--compilation aborted at t/04_DisplayEdition.t line 3. t/04_DisplayEdition.t .. Dubious, test returned 2 (wstat 512, 0x200) No subtests run t/05_Colors.t .......... Can't locate Win32/Pipe.pm in @INC (@INC cont +ains: C:\s trawberry\perl\lib\Win32-Console-ANSI-1.04\blib\lib C:\strawberry\perl +\lib\Win32 -Console-ANSI-1.04\blib\arch C:/strawberry/perl/site/lib C:/strawberry +/perl/vend or/lib C:/strawberry/perl/lib .) at t/05_Colors.t line 3. BEGIN failed--compilation aborted at t/05_Colors.t line 3. t/05_Colors.t .......... Dubious, test returned 2 (wstat 512, 0x200) No subtests run t/06_Func.t ............ Can't locate Win32/Pipe.pm in @INC (@INC cont +ains: C:\s trawberry\perl\lib\Win32-Console-ANSI-1.04\blib\lib C:\strawberry\perl +\lib\Win32 -Console-ANSI-1.04\blib\arch C:/strawberry/perl/site/lib C:/strawberry +/perl/vend or/lib C:/strawberry/perl/lib .) at t/06_Func.t line 3. BEGIN failed--compilation aborted at t/06_Func.t line 3. t/06_Func.t ............ Dubious, test returned 2 (wstat 512, 0x200) No subtests run t/07_TortureMeHoney.t .. Can't locate Win32/Pipe.pm in @INC (@INC cont +ains: C:\s trawberry\perl\lib\Win32-Console-ANSI-1.04\blib\lib C:\strawberry\perl +\lib\Win32 -Console-ANSI-1.04\blib\arch C:/strawberry/perl/site/lib C:/strawberry +/perl/vend or/lib C:/strawberry/perl/lib .) at t/07_TortureMeHoney.t line 3. BEGIN failed--compilation aborted at t/07_TortureMeHoney.t line 3. t/07_TortureMeHoney.t .. Dubious, test returned 2 (wstat 512, 0x200) No subtests run t/08_Compatibility.t ... Can't locate Win32/Pipe.pm in @INC (@INC cont +ains: C:\s trawberry\perl\lib\Win32-Console-ANSI-1.04\blib\lib C:\strawberry\perl +\lib\Win32 -Console-ANSI-1.04\blib\arch C:/strawberry/perl/site/lib C:/strawberry +/perl/vend or/lib C:/strawberry/perl/lib .) at t/08_Compatibility.t line 3. BEGIN failed--compilation aborted at t/08_Compatibility.t line 3. t/08_Compatibility.t ... Dubious, test returned 2 (wstat 512, 0x200) No subtests run t/09_Compatibility2.t .. Can't locate Win32/Pipe.pm in @INC (@INC cont +ains: C:\s trawberry\perl\lib\Win32-Console-ANSI-1.04\blib\lib C:\strawberry\perl +\lib\Win32 -Console-ANSI-1.04\blib\arch C:/strawberry/perl/site/lib C:/strawberry +/perl/vend or/lib C:/strawberry/perl/lib .) at t/09_Compatibility2.t line 3. BEGIN failed--compilation aborted at t/09_Compatibility2.t line 3. t/09_Compatibility2.t .. Dubious, test returned 2 (wstat 512, 0x200) No subtests run t/99_Kwalitee.t ........ skipped: Test::Kwalitee not installed; skippi +ng Test Summary Report ------------------- t/02_CursorFunc.t (Wstat: 512 Tests: 0 Failed: 0) Non-zero exit status: 2 Parse errors: No plan found in TAP output t/03_CursorMove.t (Wstat: 512 Tests: 0 Failed: 0) Non-zero exit status: 2 Parse errors: No plan found in TAP output t/04_DisplayEdition.t (Wstat: 512 Tests: 0 Failed: 0) Non-zero exit status: 2 Parse errors: No plan found in TAP output t/05_Colors.t (Wstat: 512 Tests: 0 Failed: 0) Non-zero exit status: 2 Parse errors: No plan found in TAP output t/06_Func.t (Wstat: 512 Tests: 0 Failed: 0) Non-zero exit status: 2 Parse errors: No plan found in TAP output t/07_TortureMeHoney.t (Wstat: 512 Tests: 0 Failed: 0) Non-zero exit status: 2 Parse errors: No plan found in TAP output t/08_Compatibility.t (Wstat: 512 Tests: 0 Failed: 0) Non-zero exit status: 2 Parse errors: No plan found in TAP output t/09_Compatibility2.t (Wstat: 512 Tests: 0 Failed: 0) Non-zero exit status: 2 Parse errors: No plan found in TAP output Files=11, Tests=0, 1 wallclock secs ( 0.06 usr + 0.08 sys = 0.14 CP +U) Result: FAIL Failed 8/11 test programs. 0/0 subtests failed. dmake: Error code 255, while making 'test_dynamic'

        dmake install:

        C:\strawberry\perl\lib\Win32-Console-ANSI-1.04>dmake install Files found in blib\arch: installing files in blib\lib into architectu +re depende nt library tree Installing C:\strawberry\perl\site\lib\auto\Win32\Console\ANSI\ANSI.bs Installing C:\strawberry\perl\site\lib\auto\Win32\Console\ANSI\ANSI.dl +l Installing C:\strawberry\perl\site\lib\Win32\Console\ANSI.pm Appending installation info to C:\strawberry\perl\lib/perllocal.pod

        To finish I just add Term/ANSI.pm at the C:\strawberry\perl\site\lib (Because it prompt in the cmd that the file can't be located

        note: on the dmake as I've read on other post it is indicated that for 64-bit running Windows (like me Windows 7) is the best one to use...