lua-users home
lua-l archive

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


> On Feb 24, 2018, at 10:31 AM, Dirk Laurie <dirk.laurie@gmail.com> wrote:
> 
> On entry to mod.lua, package.loaded.mod must be nil if the package is
> being required. The file would not be loaded otherwise.

That is true. However, once require "activated", it set 
package.loaded.mod to something that is not nil. 
--> it is marked loaded even if it is not completely loaded

You can try add print(package.loaded.mod) inside mod.lua
If you run it as mod = require "mod",  it print out "userdata"

After the require call, package.loaded.mod is replace by mod.lua 
returns.  Even if mod.lua returning nothing, package.loaded.mod 
will be set to true (to signal require not to loaded it again)

That is why below code work:

if not package.loaded.mod then 
  --  not required, run it standalone
end