在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
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, 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 #!/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.
The Luac compilerIf 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 called 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 lua hellomte10.luac It can also be used from within the stand-alone interpreter. Create a file called #!/usr/bin/env lua dofile("hellomte10.luac") The ./hellomte10 To distribute pre-compiled Lua programs you need to ship the ConclusionLua 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. |
请发表评论