From: "Ara.T.Howard" Date: 2005-06-18T01:17:38+09:00 Subject: Re: stubborn program using readline On Fri, 17 Jun 2005, Wybo Dekker wrote: > On Thu, 16 Jun 2005, Logan Capaldo wrote: > >> Theory: readline reads from STDIN correct? using rltest < rltest means >> that STDIN gets EOF, which means STDIN is closed which means readlien >> can't read anything from it. > > No, readline should not necessarily read from STDIN. It can read from the > console, /dev/tty or so. > >> I don't think you can have your cake and >> eat it too in this case. If you want the interactive prompt your going >> to have pass the file name on the command line instead of using IO >> redirection. I'm a bit confused as to the purpose of this. > > What I want is a program that reads data from either a file or STDIN and > then lets the user run various commands on those data. put another way - you want the program to ALWAYS read from EITHER a file or stdin and you ALWAYS want to interactively (eg. from a tty) run commands after reading the file - so why not just say that in the code? # # program which does the above # jib:~ > cat a.rb require 'readline' include Readline inpath = ARGV.shift infile = inpath ? open(inpath) : STDIN # count lines in stdin (the mail message): n=0; while infile.gets: n+=1 end puts "#{n} lines" # always get commands interactively STDIN.reopen '/dev/tty' unless STDIN.tty? readline("type return to quit: ") puts "quitting..." # # reading interactively from stdin and typing ctrl-D # jib:~ > ruby a.rb one two three 3 lines type return to quit: quitting... # # reading non-interactively from stdin # jib:~ > ruby a.rb < a.rb 13 lines type return to quit: quitting... # # reading from file # jib:~ > ruby a.rb a.rb 13 lines type return to quit: quitting... you could take other approaches with dup'ing and forking but this might work for you. hth. -a -- =============================================================================== | email :: ara [dot] t [dot] howard [at] noaa [dot] gov | phone :: 303.497.6469 | My religion is very simple. My religion is kindness. | --Tenzin Gyatso ===============================================================================