lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It was thus said that the Great Mike once stated:
> Hello, everybody.
> 
> Is there a way to run a piece of code from the currently executing lua script
> (I mean a lua chunk or a function) in a "subprocess" and establish a pipe
> to/from its stdin/stdout something like io.popen() does?
> 
> This is somewhat different from the launching a subprocess for an external
> program or for the lua script itself that may be done with io.popen()
> as the launched chunk is to be able to access the current values of the
> currently executing lua script global variables and/or upvalues
> (at least their copies in the forked process's image).
> More simply, I mean the feature like "process substitution" in bash,
> for example:
> -----
> #! /bin/bash
> 
> greeting="Hello world!"
> 
> do-greeting()
> {
>   echo "$greeting"
> }
> 
> cat < <(do-greeting)
> -----

  Yes, but it will involve calling pipe(), fork() and dup2() at a minimum,
which means you need something like luaposix (or lposix or whatever it's
called).  Also, depending on how you process the data, you may need to call
io.setvbuf() on the resulting streams (if you are using the io.* library to
read the pipes).  

  -spc