lua-users home
lua-l archive

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


On Tue, Jul 6, 2010 at 8:52 PM, Hisham <hisham.hm@gmail.com> wrote:
> ...given that io.popen can only do reading or writing but not both...

Lacking anything else, bidirectional piping could be implemented by
writing to a temporary file.  I recall discussions on this problem in
the past, e.g. [1].

  [1] http://lua-users.org/lists/lua-l/2007-10/msg00241.html

> The -pipe trick felt like
> a neat way to trigger evaluation after a chain of __call's, though.

That's the http://lua-users.org/wiki/MethodChainingWrapper pattern.

> However, I think a number of the things we use pipes for in shell
> script (grep, wc, sort and the like) can be reimplemented as pure Lua
> functions in this library, or would simply turn into things like
> table.sort in Lua scripts....
> I believe that instead of focusing on duplicating sh features, it's
> more important to make it easy to manipulate data from external
> commands (a few utility functions to make iterators or tables out of
> the outputs could go a long way).

Accessing a shell interface via a programming language seems to me
analogous to accessing an SQL database via a programming language.
So, you have some functions for manipulating SQL strings, particularly
quoting values into SQL strings.  Then you have functions for
executing those SQL strings and reading the result back in various
ways, such as all at once into a table or incrementally via a
statement handle.  Unfortunately, the concept of bind parameters (like
LuaDBI [2]) which avoids quoting doesn't apply in shells, nor for that
matter in many database drivers either (luasql), except maybe in a
limited way via input/output pipes which can accept unescaped data.

  [2] http://lua-users.org/lists/lua-l/2009-02/msg00322.html

I don't see the benefit of `pipe.ls("/dev").grep("^sd.*")` compared to
`exec "ls /dev | grep ^sg.*".  This reminds me of some wrappers around
SQL that add a layer of bloat.

>> Do you think that we need to support non-POSIX environments? (Since
>> you need this module for LuaRocks, I suppose that you do...)

I think the general problem we're often trying to solve is

  (1) There are functions missing or not readily accessible in the Lua universe.
  (2) There is a wealth of functions in some other universe such as
some OS shell (sh, Cygwin/MSYS, and don't forget Windows PowerShell)
or some other scripting language (Python/Perl/Ruby).
  (3) We prefer simple interfaces over performance in accessing that
other universe via Lua.  Sending text strings back and forth if it
allows us to avoid compiling/installing anything is fine.
os.execute/io.popen often do this, but other ways sometimes include
sockets, named pipes, or things like www.gtk-server.org.

Some implementations of http://lua-users.org/wiki/SleepFunction illustrate this.