What about something like this:

#!/usr/bin/perl
use Term::ReadKey;

my @data;
ReadMode 1; # Turn off controls keys

my $count=1;
while ($count < 10) {

my $key = ReadLine(0);

if ($key =~ m/Done/) {
$count=12
} else {
        push(@data,"$key");
}

}

ReadMode 0; # Reset tty mode before exiting

foreach my $entry (@data){
        print "$entry\n";
}

exit;



Gavin Bowlby wrote:
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



--
Michael Gale

Red Hat Certified Engineer
Network Administrator
Pason Systems Corp.

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to