views:

149

answers:

1

I want to do a popen() / python's subprocess.communicate from Haskell - start a program, give it stdin, and get its stdout/stderr. What's the most direct / Haskellish way to do this?

+8  A: 

Pick either MissingH's System.Cmd.Utils and the standard library's System.Process. They're easy to use, with both high-level convenience functions (which you simply throw strings at and get strings back, possibly lazily) and low-level plumbing functions (which actually give you handles, like most popen functions in other languages/frameworks).

import System.Process

main = do
    let cmd = "mail"
        args = ["root@localhost", "-s", "does this act like popen?"]
        input = ["Hello, world!"]
    (rc, out, err) <- readProcessWithExitCode cmd args input
    putStrLn $ "mail exited: " ++ show rc
    mapM_ putStrLn $ map ("out: " ++) $ lines out
    mapM_ putStrLn $ map ("err: " ++) $ lines err
ephemient
Oops, `input` should be a `String` not a `[String]`. Well, you get the idea :)
ephemient
You can edit your post, you know... :)
Porges