SoFunction
Updated on 2025-04-08

Introduction to Lua, compilation and installation tutorial, and syntax introduction of variables, etc.

The company's business is useful for Lua, which combines it with nginx to realize high-performance web applications. If you want to understand this knowledge, you will learn about Lua. Welcome to Daka and others to give me advice.

1. Introduction to Lua

Lua is a simple programming language, and its advantage is that it can integrate C++ modules to extend its own functionality, using hundreds of lines of code or less to solve complex problems. Features are:

1.1. Scalability: Lua has been designed as an easy-to-extend language from the beginning. Many functions are implemented through external libraries and are easy to interact with other programming languages ​​such as C, C++, Java, etc.
1.2. Concise: Lua is very simple, but powerful, easy to learn, and is very suitable for small-scale applications.
1.3. Efficiency: Lua has high execution efficiency.
1.4. Portability: Lua can run on any existing system.

A Lua script is a simple script containing a series of Lua commands with a text file with an extension of .lua. A script file composed of a single command or a series of commands, in Lua we call it a code block.

Code block: refers to a control structure, a function body, or a chunk (the file or text string in which the variable is declared).

2. Lua installation

First build the Lua environment for easier learning and demonstration.

Copy the codeThe code is as follows:

# curl -R -O /ftp/lua-5.2.
# tar zxvf lua-5.2.
# cd lua-5.2.3/src
# make linux

2.2 Solutions to FAQs

Question 1:

Copy the codeThe code is as follows:

:67:31: error: readline/: No such file or directory
:68:30: error: readline/: No such file or directory

Solution:
Copy the codeThe code is as follows:

# yum install readline-devel

Question 2:
Copy the codeThe code is as follows:

make all SYSCFLAGS=”-DLUA_USE_LINUX” SYSLIBS=”-Wl,-E -ldl -lreadline”
make[1]: Entering directory `/root/lua-5.2.3/src'
gcc -o lua -lm -Wl,-E -ldl -lreadline
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/: undefined reference to `PC'
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/: undefined reference to `tgetflag'
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/: undefined reference to `tgetent'
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/: undefined reference to `UP'
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/: undefined reference to `tputs'
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/: undefined reference to `tgoto'
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/: undefined reference to `tgetnum'
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/: undefined reference to `BC'
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/: undefined reference to `tgetstr'

Solution:

Since there is no link to the ncurses library, you need to add -lncurses.

Copy the codeThe code is as follows:

# vim ./src/Makefile
linux:
 $(MAKE) $(ALL) SYSCFLAGS="-DLUA_USE_LINUX" SYSLIBS="-Wl,-E -ldl -lreadline -lncurses"

3. Lua variable

In Lua, variables can be used without declaration, so you can introduce the required variables anywhere. Tracking variables becomes difficult. This requires us to be extra careful when using it, so as not to use variables with the same name in different functions and cause data confusion.

At the same time, it is not necessary to specify the types of variables, such as nul, boolean, string, number, and table. The type of a variable depends on the value assigned to it. There will be no error when accessing a variable that is not initialized, but the result is a null value nil. You can use the type function to determine the type of a variable. like:

Copy the codeThe code is as follows:

# ./lua
> value = ''
> print (type(value))
string
> value = 39514058
> -- Operation and maintenance survival time Q group
> print (type(value))
number

3.1 Global variables

The Lua variable is global by default and will remain unchanged throughout the session unless it is changed. When using global variables, adding a g letter before the variable will be more clear. However, try to use local variables.

3.2 Local variables

Create a local variable using local. Unlike global variables, local variables are only valid within the declared code block. Defining a local variable can set an initial value for it or not. like

Copy the codeThe code is as follows:
> local value1
> local vlaue2 = ‘'

4. Lua comment syntax

Single line comments: –
Multi-line comments: –[[ --]]

5. Lua command line method

Copy the codeThe code is as follows:

usage: ./lua [options] [script [args]]
Available options are:
-e stat  execute string 'stat'  //Turn directly to Lua
-i        enter interactive mode after executing 'script' //Enter interactive mode
-l name requir library 'name'  //Load a file
-v       show version information //Print version information
-E       ignore environment variables //Ignore environment variables
  --       stop handling options
  -        stop handling options and execute stdin
# ./lua -e "print(type(''))"
string

The global variable arg stores Lua's command line parameters.

Before running, Lua constructs the arg table with all parameters. The script name index is 0, and the script parameters are incremented from 1. The parameters in front of the script start from -1.

Copy the codeThe code is as follows:

> lua -e "sin=" script a b
The arg table is as follows:
arg[-3] = "lua"
arg[-2] = "-e"
arg[-1] = "sin="
arg[0] = "script"
arg[1] = "a"
arg[2] = "b"