lua-users home
lua-l archive

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



On Feb 24, 2018, at 7:46 AM, Dirk Laurie <dirk.laurie@gmail.com> wrote:

> Actually, the manual documents what 'require' does, in particular:
> 
> "Once a loader is found, require calls the loader with two arguments:
> modname and an extra value dependent on how it got the loader. (If the
> loader came from a file, this extra value is the file name.)"
> 
> And dofile does not do that.
> 
> Thanks.
> 
> -- Dirk
> 

does require cache the result, and set package.loaded.mod ?

If mod were "required", BEFORE mod.lua return results to set 
package.loaded.mod, require function set it to something not nil

AFTER the require call, package.loaded.mod is set to whatever
mod.lua returns (or true if mod return nothing)

--> with require(), package.loaded.mod always tested not nil

if package.loaded.mod then
  -- loaded from require
else
  -- run as a program
end