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.


I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh

In reply to Re^3: How to know whether user terminates system("$cmd|more") with q or not? by zentara
in thread How to know whether user terminates system("$cmd|more") with q or not? by PerlOnTheWay

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.