lua-users home
lua-l archive

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


OK, I see the problem, there was an extra dot in your match string. After removing it works in all but the redirected case, which is how my editor runs it when pressing the related F-key.

-- mod.lua
print 'always called'

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

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