I was playing around a bit here on an idea based on an unanswered question I found on stackoverflow.
- Namely this one: Julia - pipe to Julia REPL - Stack Overflow
The following is related and has a more direct answer:
- Running an external program with sequential inputs from Julia 0.5.1 - Stack Overflow
- Related to this one too: https://discourse.julialang.org/t/running-an-external-program-with-sequential-inputs
So, I threw together some tests:
# This function is borrowed from: gaston_aux.jl
function popen3(cmd::Cmd)
pin = Base.Pipe()
out = Base.Pipe()
err = Base.Pipe()
r = spawn(cmd, (pin, out, err))
Base.close_pipe_sync(out.in)
Base.close_pipe_sync(err.in)
Base.close_pipe_sync(pin.out)
Base.start_reading(out.out)
Base.start_reading(err.out)
return (pin.in, out.out, err.out, r)
end
Then a function to read the output because I couldn’t get anything else to work.
function readPipe(out)
if out.buffer.size >0
s = readavailable(out)
return string([Char(st) for st in s]...)
end
return ""
end
I run/start it with this:
in, out, err, process = popen3(`julia`)
Now the fun part…
julia> write(in, string("a = 3","\n"))
6
julia> readPipe(out)
"3\n"
julia> write(in, string("println(\"hello: $(a)\")","\n"))
ERROR: UndefVarError: a not defined
julia> write(in, string("println(\"hello: $a\")","\n"))
ERROR: UndefVarError: a not defined
julia> write(in, string("println(\"hello: \", a)","\n"))
22
julia> readPipe(out)
"hello: 3\n"
So far, everything I’ve tried to run works with the exception of string interpolation. So, the question is… am I doing something wrong or is it broken?
…also If you have any pointers on a better way of doing this please do tell.
EDIT: as an afterthought it occurs to me that julia is probably trying to interpolate the string in the current context and there is no “a” defined. Any thoughts on how to prevent this?