• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Lua学习系列(四)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

lua 资源: http://www.dcc.ufrj.br/~fabiom/lua/

 

 

第一个Lua程序

http://www.dcc.ufrj.br/~fabiom/lua/

原文:https://www.maketecheasier.com/writing-lua-program-linux/

 

There are a multitude of programming languages out there but if you are looking to learn a new language, one that is easy to grasp, fast and open source is Lua. From the Portuguese word for moon, the Lua language is found in some unexpected places. It is used in Adobe’s Photoshop Lightroom and in games like World ofWarcraft and Angry Birds. In fact Lua is currently the leading scripting language for games. It is also the language used by Corona, a free software development kit that lets you write apps for smartphones and tablets running iOS or Android.

 

Installing Lua is simple. On Ubuntu you can use the Software Center or if you prefer the command line use:

sudo apt-get install lua5.1

Once installed, you have access to two tools, lua which is the Lua language interpreter and luac which is the Lua compiler. Programming in Lua is very easy to learn. Using a text editor, create a file called hellomte.luawith the following line:

print ("Hello Make Tech Easier!")

Save the file and then from the command prompt, go to the directory where you saved the file and run the Lua program like this:

lua hellomte.lua

The output, as I hope you expected, was the text Hello Make Tech Easier!. Congratulations you have written your first Lua program!

You can also run Lua as a stand-alone interpreter like you would for bash or python. This means you can write scripts which act like stand-alone executables. Create a file called looknohands without the .lua extension. In the file add:

#!/usr/bin/env lua
print ("Look no hands!")

The first line tells Linux that this is a script file and the script uses lua. The second line prints out the text “Look no hands!” Before the script can be run, it needs to be given execute permission. To do this run the “chmod” command in the directory with the file in it:

chmod +x looknohands

This tells Linux that this script can be executed, to run it just type:

./looknohands

And you will see the text.

 

If you have any programming experience, you may be expecting that the Lua compiler generates a binary executable that can be run directly on the host, much like a C compiler would. However the Lua compiler is slightly different. Rather than executable code, it produces binary files that can be later loaded and executed within the Lua interpreter. The main advantages of pre-compiling Lua code is that it loads faster and also it protects the source code from being tampered with, either accidentally or intentionally.

Here is a simple Lua program that loops around 10 times printing some text. Create a file calledhellomte10.lua and save it with the following lines of code:

for i=1,10,1 do
print ("Hello Make Tech Easier: ", i)
end

This can be run using the Lua command:

lua hellomte10.lua

However it can also be compiled into Lua binary code like this:

luac -o hellomte10.luac hellomte10.lua

This will create a binary file called hellomte10.luac which can be run just like a normal .lua file:

lua hellomte10.luac

It can also be used from within the stand-alone interpreter. Create a file called hellomte10 without the .lua extension:

#!/usr/bin/env lua
dofile("hellomte10.luac")
 

The dofile() function will load the binary file and execute it. To run the hellomte10  program grant it execute permission using the chmod command and then run it:

./hellomte10

To distribute pre-compiled Lua programs you need to ship the .luac file along with the stand-alone interpreter script file (i.e. hellomte10.luac and hellomte10), but you don’t need to supply the original .lua file.

Lua is a very flexible language which, as we have seen, can be used in a variety of different ways. Try reading the Programming in Lua book to see what else Lua can do.


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Lua脚本热更新方案发布时间:2022-07-22
下一篇:
ESP8266使用详解--基于Lua脚本语言发布时间:2022-07-22
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap