lua-users home
lua-l archive

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



function execute(command)
     local lines = {}
     local file = io.popen(command)
     for line in file:lines() do
         table.insert(lines, line)
     end
     file:close()
     return lines
end

Unfortunately, when you're combining popen with a lines iterator it's easy to become a victim of the infamous "broken pipe" phenomenon. Here's why:

There's a suble difference between calling the lines iterator and reading a line using file:read "*l". Both try to fetch the next line and will check for file errors. However, the iterator will *throw an alert* if it sees an error, while the regular read method simply returns nil (plus extra values to indicate what went wrong).

On my cheapo Windows XP machine the lines-driven for-loop (as shown above) always comes to a screeching halt. Using a loop around a read "*l" call may look less elegant, but works just fine. Obviously, in the latter case the broken pipe situation is still there. It just doesn't bring the script to its knees, because no alert is thrown.

I've only tested this using the Lua 5.2 beta and did not try Lua 5.1. But it looks like a similar error throwing logic is in place there. So, keep that in mind.

Ashwin.