Will it work theoretically?Say, will the thread be able to capture q if it's already consumed by more?
Theoretically yes,I think your only way to do it that way is to somehow dup stdin. See
Found in /usr/lib/perl5/5.14.1/pod/perlfaq5.pod
How do I dup() a filehandle in Perl?
If you check "open" in perlfunc, you'll see that several of the way
+s to
call open() should do the trick. For example:
open my $log, '>>', '/foo/logfile';
open STDERR, '>&LOG';
Or even with a literal numeric descriptor:
my $fd = $ENV{MHCONTEXTFD};
open $mhcontext, "<&=$fd"; # like fdopen(3S)
Note that "<&STDIN" makes a copy, but "<&=STDIN" makes an alias
+. That
means if you close an aliased handle, all aliases become inacce
+ssible.
This is not true with a copied one.
Error checking, as always, has been left as an exercise for the
+ reader.
Then your stdin watcher thread and main thread might be able to both get the q .
As with error checking, seeing if it works is left as an exercise to the OP. :-). Googling for "perl duping stdin" should give you some already written code.But I think you are trying to do this the hard way, you probably would be better off putting your code into an eventloop of some sort, and using a piped open to run your $cmd.
|