[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: t._NAME etc.
- From: "Jerome Vuarand" <jerome.vuarand@...>
- Date: Mon, 26 Mar 2007 11:15:26 -0400
Thomas Lauer wrote:
> It seems what I need is a general way to get the printable name of a
> table (assuming that a module resides, one way or another, in a
> table).
>
> A quick look at the debug library has not shown me an easy way to do
> that so I'll have to try something homegrown. Or has someone an idea
> as to how to do this?
In Lua values are anonymous. Sometimes there are way to get a usable
name for a value (like function names for stack tracebacks), but it not
always possible.
However if you have a table and you know it's a module loaded with
require, you can get its name from package.loaded. Here is an example to
do so:
function getmodulename(module)
for k,v in pairs(package.loaded) do
if v==module then return k end
end
return nil, "no module found"
end
local foo = require 'some.module'
local fooname = assert(getmodulename(foo))
print(fooname)
> some.module