queuing up user input via writes to STDIN



All:

I have a program that reads STDIN for user commands while the program is
running.

I'm adding the capability to queue up user commands from the shell
command line when the program is invoked.

I'd like to be able to queue the command line data up in STDIN, so that
when the program goes into a loop reading STDIN, it will see the command
line data that was queued up as if it came from the command line.

Here's an example:

========================================================================
===

#!/usr/bin/perl

use strict;
use warnings;

my $kbcmd;

#close STDIN;
#open(STDIN, ">"); <== doesn't work so swell

foreach (@ARGV) {
print "input parameter:$_\n";
# print STDIN "$_\n";
# this fails with " Filehandle STDIN opened only for input", see
below
}

#close STDIN;
#open(STDIN, "<");

# and here's a loop that reads STDIN for user commands
while(1) {
$kbcmd = <STDIN>;
print "keyboard command received: $kbcmd";
}

$ perl -d test3.pl a b c

Loading DB routines from perl5db.pl version 1.28
Editor support available.

Enter h or `h h' for help, or `man perldebug' for more help.

main::(test3.pl:8): my $kbcmd;
DB<1> s
main::(test3.pl:10): foreach (@ARGV) {
DB<1>
main::(test3.pl:11): print "input parameter:$_\n";
DB<1>
input parameter:a
main::(test3.pl:12): print STDIN "$_\n";
DB<1> s
Filehandle STDIN opened only for input at test3.pl line 12.
at test3.pl line 12
========================================================================
====

Of course, there are lots of other ways to do this, like saving the
commands in an array and executing the array elements as if they were
keyboard commands, or opening a pipe to another program that fed
commands in.

Is there a way to do this with manipulation of STDIN?

thanks,
Gavin Bowlby

.



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)
  • STDIN on the fileevent handle
    ... I am using STDIN to feed a fileevent handle and populate a status ... So, for example, if I type in one of four acceptable commands ... # Configure the top level widgets ... chomp $value; ...
    (comp.lang.perl.tk)
  • 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)