User:OrenBochman/Intoduction To Lua

From Meta, a Wikimedia project coordination wiki

In this unit we Introduce the first Lua Module.


Introducing Lua[edit]

Lua is a relatively new scripting language. It has been used successfully in a number of situations. It has similar capabilities to Javascript but avoids most of it shortcomings. Lua is a small programming language. Its syntax resembles other languages.

The first lessons is a quick introduction aimed at absolute beginners.

Scribunto[edit]

  • Scribunto is the extension which when added to MediaWiki introduces support for scripting in Lua.
  • The official WMF test machine is at scribunto with the latest Lua integration support.
  • To work through this tutorial you will need to Create an account and Log into it.

Module Name Space[edit]

Lua modules are created in their own name space - the module name space. This allows the MediaWiki software to treat them different from other articles, talk pages and template.

Creating Hello World Lua[edit]

To create your first Lua module access scribunto and create a page called:

  • [[Module:Hello]].

Then add to it the following Lua code:

   local p = {}

   function p.hello()
      return 'Hello, world!'
   end

   return p

explanation:


Using Hello World Lua[edit]

To invoke hello world Lua use:

 {{#invoke: Hello | hello }}

and the result should look like:

Hello, world!

Readable Lua Code[edit]

You can document you code using short one line comments or longer multiline comments

   -- This is defines p as a local ?? definition
   local p = {}

   --[[ 
      And This is a function definition.
      It uses p already defined
   --]]
   function p.hello()
      return 'Hello, world!'
   end

   return p

By adding comments and indenting the code with spaces you will make your code not only readable to others but also readable for yourself when you have to change it later.


See Also[edit]