[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Equivalent to __name__ == “_main__”?
- From: Patrick Kurth <p.kurth@...>
- Date: Tue, 07 Dec 2021 07:01:55 +0000
> Excuse my ignorance, but is there a “official” (read:
> preferred/reliable) way to detect that you have run a module
> directly as a program instead of loading it with require()?
>
> Like the “name == main” trick in Python?
>
> I have seen suggestions to use debug.getinfo() to probe the call
> stack depth, and testing whether the stack is too shallow to be
> inside a call to require(). Does that work? Is there a better way?
>
> Thanks.
I've been using lua now for some years and I never had the need for
this. Could you elaborate on why you want to use this? It should be
possible to put your code in a module which is always called by
require(). If you also want to have a standalone version of this
module, just use a main file where you require this module and execute
the needed code.
Kind regards,
Patrick