From: Ara.T.Howard@... Date: 2004-10-16T11:24:28+09:00 Subject: Re: Popen and operations failing later On Sat, 16 Oct 2004, David G. Andersen wrote: > This is likely my brain missing something, but I'm having a hard > time figuring out the source of this error. I have code that > does: > > class foo > > def get_next_line > if the file isn't open > @file = popen("zcat ") > end > begin > line = @file.readline > return line > rescue => err > print "eek: #{err}\n" > end > end > > def process > line = get_next_line > process_line(line) > end > > class bar < foo > def process_line(line) > .. do something with the line .. > .. write to a file .. > end > end > > The problem is that the popen exits, typically with a > non-zero exitcode (garbage at the end of the gzipped file). > The next time the program does something in the > process_line function - typically writing to a file, > or reading/writing to/from the database - _that_ > operation generates an error. > > I assume I'm doing something wrong with respect to > trapping the errors from the popen, but I'm baffled > about exactly what I should do. > > Clues _very_ appreciated; I've been bashing my head > on this for a while. Stuffing in lots and lots of > begin / end checks around the operations in process_line > typically does the trick, but it's very.. uhh, inelegant. > > Thanks! not sure if it's important but irb(main):001:0> pipe = IO::popen 'bash -c exit' => # irb(main):002:0> pipe.read => "" irb(main):003:0> pipe.read => "" irb(main):004:0> pipe.read => "" irb(main):005:0> pipe.read => "" irb(main):006:0> pipe.read => "" just because the process is dead - you still can 'read' from the pipe. regards. -a -- =============================================================================== | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov | PHONE :: 303.497.6469 | When you do something, you should burn yourself completely, like a good | bonfire, leaving no trace of yourself. --Shunryu Suzuki ===============================================================================