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.

The Luac compiler

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.

Conclusion

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.

Lua学习系列(四)的更多相关文章

  1. scrapy爬虫学习系列四:portia的学习入门

    系列文章列表: scrapy爬虫学习系列一:scrapy爬虫环境的准备:      http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_python_00 ...

  2. DocX开源WORD操作组件的学习系列四

    DocX学习系列 DocX开源WORD操作组件的学习系列一 : http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_sharp_001_docx1.htm ...

  3. .net reactor 学习系列(四)---.net reactor应用场景

    原文:.net reactor 学习系列(四)---.net reactor应用场景         前面已经学习了.net reactor一些基础知识,现在准备学习下实际的应用场景,只是简单的保护和 ...

  4. Identity Server4学习系列四之用户名密码获得访问令牌

    1.简介 Identity Server4支持用户名密码模式,允许调用客户端使用用户名密码来获得访问Api资源(遵循Auth 2.0协议)的Access Token,MS可能考虑兼容老的系统,实现了这 ...

  5. MVC3+EF4.1学习系列(四)----- ORM关系的处理

    上篇文章 终于把基础的一些操作写完了 但是这些都是单表的处理 而EF做为一个ORM框架  就必须点说说对于关系的处理 处理好关系 才能灵活的运用EF 关于关系的处理 一般就是  一对一   一对多  ...

  6. Lua学习系列(二)

    资源整理: 风云老师博客: http://blog.codingnow.com/eo/luaoeeeaeau/ 知乎: https://www.zhihu.com/question/20736660 ...

  7. Lua学习系列(一)

    从现在开始,打算学习一门新的脚本语言-lua. 1.什么是lua? a) lua1 • Lua 1.0 was implemented as a library, in less then 6000 ...

  8. Vue学习系列(四)——理解生命周期和钩子

    前言 在上一篇中,我们对平时进行vue开发中遇到的常用指令进行归类说明讲解,大概已经学会了怎么去实现数据绑定,以及实现动态的实现数据展示功能,运用指令,可以更好更快的进行开发.而在这一篇中,我们将通过 ...

  9. WCF学习系列四--【WCF Interview Questions – Part 4 翻译系列】

    WCF Interview Questions – Part 4   This WCF service tutorial is part-4 in series of WCF Interview Qu ...

随机推荐

  1. spring securiy使用总结

    我们常见的几个功能: 注册后直接登录,并且remember-me这种在网上找到很多注册后登录的,但是remember-me没有.其实解决方案还是看源码比较方便.a. 装载authenticationM ...

  2. web开发在线调试

    来源: http://www.cnblogs.com/itech/archive/2012/09/23/2698754.html 通常我们开发web时候,使用ie的developertoolgs,或c ...

  3. php 数组操作符

    1.数组操作符 数组运算符 例子 名称 结果 $a + $b 联合 $a 和 $b 的联合. $a == $b 相等 如果 $a 和 $b 具有相同的键/值对则为 TRUE. $a === $b 全等 ...

  4. ios屏幕

    设备 屏幕尺寸 分辨率(pt) Reader 分辨率(px) 渲染后 PPI iPhone 3GS 3.5吋 320x480 @1x 320x480   163 iPhone 4/4s 3.5吋 32 ...

  5. oracle一次删除多张表

    通过拼接sql语句来完成 例如有如下个表 想一次性删除,执行如下语句: select 'drop table '||table_name ||';' as dropsql from USER_TABL ...

  6. 大学二三事——那些人(1)

    校歌墙的对面是一座历史比较悠久的建筑,以前叫做12号楼,后来改成了"诚"字楼. 在诚字楼一楼昏暗的走廊上,你总是能看见一位大概四五十岁的大叔,有时他会指着挂在墙上的学校简介,一个人 ...

  7. 天天果园,中粮我买网等生鲜APP竞品分析

    奈何对生鲜行业的品类,价格,供应链不熟悉,想先从APP开始来了解生鲜行业和各个生鲜企业,若有不足之处,还望海量,也请帮忙指正. 选取了以下竞品:天天果园,易果生鲜,一米鲜,中粮我买网,爱鲜蜂,每日优鲜 ...

  8. overload与override

    一.override(重写.覆写) 1.子类重写父类的方法(两同一小一大一权限) ① 方法签名必须相同,返回值类型必须相同. ② 抛出的异常必须小于等于父类方法 ③ 权限修饰符必须大于等于父类方法的权 ...

  9. 关于在jsp中的表达式

    列子: <%List<F_dd_tourist_info_markup> tourists = (List<F_dd_tourist_info_markup>) requ ...

  10. java 数据结构 队列的实现

    java 数据结构队列的代码实现,可以简单的进行入队列和出队列的操作 /** * java数据结构之队列的实现 * 2016/4/27 **/ package cn.Link; import java ...