lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Monday 31 October 2005 16:27, J. Perkins wrote:
[...]
>   package.name = "MyPackage"
>
> ...where `package` hasn't been defined yet.

This is, AFAIK, equivalent to:

local t = _G["package"]    -- *read* from global
t["name"] = "MyPackage"    -- *write* to result

When I try your code fragment, it does what I would expect, which is to die 
because package is nil and can't be indexed.

If I do this:

setmetatable(_G, { __index = function(table, key)
    local t = rawget(table, key)
    if not t then
      t = {}
      rawset(table, key, t)
    end
    return t
  end })
package.name = "MyPackage"

...then that works (silently creating 'package' the first time it's accessed). 
Is that what you wanted?

-- 
+- David Given --McQ-+ 
|  dg@cowlark.com    | "Home is where, when you have to go there, they
| (dg@tao-group.com) | have to take you in." --- Cordelia Naismith
+- www.cowlark.com --+ 

Attachment: pgpEO0m5QOOfQ.pgp
Description: PGP signature