lua-users home
lua-l archive

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


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.
>