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:
> 
> I have settled on the following version as conforming to the docs, i.e. safest:
> 
> local modname, filename = ...
> if filename and filename:match(modname .."%.lua$") then return end
> 
> This code can be cut-and-pasted since it does not depend on the module
> name. It even works for submodules, e.g.
>    require "mod.submod"
> 
> The only caveat is that the module name must not contain a magic
> character except "."; in particular (I burnt my fingers on this one)
> not a hyphen.
> 

There is a problem with circular reasoning.

The goal is to test if mod.lua were required, BUT
the test ASSUMED that the call were from require, even 
study documentation of lua require, then test if it is being required.

If it is called like lua mod.lua arg1 arg2, your first line is already wrong.
You can always adjust arg1 arg2 to make above code pass (or fail)

For the same reason, package.loaded.mod should NOT 
be replaced by package.loaded[ ... ]

(It only work if ... were REALLY from require(...))