Cours LUA modules and packages

Extrait du cours LUA modules and packages

15-Modules and Packages
Usually, Lua does not set policies. Instead, Lua provides mechanisms that are powerful enough for groups of developers to implement the policies that best suit them. However, this approach does not work well for modules. One of the main goals of a module system is to allow different groups to share code. The lack of a common policy impedes this sharing.
Starting in version 5.1, Lua defines a set of policies for modules and packages (a package being a collection of modules). These policies do not demand any ex- tra facility from the language; programmers can implement them using what we have seen so far: tables, functions, metatables, and environments. However, two important functions ease the adoption of these policies: require, for using mod- ules, and module, for building modules. Programmers are free to re-implement these functions with different policies. Of course, alternative implementations may lead to programs that cannot use foreign modules and modules that cannot be used by foreign programs.
15.1 The require Function
Lua offers a high-level function to load modules, called require. This function tries to keep to a minimum its assumptions about what a module is. For require, a module is just any chunk of code that defines some values (such as functions or tables containing functions).
To load a module, we simply call require »modname ». Typically, this call returns a table comprising the module functions, and it also defines a global variable containing this table. However, these actions are done by the module, not by require, so some modules may choose to return other values or to have different side effects.
Listing 15.1. The require function:
function require (name)
if not package.loaded[name] then — module not loaded yet?
local loader = findloader(name)
if loader == nil then
error(« unable to load module  » .. name)
end
package.loaded[name] = true — mark module as loaded
local res = loader(name) — initialize module
if res ~= nil then
package.loaded[name] = res
end
end
return package.loaded[name]
end
15.2 The Basic Approach for Writing Modules
The simplest way to create a module in Lua is really simple: we create a table, put all functions we want to export inside it, and return this table. Listing 15.2 illustrates this approach. Note how we define inv as a private name simply by declaring it local to the chunk.
The use of tables for modules does not provide exactly the same functionality as provided by real modules. First, we must explicitly put the module name in every function definition. Second, a function that calls another function inside the same module must qualify the name of the called function. We can ameliorate these problems using a fixed local name for the module (M, for instance), and then assigning this local to the final name of the module.
Following this guideline, we would write our previous module like this:
local M = {}
complex = M — module name
M.i = {r=0, i=1}
function M.new (r, i) return {r=r, i=i} end
function M.add (c1, c2)
return M.new(c1.r + c2.r, c1.i + c2.i)
end
<as before>

……….

Si le lien ne fonctionne pas correctement, veuillez nous contacter (mentionner le lien dans votre message)
Cours LUA modules and packages (318 KO) (Cours PDF)
Cours LUA

Télécharger aussi :

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *