String interpolation not working in julia pipe to julia !?!

I was playing around a bit here on an idea based on an unanswered question I found on stackoverflow.

The following is related and has a more direct answer:

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?

Ok, for those that may be interested I found the solution.

Example:

julia> write(in, string(raw"println(\"hello: $a\")", "\n"))
21

julia> readPipe(out)
"hello: 3\n"

Just to explain why: you were trying to interpolate a from the parent process while it’s only defined in the child process. You could put a backslash in front of the $ in either of your original attempts and it would work too.

Cool, thanks for the tip. If I keep playing around with julia I might get good at it someday. Meanwhile, I’m having fun.