lua-users home
lua-l archive

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


Thanks, but it doesn't seem to work (for me at least). Did you try it under Win?

Also, you can run a script many different ways, so such methods have to be more elaborate to catch all possible cases, or they fail.

For example, you can run this example you gave as:

1. lua mod.lua
2. mod.lua (having defined 'assoc'iated .lua to run it as Lua script) 3. mod (having made .lua a valid executable extension in PATHEXT which then calls Lua behind the scenes)
4. lua < mod.lua
...

-----Original Message----- From: steve donovan
Sent: Saturday, April 05, 2014 1:55 PM
To: Lua mailing list
Subject: Re: How to tell if running script directly?

On Sat, Apr 5, 2014 at 12:16 PM,  <tonyp@acm.org> wrote:
where _MAIN would be true if the script is run directly, and false if it is
part of require or dofile.

It's easy to detect if a module has been launched directly. arg[0] has
the name of the script, so

-- mod.lua
print 'always called'

if arg[0]:match 'mod.%.lua$' then
   print 'called directly!'
end

does what's expected