lua-users home
lua-l archive

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


It was thus said that the Great Dirk Laurie once stated:
> This has come up before, but I can't remember or find the answer.
> 
> Is there a test that allows the following logic?
> 
> ----
> if test then -- module was invoked by 'require'
>   return module
> end
> -- continue with main program
> ----
> 
> The best I can come up with is to insist on 'lua -i' for the
> standalone, and use os.exit if I don't want to fall back to Lua.

  You mean like this?

	[spc]lucy:/tmp>lua mod.lua 
	I was executed as a program
	[spc]lucy:/tmp>lua
	Lua 5.3.4  Copyright (C) 1994-2017 Lua.org, PUC-Rio
	> m = require "mod"
	I was included as a module
	> 

Here's my "module":

	if ... then
	  print("I was included as a module")
	  return {}
	end
	
	print("I was executed as a program")

It also works for Lua 5.1 and 5.2.

  -spc