[erlang-questions] Newbie: half-closing a port?

Evan Miller <>
Mon Feb 4 02:54:19 CET 2008


Per Hedeland wrote:
> Evan Miller <> wrote:
>> I would like to be able to do something like this just before closing:
>>
>> Port ! {self(), eof}.
>> % read output...
>>
>> Is anything like this possible?
> 
> As you can figure out from Tony's response, it isn't (and FWIW, I agree
> that it would be a good thing to add - it would fill a "functionality
> hole"). Assuming Unix and "text" input to the program, you can work
> around the problem with this hack though:
> 
> 1> EOFwatcher = "sed -n -e /END-OF-FILE/q -e p | ".
> "sed -n -e /END-OF-FILE/q -e p | "
> 2> Port = open_port({spawn, EOFwatcher ++ "sed 's/foo/bar/'"}, []).
> #Port<0.88>
> 3> Port ! {self(), {command, "foo\n"}}.
> {<0.30.0>,{command,"foo\n"}}
> 4> Port ! {self(), {command, "END-OF-FILE\n"}}.
> {<0.30.0>,{command,"END-OF-FILE\n"}}
> 5> flush().
> Shell got {#Port<0.88>,{data,"bar\n"}}
> ok

Thanks for the hack, Per.

While we're making a wish-list for the spawn driver, it would also be 
nice to be able to read stdout and stderr separately.

Evan

> 
> 
> --Per Hedeland




More information about the erlang-questions mailing list