Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Lua for configuration/plugins? [closed]

As far as I can see Lua is basically a normal script language, not so different from PHP, Python or Ruby, which are much more famous.

But on the other hand there are a lot of projects (e.g. the awesome window manager) which require configuration and/or plugins in Lua. So I think there should be reasons that make a generally unknown language the favorite choice for so many talented developers. Can you name some?

What do you gain from using/learning Lua?

like image 802
erikbstack Avatar asked Mar 08 '13 16:03

erikbstack


People also ask

Why is Lua used for plugins?

Lua is relatively lightweight, and designed to be embedded in (and interact with) applications easily. This often makes it a better choice than heavier languages that are designed to run on their own and create their own whole little world.

What are Lua plugins?

In simple terms, a Lua plugin is nothing more than a module or series of modules that provide the end user with a set of methods (functions) and properties (variables) that can be called to execute the code you're providing.

Does vim have Lua support?

Vim has good Lua support, but it's API is smaller of course.


2 Answers

Lua was both born with the primary goal of scripting and extending a hosting application written in C. Whilt it has gone on to become powerful free-standing languages, it has preserved the feature of easy embedding in an application as a primary feature.

As I understand its history, PHP came out of the need to have easier ways to write Web applications. While it can be used independently of a web server, that use is not nearly as common.

Python and Ruby (like Perl) started life as free-standing languages with specific design goals. While it is possible to embed a python interpreter in a larger application, that was not a feature that was important to its user community in the early days. Python is today far more likely to be found embedded in an application than Perl, but both shine as the framework on which applications ranging from small scripts to full-featured GUIs are built.

Ruby (also like Perl) has achieved a lot of visibility in the web application domain, with the Rails framework likely the cause. While I really can't say a lot about Ruby from personal experience, it regularly places fairly highly in the Tiobe Index. My (limited) understanding is that embedding a Ruby interpreter in a larger application was not an early design goal, but is possible to do and apparently even supported by more recent builds. A quick scan just now of questions at SO about embedding Ruby seemed to yield a consensus of "don't do that, use Lua instead" balanced by "it is possible, go for it".

Perl is certainly best known as a stand-alone scripting language that predates the modern web. As CGI was developing, Perl found a home there thanks to being interpreted which made writing CGI scripts in Perl for running at a commercially hosted web server with limited (or no) command line access actually practical. While it is possible (and even fairly easy these days) to extend Perl with library code written in C, it has never been very easy to embed a Perl interpreter in an application. My sense from a lot of years on the fringes of the Perl community was always that embedding was never a priority.

Lua has a nice balance of powerful features and plain syntax. It is often quite easy for even non-programmers to read and modify Lua scripts. Using it as a configuration language is often the first step toward a fully scriptable application. It is straight-forward to use Lua to parse and execute a configuration file once at startup to get program settings. Once that functionality is available, using the Lua core for other tasks becomes easy ("it was just sitting there"). As a natural progression, it isn't unusual to end up with an application built out of a thin veneer of C code, core "hard" functions in C, UI framework in C, and the majority of the user experience scripted in Lua. As an example, Adobe Lightroom is built that way.

It doesn't hurt that the entire Lua source kit, including the reference manual and a binary distribution only needs a megabyte or so of storage. (On my Windows box right now, Lua and its DLL total just over 200KB, compared to Perl and its DLL which weigh in at about 1.7MB. For most applications, that implies that adding a Lua interpreter costs about 200KB in additional executable size.)

One other factor to consider is likely the license. Like many successful scripting languages, Lua is open source. However, it is licensed under the simple to understand MIT license which explicitly permits commercial use. While products released under the GPL (or more likely LGPL) can make their way into commercial settings, it gives the lawyers more headaches. A simple license such as that applied to Lua makes it easy to get your legal department to agree. As a result, Lua is used in some significant commercial products, and the developer teams of those products have been visible in the Lua language community.

Edit: I've revised my discussion of Ruby thanks to comments pointing out its richer history prior to the Rails framework. That provided an excuse to give Perl its own paragraph and to brag about Lua's size with concrete measurements. Yes, I do like Lua. A lot.

like image 52
RBerteig Avatar answered Sep 30 '22 14:09

RBerteig


Lua is relatively lightweight, and designed to be embedded in (and interact with) applications easily. This often makes it a better choice than heavier languages that are designed to run on their own and create their own whole little world.

like image 42
cHao Avatar answered Sep 30 '22 13:09

cHao