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

Per Hedeland per@REDACTED
Mon Feb 4 00:12:44 CET 2008


Evan Miller <emmiller@REDACTED> 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


--Per Hedeland



More information about the erlang-questions mailing list