Introduction to Lua, compiling and installing tutorials and variables, etc. _lua

Source: Internet
Author: User
Tags lua readline sin

Business useful to Lua, and Nginx combined to achieve high-performance Web applications, want to understand this knowledge, to understand the learning of the next Lua, welcome to the big click of the advice.

1. Lua Introduction

LUA is a simple programming language with the advantage of integrating C + + modules to extend its functionality, using hundreds of of lines of code or less to solve complex problems. The features are:

1.1. Scalability: Lua has been designed to be an extensible language from the outset, many features are implemented through external libraries, and can easily interact with other programming languages such as C, C + +, Java, and so on.
1.2. Simplicity: Lua is very concise, but powerful, easy to learn, suitable for small-scale applications.
1.3. High efficiency: LUA has a high level of execution efficiency.
1.4. Portability: Lua can run on any existing system.

The Lua script is a simple script that contains a series of LUA commands, and a text file with the. lua extension. A single command or a sequence of commands, which we call a code block in Lua.

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

2. LUA Installation

Set up the LUA environment first to facilitate later learning.

Copy Code code as follows:

# Curl-r-O http://www.lua.org/ftp/lua-5.2.3.tar.gz
# tar ZXVF lua-5.2.3.tar.gz
# CD LUA-5.2.3/SRC
# Make Linux

2.2 Common problem solving methods

Question 1:

Copy Code code as follows:

Lua.c:67:31:error:readline/readline.h:no such file or directory
Lua.c:68:30:error:readline/history.h:no such file or directory

Workaround:
Copy Code code as follows:

# yum Install Readline-devel

Question 2:
Copy Code code 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 LUA.O Liblua.a-lm-wl,-e-ldl-lreadline
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/.. /.. /.. /.. /lib64/libreadline.so:undefined reference to ' PC '
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/.. /.. /.. /.. /lib64/libreadline.so:undefined reference to ' Tgetflag '
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/.. /.. /.. /.. /lib64/libreadline.so:undefined reference to ' tgetent '
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/.. /.. /.. /.. /lib64/libreadline.so:undefined reference to ' up '
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/.. /.. /.. /.. /lib64/libreadline.so:undefined reference to ' tputs '
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/.. /.. /.. /.. /lib64/libreadline.so:undefined reference to ' Tgoto '
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/.. /.. /.. /.. /lib64/libreadline.so:undefined reference to ' Tgetnum '
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/.. /.. /.. /.. /lib64/libreadline.so:undefined reference to ' BC '
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/.. /.. /.. /.. /lib64/libreadline.so:undefined reference to ' Tgetstr '

Workaround:

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

Copy Code code as follows:

# vim./src/makefile
Linux:
$ (make) $ (all) syscflags= "-dlua_use_linux" syslibs= "-wl,-e-ldl-lreadline-lncurses"

3. LUA variables

In Lua, variables don't need to be declared, so you can introduce the variables you need anywhere. The tracking of variables becomes difficult. This requires us to be very careful in using them, lest different functions use the same name variables and cause data chaos.

Also, you do not need to specify the type of the variable, such as Nul, Boolean, string, number, table. The type of the variable depends on the value assigned to it. Accessing a variable without initialization is not an error, except that the result is a null value nil. You can use the type function to determine the types of variables. Such as:

Copy Code code as follows:

#./lua
> value = ' www.jb51.net '
> Print (Type (value))
String
> value = 39514058
>--jb51.net survival Time Q Group
> Print (Type (value))
Number

3.1 Global variables

The LUA variable defaults to global and remains unchanged throughout the session, unless it is changed. When using global variables, it is more explicit to add a G letter before the variable. However, try to use local variables.

3.2 Local Variables

Create a local variable using local, unlike a global variable, where the locals are valid only within the block of code that is declared. Defining a local variable can set an initial value for it, or it may not. Such as

Copy Code code as follows:
> Local value1
> Local vlaue2 = ' www.jb51.net '

4. Lua annotation Syntax

Single-line Comment: –
Multiline Comment: –[[--]]

5. LUA command line mode

Copy Code code as follows:

Usage:./lua [Options] [script [args]]
Available options are:
-e Stat execute string ' stat '//Direct incoming command to LUA
-I enter interactive mode after executing ' script '/enter interactive pattern
-l name require library ' name '//load a file
-V Show version information//print version information
-e Ignore Environment variables//ignoring environment variables
--Stop handling options
-Stop handling options and execute stdin
#./lua-e "Print" (Type (' www.jb51.net '))
String

Global variable arg holds the command line arguments for LUA.

Before running, LUA constructs the ARG table with all the parameters. The script name index is 0, and the script's parameters increase from 1. The parameters in front of the script begin to decrease from-1.

Copy Code code as follows:

> lua-e "sin=math.sin" script a B
ARG table is as follows:
Arg[-3] = "LUA"
Arg[-2] = "-E"
Arg[-1] = "Sin=math.sin"
Arg[0] = "Script"
ARG[1] = "a"
ARG[2] = "B"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.