Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!elroy.jpl.nasa.gov!jpl-devvax!lwall From: lwall@jpl-devvax.jpl.nasa.gov (Larry Wall) Newsgroups: comp.lang.perl Subject: Re: Reading from /dev/tty in forked process. Message-ID: <1991May25.045321.2826@jpl-devvax.jpl.nasa.gov> Date: 25 May 91 04:53:21 GMT References: <1991May24.163027.12281@amc.com> Reply-To: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Distribution: na Organization: Jet Propulsion Laboratory, Pasadena, CA Lines: 63 In article <1991May24.163027.12281@amc.com> stuart@anabol.amc.com () writes: : Warning: I'm new to perl. : : My question is how do you get perl to stop and read from /dev/tty when : you have a forked process sub function that is taking its' input from : STDIN. : : For example, say under sh I decide write a "Pager" function that prints : a screen full of lines then prompts the user to hit return to get to : the next screen of lines. : ... : Thinking along these lines I tried this perl program: : : #!/usr/local/bin/perl : : sub pager { : local($pagesize,$linecount) = (24,1); : open(TTYIN,") { : if ( $linecount >= $pagesize ) { : print "Hit return to continue: "; : $ans = ; : print "\n" ; : $linecount = 1; : } : print ; : $linecount++ ; : } : close(TTYIN); : } : : : $pid = open (OUT,"|-"); : if (!$pid) { : &pager; : exit; : } else { : open(INPUT,"data") || die "Cannot open data "; : while() { : print OUT $_; : } : } : : This prints out the "Hit return to continue" messages but it doesn't : wait for the return, it keeps on going. Oddly enough, it stops for me. It only needs two things to work right here. First, $| = 1 so that the prompt comes out before it stops, and a close OUT after the final loop so that the input process waits for the pager process to finish. As to why yours isn't stopping, I couldn't say. P'raps your stdio thinks TTYIN is always at EOF for some reason. Try sysread(TTYIN, $ans, 1) and see if it helps. : Most likely my problem is I'm not thinking in perl yet, but I'm : tryin'. Looks to me like you're doin' a durn good job of thinking in Perl. (At least, for someone from the north end of the lake...) ;-) Larry