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:
> = "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)
    return t
  end }) = "MyPackage"

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

+- David Given --McQ-+ 
|    | "Home is where, when you have to go there, they
| ( | have to take you in." --- Cordelia Naismith
+- --+ 

Attachment: pgpMQDsXxdXOJ.pgp
Description: PGP signature