lua-users home
lua-l archive

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


Am 05.04.2014 15:38 schröbte steve donovan:
Using the debug library could tell you exactly the current name of the
script, so that

if debug.getinfo(1,'S').short_src == 'mod.lua' then
---
end

should work - (actually tested it this time ;))

That's rather verbose, but could be wrapped up as a requireable module

if require 'this_mod' 'mod.lua' then

end

where this_mod.lua returns a suitable function.

I scan the call stack and look for the `require` function (see e.g. here[1]). Seems to work fine so far (even when `arg` is nil[2]) ...


Since it _can_ be done with Lua, and a robust solution can be made
into a module, such a feature is unlikely to ever get baked into the
language.  Lua provides mechanism, not policy...


Since I only needed it once (or twice) so far, I haven't bothered to put it in a library.

Philipp


[1]: https://github.com/siffiejoe/lua-amalg/blob/master/src/amalg.lua#L230
  [2]: which can happen if called via `lua -l mod` or `lua < mod.lua`