winapi - Write to console when stdin/stderr/stdout redirected in Activestate Perl -
i have next code write windows command console:
use win32::console; $console = new win32::console(win32::console::std_error_handle()); $defaultattribute = $console->attr(); $defaultfg = ($defaultattribute & 0x0f); $defaultbg = ($defaultattribute & 0xf0); $console->attr($defaultbg | $win32::console::fg_lightgreen); $console->write("blah blah"); $console->attr($defaultattribute);
this code fails if user redirects stderr when invoking script:
perl myscript.pl 2> foo
how can obtain handle win32 console process attached without reference 1 of standard handles doesn't matter redirections user makes?
the effect want able write message on console next normal programme output regardless of redirection in similar way bash builtin time
command. essentially, similar opening , writing /dev/tty
in unix.
i've tried my $console = new win32::console()
allocate new console followed $console->display()
wrong thing.
after asking question, delved bit deeper , able solve using nasty hack:
use win32api::file qw(createfile); utilize win32::console; $handle = createfile('conout$', 'rwke') or die "conout\$: $^e\n"; # $console = new win32::console($handle) or die "new console: $^e\n"; $console = bless {handle => $handle}, 'win32::console';
i looked @ code new()
function within win32::console
, saw creates hash containing handle console. if parameter specifies stdin/stdout/stderr, retrieves associated handle otherwise creates new console screen buffer , uses handle that.
so manually created win32::console
object containing handle console returned createfile.
so perl myscript.pl > nul 2> nul < nul
write blah blah
on screen below command line.
i'll take improve reply if comes one.
perl winapi console windows-console activestate
No comments:
Post a Comment