minixman has asked for the wisdom of the Perl Monks concerning the following question:

All

Is there a way to send a control like symbol to a running
program, so if i run the following

<localhost>$ perl current_users.pl
#################################
Current users info
#################################

So the program has started, but i want to be able to pass
something like "display" by typing it in, and if it gets
that passed into the terminal it knows it has to print
something out.

But i would not want to use something like

my $INFO = <STDIN>; as that would make the program wait for
input, i want it to carry on processing, and then allow a user to pass input.
  • Comment on Reading terminal input while program is running

Replies are listed 'Best First'.
Re: Reading terminal input while program is running
by tirwhan (Abbot) on Feb 01, 2006 at 10:09 UTC

    Take a look at the non-blocked reading mode in Term::ReadKey. For example:

    #!/usr/bin/perl use warnings; use strict; use Term::ReadKey; while(1) { #do some stuff print "Did something\n"; sleep 5; if (my $line= ReadLine -1) { print "Got this command: $line"; } }

    There are ten types of people: those that understand binary and those that don't.
      When i run it i am not getting anything back from the request

      $ perl current_users.pl `pwd` ############################################## GFMIS Exlink User Activity Start: 01/02/2006 10:24 : Host = EU1 ############################################## 2006-01-25 15:38:24,953: Trader (DBL99911) = Connected DISPLAY 2006-01-25 15:38:24,953: Trader (DBL99911) = Connected Hey i got the following signal INT Somebody sent me a SIGINT at current_users.pl line 110.

        Sorry, my magic crystal ball is still out for repair (damn Grima!). You'll need to post code for anyone to figure out what's wrong in your script. Please make an effort to cut it down to the part that's necessary (i.e. the bit that's not doing what you think it ought to) before doing so though.


        There are ten types of people: those that understand binary and those that don't.
      Or depending on his actual needs, even something like Curses.
Re: Reading terminal input while program is running
by zentara (Cardinal) on Feb 01, 2006 at 12:54 UTC
    #!/usr/bin/perl use warnings; use strict; use IO::Select; # by edan of perlmonks # I just remembered what select says about mixing # buffered reading and "select", so even though the # code works, you might want to substitute # the read via <$fh> with: # my $input; # sysread( $fh, $input, 1024); # loop every 1 second my $timeout = 1; my $s = IO::Select->new(); $s->add( \*STDIN ); while (1) { if ( my @ready = $s->can_read($timeout) ) { # we got input for my $fh (@ready) { my $input = <$fh>; print "got: $input"; } } else { # no input } # just to show that we're looping print scalar localtime,"\n"; }

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