Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: How to know whether user terminates system("$cmd|more") with q or not?

by PerlOnTheWay (Monk)
on Sep 13, 2011 at 12:06 UTC ( [id://925688]=note: print w/replies, xml ) Need Help??


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

You mentioned

watch STDIN in the separate thread, and let it watch for q

Will it work theoretically?

Say, will the thread be able to capture q if it's already consumed by more?

  • Comment on Re^2: How to know whether user terminates system("$cmd|more") with q or not?
  • Download Code

Replies are listed 'Best First'.
Re^3: How to know whether user terminates system("$cmd|more") with q or not?
by zentara (Archbishop) on Sep 13, 2011 at 15:17 UTC
    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

      What I want to run is not $cmd,but $cmd|more

        I think you should describe exactly what you are trying to do, what $cmd you are running, and the whole big picture. Can you use a gui like Tk or does it have to be commandline?

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://925688]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-24 01:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found