[Haskell-cafe] Emulating bash pipe/ process lib

Donn Cave donn at drizzle.com
Thu Feb 9 12:58:27 EST 2006


On Thu, 9 Feb 2006, Marc Weber Marc Weber wrote:
...
> So my simple test looks like this:
> 
> 
> module Main where
> import System.IO
> import System.Posix.IO
> 
> main = do
>   (fdIn,fdOut) <- createPipe
>   let (iohIn, iohOut) = (fdToHandle fdIn, fdToHandle fdOut)
>   hIn <- iohIn
>   hOut <- iohOut
>   hPutStr hIn "test"
>   line <- hGetLine hOut
>   print line -- should now print test having been piped through my pipe
> 
> but I get the error:
> pipe2: <file descriptor: 3>: hPutStr: illegal operation (handle is not
> open for writing)

That's right.  "The first component is the fd to read from, the second
is the write end."  You're writing to the read end.

> And in current CVS docs in base.System.Process.hs it is said that
> createPipe is no longer exported ?

Maybe they're just going to export functions like this under their
common POSIX names ("pipe", "dup2"), and bring them more into line
with standard behavior (execve takes argv[0..n], not argv[1..n].)

"Slow" devices like pipes, sockets etc. get along fine with Handles
or whatever buffered I/O - as long as you have only one going at a time.
Multiple input sources - like, say you want to read a process' output
(unit 1) and diagnostic output (unit 2) separately, and either one has
the potential to fill up the pipe and block while you're waiting for
input on the other pipe - buffers at least complicate the dispatching
mechanics you need for this, if not make it impossible.  For my money,
it makes more sense to learn how deal with pipes directly at the file
descriptor level, and at worst if the committees find it too unsightly
you may have to write FFI interfaces to get pipe() back.

	Donn Cave, donn at drizzle.com



More information about the Haskell-Cafe mailing list