Lua as a general-purpose scripting language?

12,448

Solution 1

Just because it is "marketed" (in some general sense) as a special-purpose language for embedded script engines, does not mean that it is limited to that. In fact, WoW could probably just as well have chosen Python as their embedded scripting language.

Solution 2

Lua is a cool language, light-weight and extremely fast!

But the point is: Is performance so important for those tasks you mentioned?

  • Renaming a bunch of files
  • Download some files from the web
  • Webscraping

You write those programs once, and run them once, too maybe. Why do you care about performance so much for a run-once program?

For example:

  1. Cost 3 hours to write a C/C++ program, to handle data once, the program will take 1 hour to run.
  2. Cost 30 Minute to write a Python program to handle data once, the program will take 10 hours to run.

If you choose the first, you save the time to run the program, but you cost your time to develop the program.

On the other hand, if you choose the second, you waste time to run the program, but you can do other things when the program is running. How about play World of Warcraft, kill monsters with your warlock? Eat my D.O.T! :P

That's it! Although Lua is not so difficult to write, everything about Lua is designed to be efficient.And what's more, there are little modules for Lua, but there are so many modules for Python. You don't want to port a C library for Lua just for a run-once program, do you? Instead, choose Python and use those module to achieve your task easily might be a better idea.

FYI: Actually, I have tried to use Lua to do webscraping, but finally, I realized I do not have to care so much about language performance. The bottleneck of webscraping is not on the performance of the language. The bottleneck is on network I/O, HTML parsing and multitasking. All I have to do is make sure the program works and find the bottleneck. Finally, I chose Python rather than Lua. There is so many excellent Python modules; I have no reason to build my own.

According to my experience about webscraping, I chose Twisted for network I/O and lxml for html parsing as the backend of my webscraping program. I have wrote an article for an introduction to this technology.

The best choice to grab data from websites: Python + Twisted + lxml

Hope this is helpful.

Solution 3

Lua has fewer libraries than Python. But be sure to have a look at LuaForge. It has a lot of interesting libs, like LuaCURL, wxLua or getopt.

Then, visit LuaRocks, the package management system for Lua. With it, you can search and install most mature Lua modules with dependencies. It feels like RubyGems or aptitude.

The site lua-users.org has a lot of interesting resources too, like tutorials or the Lua Wiki.

What I like about Lua is not its speed, it's its minimal core language, flexibility and extensibility.

That said, I would probably use Python for the tasks you mentionned because of the larger community doing such things in Python.

Solution 4

It's probably because Lua was designed as a scripting and extension language. On the official site it's described as a powerful, fast, light-weight, embeddable scripting language. There's nothing stopping you from writing general purpose programs for it (if I recall correctly it ships with an interpreter and compiler), but the language designers intended it to be used mainly as an embedded language (hence being light-weight and all)

Solution 5

This is a sociological question, not a programming question.

I use Lua for general-purpose scripting almost exclusively. But I had to write a few hundred lines of code so that Lua would play better with the shell. This included such tricks as

  • Quoting a string so it is seen as one word by the shell
  • Writing a function to capture the output of a command as in shell $(command)
  • Writing a function to crawl the filesystem using the Lua posix library and expand shell globbing patterns

(For those who may be interested, I've left the code in my Lua drop box, which also contains some other stuff. The interesting stuff is probably in osutil in os.quote, os.runf, os.capture, and maybe os.execve. The globbing is in posixutil.lua. They both use Luiz Henrique de Figuereido's Lua Posix library.)

To me, the extra effort is worth it because I can deal with simple syntax and great data structures. To others, a more direct connection with the shell might be preferred.

Share:
12,448
user3275601
Author by

user3275601

Updated on June 13, 2022

Comments

  • user3275601
    user3275601 almost 2 years

    When I see Lua, the only thing I ever read is "great for embedding", "fast", "lightweight" and more often than anything else: "World of Warcraft" or in short "WoW".

    Why is it limited to embedding the whole thing into another application? Why not write general-purpose scripts like you do with Python or Perl?

    Lua seems to be doing great in aspects like speed and memory-usage (The fastest scripting language afaik) so why is it that I never see Lua being used as a "Desktop scripting-language" to automate tasks? For example:

    • Renaming a bunch of files
    • Download some files from the web
    • Webscraping

    Is it the lack of the standard library?

  • TM.
    TM. over 14 years
    Good answer, although I'm trying to think of what crazy one-time usage scheme you could come up with that would run 10x slower in a Python than in C... most of these one-off scripts tend to be limited by disk anyway.
  • RyanE
    RyanE over 14 years
    Do you have a link to those few hundred lines of code somewhere?
  • Peter Hansen
    Peter Hansen over 14 years
    @TM, agreed. And generally you can use some of the 2.5 hours saved to use a better algorithm and end up running faster than C anyway, not to mention having more confidence it's working properly and is more maintainable.
  • Charles Stewart
    Charles Stewart over 14 years
    I don't think Python would be such a good choice. The difference between Lua and Python for embedding, is that Lua's semanticsare designed to minimise problems working alongside other code, whereas Python generally assumes that it is control.
  • Jimmy
    Jimmy almost 13 years
    Which GUI toolkit do you prefer, IUP or wxLua?
  • RBerteig
    RBerteig almost 13 years
    @Jimmy, personally, I've used IUP for several small projects and have been very happy with it. Its greatest strength is also its weakness: it likes to do its own layout of controls with a boxes and glue model, so it is really easy to specify a dialog box full of controls, but hard to make its layout perfectly match other dialogs with a different mix of controls. I've tried the wxLua demos, and it is clearly a quality product, but I haven't had a personal reason to switch.