lua-users home
lua-l archive

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


On Mon, Apr 23, 2012 at 9:46 PM, Coda Highland <chighland@gmail.com> wrote:

>>>  proc{'find', "/usr", "-name", pattern, "-print0"}.pipe{'xargs', '-0', 'du'}()
>>>  find /usr -name $pattern -print0 | xargs -0 du

> for file in path.find{basedir="/usr", name=pattern} do
>  print file, fs.stat(file).size
> end
>
> If you're thinking about it using your bash/POSIX idioms you're not
> taking advantage of the fact that you have a scripting language!

Absolutely. There are a couple different things hiding in here though.

First, I needed to pick example commands recognizable by a Unix
audience. I figured everybody thinking about syntax for pipeline
creation had documentation on find, xargs, and du at hand.

My -print0 *was* a subtle dig at using pipelines for this; there are a
zillion scripts out there like mine which use -print instead, but they
break when they encounter a file with a newline in the file name. If
Lua handles path.find() (and xargs if calling out) there's no problem
as paths do not have to be marshalled into records by find and then
unmarshalled by xargs.

A significant part of the time it's not du at the end; it's
arm-none-eabi-nm or filefrag or some other non-trivial component. I
didn't want to use an example that itself needed explaining. To be
fair, one of my favorite commands is perl's "rename" which just feeds
its args into $_ and runs a perl expression on it to generate a rename
target; "rename 's/^/old-/' *.txt" is fun.

That leads into the second point. The all-Lua implementation is
clearer, but it is also twice as long as the shell. (I'm not charging
it for require"lfs" require"path" :-) . Even the Lua pipeline
specification is shorter than the in-language iteration. People
serious about the Occupy Bourne Shell movement have a bunch of work to
do in taking common shell idioms and patterns and rendering them in
Lua. Reducing the cognitive and keyboard cost of interoperation with
processes/pipelines is a lot of it. I'm thinking about something like
https://dev.openwrt.org/browser/branches/backfire/package/base-files/files/sbin/sysupgrade
as a use case.

I think you'll end up reimplementing xargs in Lua, or to keep the
process parallelism, wrapping it enough that you can hand in a Lua
array and not have to worry about syntax. On the positive side, wraps
stand a chance of fixing long-standing problems--create a file named
"-i" in the current directory and watch scripts using "*" blow up
spectacularly. Wrap enough stuff and it starts to look like PowerShell
I guess.

My (sketchy) belief is that Lua syntax is not flexible enough to be
ergonomic for bouncing in and out of process interaction, which is why
I'm muttering about proc:$'find /usr -name $pattern -print0 | xargs -0
du' syntax; we can do little languages with strings, but without
compile-time syntax there's no way of capturing the lexical value of
the variable "pattern". But I don't know. I haven't really made a
full-force run at "write no shell scripts, write Lua script
infrastructure whenever you find something missing". I have
closed-UTF-8 string.* higher on my todo list since it screwed me again
the other day.

Jay