[comp.lang.perl] you still have to type something, silly user

thoth@reef.cis.ufl.edu (Robert Forsman) (04/17/91)

  I have a program that maintains a database of files and their vital
stats (CRC, mtime, ctime, etc).  When updating, it takes a list of
files from <> and when <> runs out it inquires if the user wants to
update the database on file.
  But!, <> is empty.  How do I reopen the tty for input.  I have a
feeling that my problem is woefully underspecified, but take a stab at
it anyway.

  Yes, I'm probably doing things pretty goofy.  I'll talk to my boss
who commissioned this project and tell him about these minor problems.
  I'm reading files from <> and thus STDIN (the terminal) may still
have input, but I want to know the politically correct way to reopen
the terminal under perl anyway.  A solution that works (a very fuzzy
term: "works") when STDIN still has input would be the best.
--
"Rob, you're stupid, and that makes you dangerous." - chess opponent
I deal with Reality as you _don't_ understand it. 
"The way I see things, food exists to give texture to the taste of ketchup."

lwall@jpl-devvax.jpl.nasa.gov (Larry Wall) (04/19/91)

In article <THOTH.91Apr16210703@reef.cis.ufl.edu> thoth@reef.cis.ufl.edu (Robert Forsman) writes:
: 
:   I have a program that maintains a database of files and their vital
: stats (CRC, mtime, ctime, etc).  When updating, it takes a list of
: files from <> and when <> runs out it inquires if the user wants to
: update the database on file.
:   But!, <> is empty.  How do I reopen the tty for input.  I have a
: feeling that my problem is woefully underspecified, but take a stab at
: it anyway.
: 
:   Yes, I'm probably doing things pretty goofy.  I'll talk to my boss
: who commissioned this project and tell him about these minor problems.
:   I'm reading files from <> and thus STDIN (the terminal) may still
: have input, but I want to know the politically correct way to reopen
: the terminal under perl anyway.  A solution that works (a very fuzzy
: term: "works") when STDIN still has input would be the best.

As you say, a bit underspecified, but you can get at the terminal regardless
of what STDIN is attached to by saying:

	open(TTY, "</dev/tty");
	$ttyline = <TTY>;

You might have to get fancier if you want to write on the tty as well.  Here's
one way:

	open(ITTY, "</dev/tty");
	open(OTTY, ">/dev/tty");
	select((select(OTTY), $| = 1)[0]);
	print OTTY "prompt: ";
	$ttyline = <ITTY>;

You can arrange to read and write the same filehandle, but you may have
to turn around stdio between reads and writes with a seek.  I should really
make +> opens on special files do the double stdio stream trick like sockets
do...

Larry