detect if the current file is the main file ?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

detect if the current file is the main file ?

Mildred Ki'Lya
Hi,

I know in python it is possible to detect if a file is used as a module
or used as the main programm. And if it is run as the main programm run
a main function.
Is it possible to do the same with lua ? I tried with some
debug functions without success.

Thanks

Mildred

--
Mildred       <xmpp:[hidden email]> <http://mildred632.free.fr/>
Clef GPG :    <hkp://pgp.mit.edu> ou <http://mildred632.free.fr/gpg_key>
Fingerprint : 197C A7E6 645B 4299 6D37 684B 6F9D A8D6 [9A7D 2E2B]
Reply | Threaded
Open this post in threaded view
|

Re: detect if the current file is the main file ?

Niklas Frykholm-2

>Hi,
>
>I know in python it is possible to detect if a file is used as a module
>or used as the main programm. And if it is run as the main programm run
>a main function.
>Is it possible to do the same with lua ? I tried with some
>debug functions without success.
>  
>
I've been using this (suppose the file is called "test.lua"):

if arg[0]:find("test%.lua$") then
  main()
end

It's not foolproof. The end user could have renamed the file "test.lua".
Or he could be running some other program that ends in "test.lua".

// Niklas
Reply | Threaded
Open this post in threaded view
|

Re: detect if the current file is the main file ?

Jamie Webb-3
In reply to this post by Mildred Ki'Lya
On Sun, May 14, 2006 at 09:36:26PM +0200, Mildred wrote:
> Hi,
>
> I know in python it is possible to detect if a file is used as a module
> or used as the main programm. And if it is run as the main programm run
> a main function.
> Is it possible to do the same with lua ? I tried with some
> debug functions without success.

Check the stack height at the top of your file and set a flag. When
using the 'lua' binary, you're at the top level iff debug.getinfo(3)
== nil.

-- Jamie Webb