I have the following code to write to the Windows command console:
use Win32::Console;
my $console = new Win32::Console(Win32::Console::STD_ERROR_HANDLE());
my $defaultAttribute = $console->Attr();
my $defaultFG = ($defaultAttribute & 0x0F);
my $defaultBG = ($defaultAttribute & 0xF0);
$console->Attr($defaultBG | $Win32::Console::FG_LIGHTGREEN);
$console->Write("blah blah");
$console->Attr($defaultAttribute);
This code fails if the user redirects STDERR when invoking my script:
perl myscript.pl 2> foo
How can I obtain a handle to the Win32 console the process is attached to without reference to one of the standard handles so that it doesn't matter what redirections the user makes?
The effect I want is to be able to write a message on the console immediately following normal program output regardless of any redirection in a similar way to the bash builtin time
command. Essentially, similar to opening and writing to /dev/tty
in Unix.
I've tried my $console = new Win32::Console()
to allocate a new console followed by $console->Display()
but this does completely the wrong thing.