[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Modules with standalone main program
- From: Paul K <paul@...>
- Date: Sat, 24 Feb 2018 15:03:10 -0800
Hi Dirk,
> I.e. "doflile" is treated just like "require'.
> I want "require" to return and all other ways of running the file to continue.
It wasn't clear from your question that you want to distinguish
between require and dofile calls, but it shouldn't be too difficult to
add:
if pcall(debug.getlocal, 4, 1) then
if select('#', ...) == 0 then
print("dofile package")
else
print("require package")
end
else
print("main script")
end
Paul.
On Sat, Feb 24, 2018 at 12:37 PM, Dirk Laurie <dirk.laurie@gmail.com> wrote:
> 2018-02-24 21:46 GMT+02:00 Paul K <paul@zerobrane.com>:
>>> Unfortunately, the conclusion was that there seems to be no fail-proof method.
>
> I don't need a fail-proof method. I need a method that works for a
> one-file module of which I know the name by which it will be required.
>
>> I already suggested a method earlier that works with Lua 5.1+ and
>> handles command line parameters as well as require/dofile calls:
>>
>> if pcall(debug.getlocal, 4, 1) then
>> print("package")
>> else
>> print("main script")
>> end
>
> It does not do what I want.
>
> $ lua
> Lua 5.3.4 Copyright (C) 1994-2017 Lua.org, PUC-Rio
>> dofile"lmodules/mod.lua"
> package
>
> I.e. "doflile" is treated just like "require'.
>
> I want "require" to return and all other ways of running the file to continue.
>