lua-users home
lua-l archive

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


And what happens if you rename the module? It stops working (unless you edit)...

I think a better solution is needed. It should be fairly simple to have (for example) the symbol _MAIN defined as local variable when starting to process any script (regardless of input source). Since this would not be defined in 'included' modules, and being local to the first script that started running, it would do the job, don't you think?

Tony

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