[Haskell-cafe] Automatic file closing after readFile

Magnus Therning magnus at therning.org
Thu Oct 18 04:28:21 EDT 2007


I recently bumped into a problem with the feed on my WordPress blog.
The problem was an extra empty line at the top of the XML file which
some parsers choked on.  I suspected there was some PHP file that
contained an extra empty line at the top or bottom of the file so I
hacked up this:

    hasEmpty s = let
            _first_empty = s !! 0 == '\n'
            _last_empty = (reverse s) !! 1 == '\n'
        in _first_empty || _last_empty

    loadAndCheck fp = liftM hasEmpty $ readFile fp

    main = getArgs >>= filterM loadAndCheck >>= mapM_ putStrLn

Maybe not the most effective piece of code, but it worked.

The one problem I had was that running this on /all/ files in my WP resulted in
an exception:

    *** Exception: ./wp-includes/images/smilies/icon_mrgreen.gif: openFile: resource exhausted (Too many open files)

Is there some (easy) way to avoid this while still using readFile?

/M

-- 
Magnus Therning                             (OpenPGP: 0xAB4DFBA4)
magnus@therning.org             Jabber: magnus.therning@gmail.com
http://therning.org/magnus

Beauty is more important in computing than anywhere else in technology
because software is so complicated. Beauty is the ultimate defence
against complexity.
     -- David Gelernter
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20071018/e879d460/attachment.bin


More information about the Haskell-Cafe mailing list