[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Modules with standalone main program
- From: Albert Chan <albertmcchan@...>
- Date: Sat, 24 Feb 2018 12:55:33 -0500
> On Feb 24, 2018, at 10:31 AM, Dirk Laurie <email@example.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(...))