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

But how to know whether user terminates system("$cmd|more") with q or not in the first place?

It probably will get more complicated than you want. You want to intercept the q keypress.

Here is a simple example, (or you can move up to an eventloop system, or watch STDIN in the separate thread, and let it watch for q). I 'm not sure how to pass the q to killing your command , unless you use a piped open or some other IPC module that returns a pid. Maybe someone knows a clever trick for that using return values from system.

#!/usr/bin/perl use warnings; use strict; use Term::ReadKey; #passing ReadKey() an argument of -1 to indicate not to block: ReadMode('cbreak'); while(1){ my $char; if (defined ($char = ReadKey(0)) ) { print "$char->", ord($char),"\n"; # input was waiting and i +t was $char if(ord($char) == 113){ print "Got a q\n"; print "Do your thing + here\n" } if(ord($char) == 27){ print "Got an Escape\n"; exit; } } else { # no input was waiting } } ReadMode('normal'); # restore normal tty settings

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh
  • Comment on Re: How to know whether user terminates system("$cmd|more") with q or not?
  • Download Code

Replies are listed 'Best First'.
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

    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?

      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