In python, one would usually define a main function, in order to allow the script to be used as module (if needed):
def main():
print("Hello world")
return 0
if __name__ == "__main__":
sys.exit(main())
In Lua, the idiom if __name__ == "__main__" isn’t possible as such (that means, I don’t think it is).
That’s what I’m usually doing in order to have a similar behaviour in Lua:
os.exit((function(args)
print("Hello world")
return 0
end)(arg))
… But this approach seems rather “heavy on parentheses” 🙂
Is there a more common approach (besides defining a global main function, which seems redundant)?
There’s no “proper” way to do this, since Lua doesn’t really distinguish code by where it came from, they are all just functions. That said, this at least seems to work in Lua 5.1:
It works by checking whether the stack depth is greater than 3 (the normal depth for a file in the stock Lua interpreter). This test may break between Lua versions though, and even in any embedded/custom Lua builds.
I’ll also include this (slightly more portable) alternative, although it’s taking an even greater leap in heuristics, and has a failure case (see below):
This one works by comparing the contents of _G.arg with the contents of ‘…’. In the main chunk they will always be the same. In a module _G.arg will still contain the command-line arguments, but ‘…’ will contain the module name passed to require(). I suspect this is closer to the better solution for you, given that you know your module name. The bug in this code lies when the user executes the main script with 1 argument, and this is the exact name of your module:
Given the above I hope at least you know where you stand, even if it isn’t exactly what you had in mind 🙂
Update: For a version of hybrid.lua that works in Lua 5.1 and 5.2, you can replace getfenv with debug.getlocal: