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
2009-11-11 01:29:57
Oops, `input` should be a `String` not a `[String]`. Well, you get the idea :)
ephemient
2009-11-11 02:20:30
You can edit your post, you know... :)
Porges
2009-11-11 02:29:31