+4 votes
187 views
in Web development by (242k points)
reopened
What is Lua ?: an introduction to the programming language

1 Answer

+5 votes
by (1.6m points)
edited
 
Best answer

What is Lua?
How is programming with Lua structured and what is it used for?
In what areas is Lua used?
Lua: a programming language with great strengths and small weaknesses
Introduction to the Lua programming language (with code snippets)
First example: line breaks in Lua
Second example: variables in Lua

image

What is Lua ?: an introduction to the programming language

? Lua? is a Portuguese term that means? moon ?. The eponymous programming language has several peculiarities, such as its platform-independent extensibility, its small size of only 120 kilobytes, and its high speed - reasons enough to take a closer look at this scripting language..

Index
  1. What is Lua?
  2. How is programming with Lua structured and what is it used for?
  3. In what areas is Lua used?
  4. Lua: a programming language with great strengths and small weaknesses
  5. Introduction to the Lua programming language (with code snippets)
    1. First example: line breaks in Lua
    2. Second example: variables in Lua

What is Lua?

The universe of Internet programming languages ​​is almost infinite. Lua manages to stand out from the crowd, but in terms of popularity, it's still relatively little known compared to Python or Perl, for example. However, Lua has existed for over two decades, although rarely used as independent programming language: developers often use as language scripting integrated into individual programs. Its main fields of application have always been video games and game engines , but Lua is also used to develop a large number of network and system programs .

Lua was developed at the Catholic University of Rio de Janeiro by Roberto Ierusalimschy, Luiz Henrique de Figueiredo and Waldemar Celes. Until 1992, the importation of hardware and software was subject to strict limitations in Brazil. Due to this need, all three programmers developed their own scripting language and named it Lua. Today, it has become one of the most important and popular programming languages ​​in many fields..

What is Lua? This scripting language basically consists of a library that programmers can incorporate into their software to make it programmable. Lua is available for all common operating systems and contains a very compact interpreter that is managed with a few lines of code and that can also access the library. Despite the minimalist concept, the interpreter offers useful features such as automatic cleaning of data structures that are no longer used, thus freeing up memory.

In summary, these are the most important features of Lua:

  • Compact
  • Fast execution
  • Excellent expandability
  • Easy to learn
  • Available for free

How is programming with Lua structured and what is it used for?

The Lua programming language is used for general procedural programming . Therefore, within the programming paradigms, it is considered belonging to the languages scripting imperatives . It is implemented as a C library and offers functions such as Lua code and a sample host program (called lua), which provides a separate Lua interpreter..

As a scripting language , Lua does not have its own main program. It only works built into a host application , which can call the above-mentioned functions through Lua code. Thanks to the C function, Lua can be extended by numerous functions to cover the equally numerous programming requirements. This programming language can be adapted very easily, so that a common syntactic framework is guaranteed .

If you already have knowledge of Pascal, you will have no problem with the Lua syntax, because both programming languages ​​are very similar at this point: it is possible to separate several commands in a script line with a semicolon. Lua has a total of 20 defined keywords that are easy to learn. To give you an idea, Lua often uses function blocks that start with keywords like goto and end with words like end , elseif, or else . The local keyword identifies local variables that are only valid for the function block in which it appears. Function blocks can also be nested together .

Lua can work with the following data types: table, userdata, function, thread, nil, boolean, string, and number . From this list, only the first data type mentioned, table (table in Spanish), is structured. Variable data types are determined during script execution , but can be converted at any time if necessary.

In what areas is Lua used?

Due to the lack of limitations imposed on developers, the scope of applications for the Lua scripting language is very wide. Through user-defined modules, Lua can be extended virtually without end. The applications of programming with Lua include everything from web servers to video game development. In addition, Lua is ideal for programming your own application.

The Lua programming language is especially used in the video game industry and several use Lua as a scripting language . Lua is often used in game engines to keep game characters and settings separate, making the game engine more flexible and allowing different games to use the same engine. Some of the most popular examples are the multiplayer online role-playing game World of Warcraft, now a classic, or the hit puzzle game Enigma.

Another great area of ​​application for Lua is network and system programming. Lua is used to configure and automate programs. Some well known examples are VLC Media Player or Adobe Photoshop Lightroom. As for networks, it is network analyzers, such as Wireshark, that use the Lua functions. In addition, also MySQL Proxy, Redis NoSQL database and Apache and Nginx web servers rely on this scripting language.

Lua: a programming language with great strengths and small weaknesses

Conceptually, as a programming language, Lua offers few innovative features . Its syntax is pretty conventional, and with the 20 reserved keywords already mentioned, it's pretty simple too.

The typing in Lua is dynamic . The type of a variable is determined when a script is run , but, as mentioned, it can be cast to another type at any time. This is a great strength of Lua, especially compared to CSS or PHP, in which, for example, the assignment of any type to "Boolean" (that is, the logical values) does not follow a specific method.

The biggest advantage of Lua: all the texts or numbers to be adapted are written directly in variables. The assignment is done in a closed block at the beginning of the module. Subsequent functions only access variables. If the environment changes, only the block values ​​need to be adapted accordingly. This is the simplest programming method for initial creation.

However, Lua also has some disadvantages: if a function is modified due to an update, the source code must be recomposed in each project, which means that all the pages involved will need to be updated.

In summary

The Lua scripting language can be used to write standalone programs, but it can also serve as an embedded language. Lua may not be as popular as other programming languages, but this is certainly not due to a lack of programming tools.

Introduction to the Lua programming language (with code snippets)

Finally, we help you get an idea of ​​this scripting language with two simple examples, intended to give you an overview of how Lua works .

First example: line breaks in Lua

In Lua, line breaks are appended with? \? or? \ n? and a real line break:

  print ('1 : Hello\ World') print ("2: Hello\ World")  

In this case, the output looks like this:

  1: Hello World 2: Hello World  

Second example: variables in Lua

You do not have to declare variables in the Lua programming language, they are only created if necessary. As a reminder, the values ​​can be of the following types:? Nil? that is to say null (along with the value of the uncreated variables), numbers, literals (characters, letters, words, etc.), Booleans (true / false or true / false ), tables and functions. A simple coding example would be the following:

  print ( var ) var = "Hello World" print ( var )  

Since undefined variables result in null (? Nil?), The result is as follows:

  nil Hello World  

...