Chinaunix首页 | 论坛 | 博客
  • 博客访问: 511074
  • 博文数量: 130
  • 博客积分: 10060
  • 博客等级: 上将
  • 技术积分: 1720
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-21 12:35
文章分类

全部博文(130)

文章存档

2011年(2)

2010年(9)

2009年(41)

2008年(78)

我的朋友

分类:

2009-01-04 22:56:52

15.4 The module Function
     函数module



Probably you noticed the repetitions of code in our previous examples. All of them started with this same pattern:
你或许注意到了我们以前例子中的代码重复。它们都开始于该同样的模式:

      local modname = ...
      local M = {}
      _G[modname] = M
      package.loaded[modname] = M
        
      setfenv(1, M)


----------------------- Page 9-----------------------145


Lua 5.1 provides a new function, called module, that packs this functionality. Instead of this previous setup code, we can start a module simply like this:
Lua 5.1提供一个新函数,叫做module,包装了这个功能。不用以前的配置代码,我们可像这样开始一个模块:

      module(...)

This call creates a new table, assigns it to the appropriate global variable and to the loaded table, and then sets the table as the environment of the main chunk.
该调用创建一个新表,把它赋给合适的全局变量和loaded表,然后设置该表作为主程序块的环境。

    By default, module does not provide external access: before calling it, you must declare appropriate local variables with the external functions or modules you want to access. You can also use inheritance for external access adding the option package.seeall to the call to module. This option does the equivalent of the following code:
    module缺省不提供外部访问:在调用它之前,你必须为你要访问的外部函数或模块声明合适的局部变量。你也能为外部访问使用继承,(方法是)把选项package.seeall加入module的调用。该选项执行与下面的代码等价:

       setmetatable(M, {__index = _G})

Therefore, simply adding the statement
因此,简单地在文件开头加入语句

      module(..., package.seeall)

in the beginning of a file turns it into a module; you can write everything else like regular Lua code. You need to qualify neither module names nor external names. You do not need to write the module name (actually, you do not even need to know the module name). You do not need to worry about returning the module table. All you have to do is to add that single statement.
使它变成模块;你能编写类似正常Lua代码的其他任何东西。你不需要限定模块名或外部名。你不需要些模块名(实际上你甚至不需要知道模块名)。你不需要关心返回模块表。你所要做的只是加入那条单独的语句。

    The module function provides some extra facilities. Most modules do not need these facilities, but some distributions need some special treatment (e.g., to create a module that contains both C functions and Lua functions). Before creating the module table, module checks whether package.loaded already contains a table for this module, or whether a variable with the given name already exists. If it finds a table in one of these places, module reuses this table for the module; this means we can use module for reopening a module already created. If the module does not exist yet, then module creates the module table. After that, it populates the table with some predefined variables: _M contains the module table itself (it is an equivalent of _G); _NAME contains the module name (the first argument passed to module); and _PACKAGE contains the package name (the name without the last component; see next section).
    module函数提供一些额外的功能。多数模块不需要这些功能,但是一些发行包需要特殊的处理(举例来说,创建包含C函数和Lua函数的模块)。在创建模块表以前,module检查package.loaded是否已经包含了该模块的表,或是否已经存在给定名字的变量。如果在其中一个地方找到了表,module重用该表作为模块;这表明我们可以用module重新开始已经创建的模块。如果模块还不存在,则module就创建模块表。在那之后,它用一些预定义的变量组装表:_M包含模块表自身(它与_G等效);_NAME包含模块名(传入module的第一参数);以及_PACKAGE包含包名(不含最后组件的名字;见下一节)。
阅读(611) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~