lua-users home
lua-l archive

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




On 2018-02-24 10:28 AM, albertmcchan wrote:

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


Why does nobody support renaming modules, it's 2018 :(

local function compareargs(...)
    local vargs = table.pack(...)
    for i=1,vargs.n do
        if arg[i] ~= vargs[i] then return false end
    end
    return true
end
if package.loaded[...] and not compareargs(...) then -- be nice to ppl
    -- (probably? with about a 99.999% chance) loaded from require
else
    -- do whatever you want just don't screw with the [...] above >.>
end

--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.