[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Modules with standalone main program
- From: Sean Conner <sean@...>
- Date: Sat, 24 Feb 2018 03:50:26 -0500
It was thus said that the Great Dirk Laurie once stated:
> This has come up before, but I can't remember or find the answer.
> Is there a test that allows the following logic?
> if test then -- module was invoked by 'require'
> return module
> -- continue with main program
> The best I can come up with is to insist on 'lua -i' for the
> standalone, and use os.exit if I don't want to fall back to Lua.
You mean like this?
I was executed as a program
Lua 5.3.4 Copyright (C) 1994-2017 Lua.org, PUC-Rio
> m = require "mod"
I was included as a module
Here's my "module":
if ... then
print("I was included as a module")
print("I was executed as a program")
It also works for Lua 5.1 and 5.2.