Norman Ramsey wrote:
> > The simplest thing is to use readFile (from the Prelude) instead of
> > using handles. readFile will take care of everything for you when the
> > time is right.
>> Thanks---I'll try it. Somehow my hoogle query missed readFile...
> undoubtedly because I asked for 'String -> IO String' instead
> of 'FilePath -> IO String'. Dunno if this is a bug or a feature,
> since as far as the compiler is concerned, FilePath and String are the
> same type...
There seems to be a misunderstanding here: readFile in itself is not the
solution. readFile is defined thus:
readFile name = openFile name ReadMode >>= hGetContents
and the original code was this:
load fn = do handle <- IO.openFile fn IO.ReadMode
contents <- IO.hGetContents handle
IO.hClose handle
return $ XP.xmlParse fn contents
Sure, you can replace the openFile/hGetContents pair by readFile, but the real
problem is the presence of the hClose. Removing that will solve your problem
(but note that you now have no control over when the file is actually closed).
Cheers,
Simon