lua-users home
lua-l archive

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


Seems like a great solution, and it also feels the right Lua way to do it!!! Thanks Kevin.

I guess it may be simplified even further by not using the local variable ('requirename') at all. Or am I missing something?

So, in Lua 5.2.3 style,

--==========================================--
local m = {}

-- module functions here

if ... then
 return m        -- done with the library section of the file
end

-- test code or app specific code goes here
--==========================================--

I tried it and it works fine in every case (including redirection.) There are no dependencies on module name, and no 'mystic' coding that's hard to remember.

(Does anyone see any possibility of this method not working in all possible cases, e.g., certain corner cases it would fail?)

By the way, testing is only one of the possible usages. I (as many other I suppose) often write some very specific small scripts that do a single job, and I often write these as single-file standalone apps.

On occasion, however, it's nice to be able to borrow some -- otherwise rarely used -- function from those apps by treating the app itself as a library module.

Of course you could place the 'library' routines in a separate file and 'require' it from every app that needs it but for very small scripts this is not very flexible -- if for no other reason, just think of all the disk space you waste by creating an extra tiny file that simple calls the perhaps only function in the other file :)

Someone suggested to create a separate file for testing a module's function. In general, I find this a bad idea as the two files will soon get out of sync (talking from experience.) For one thing, you need some file naming convention just to keep track of things (which file tests which library). Also, renaming the module file, and forgetting to adjust (and possibly rename) the related test file is enough to break the special bond between the two. The best place to put module test code (in my view, at least) is the module itself.

Thank you all for your contributions.

From: Kevin Martin
Sent: Saturday, April 05, 2014 6:56 PM
To: tonyp@acm.org
Subject: Fwd: How to tell if running script directly?

Hi,

Sent this into list hours ago, but hasn't appeared yet? See my solution below

Thanks,
Kev


Begin forwarded message:


 From: Kevin Martin <kev82@khn.org.uk>

 Subject: Re: How to tell if running script directly?

 Date: 5 April 2014 12:39:30 BST

 To: Lua mailing list <lua-l@lists.lua.org>



 On 5 Apr 2014, at 12:32, <tonyp@acm.org> wrote:


   I think a better solution is needed.


 (This is 5.2, don't know 5.1 situation)

If some code is called through require, then the chunk has the name given to require as it's first argument, if executed, there are no arguments to the chunk.

 So, for testing, I do

 local requirename = …
 if requirename then
 return moduletable
 end

 --tests go here

 Thanks,
 Kev