Re: STDIN on the fileevent handle



On 17 May 2006 11:27:59 -0700, "CORBA" <mkess1021@xxxxxxxxx> wrote:

Hello all.

I have been on here a few times and the help has been tremendous. So
when I ran into a recent roadblock, I thought I'd run it by the group
and see if anyone knows the solution.

I am using STDIN to feed a fileevent handle and populate a status
board. So, for example, if I type in one of four acceptable commands
to STDIN, then my status board will take a certain action based on the
input command.
.....snip.......
But for a reason I don't understand, this does not work with piped
input. All the commands are correct and I can verify that the status
board does get the input. However, there are two problems. #1) The
widgets never get displayed to the status board - the top level widget
is created but nothing is done with "readinput", my fileevent call.
#2) After recieving a legit command, the status board seems to think it
gets endless messages and continues to read blank lines. It is almost
as if it thinks there is input available and just continually reads.

Yeah, STDIN is a bit tricky to deal with. First STDIN can either be a
pipe or the keyboard, but it takes some doing to make it accept both
simultaneously. For instance if you run the program with a pipe, you
won't get the keyboard back until you close STDIN and reopen it

open (STDIN, "</dev/tty") or die $!;

The other thing is that STDIN never closes, unless you do it, so you
get the "endless messages" problem.

There are solutions. See
http://perlmonks.org?node_id=439053

This runs "./sender | ./write_me"

You might want to look at using sockets instead of STDIN.
Or you might want to look at IPC::Open3 instead of trying to run piped
opens. What is your overall objective?

############# sender #################################

#!/usr/bin/perl
$|++;

while(1){
print "xterm\n";
sleep 5;
}

############ write_me ################################

#!/usr/bin/perl
use warnings;
use Tk;

my $mw = MainWindow->new;
my $top = $mw->Toplevel();
$mw->withdraw;
$top->geometry( '100x100+100+100' );
$top->title("Test Connection Status Board");
$top->focusmodel ("active");
$top->waitVisibility;
# The waiting game

$mw->fileevent(\*STDIN, readable => [\&readinput, $top]);

MainLoop;

sub readinput {
# Process input
my($widget) = @ARG;

# Chop out the action and values
my $ARG = <STDIN>;
chomp $ARG;
if ((length $ARG) == 0) {
print "no length $tmp\n";
$tmp++;
return;
}
my @parts = split /:/, $ARG;
my $action = $parts[0];
my $value = $parts[1];
if ($value) {
chomp $value;
}

print "action ->$action\n";
if(fork() == 0){exec( $action )}
}

__END__

--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
.



Relevant Pages

  • Re: Check Pop3 or IMAP4
    ... I read somewhere that telnet does not support stdin and stdout. ... to send commands to the port 110. ... This works only if the server doesn't require the encoding of the password, ...
    (microsoft.public.scripting.vbscript)
  • Re: queuing up user input via writes to STDIN
    ... ReadMode 0; # Reset tty mode before exiting ... I have a program that reads STDIN for user commands while the program is ...
    (perl.beginners)
  • Re: While loop and commands that read from std input
    ... the arguments of the loop and the usage of '/dev/null' to prevent such ... difference between such commands and others that don't read std input. ... they obviously read stdin. ... While it's pretty obvious that ssh reads from stdin if you don't provide ...
    (comp.unix.shell)
  • queuing up user input via writes to STDIN
    ... I have a program that reads STDIN for user commands while the program is ... I'm adding the capability to queue up user commands from the shell ...
    (perl.beginners)
  • Re: Redirecting stdin to a file requiring parameters
    ... The point is, in a pipeline like cat | sh, where stdin must serve both for ... commands that read from stdin can vary depending upon how the lines of the ... The shell consumes all stdin when reading the first line of the script, ... "cat" gets EOF. ...
    (comp.unix.shell)