[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Is there a similar way to do if __name__ == '__main__' as in python ?
- From: Rici Lake <lua@...>
- Date: Thu, 8 Feb 2007 01:52:00 -0500
On 8-Feb-07, at 1:38 AM, gary ng wrote:
--- Rici Lake <lua@ricilake.net> wrote:
Yes, that's one of the reasons I don't use module().
The actual code in my original message avoids
hard-coding
the module name with the line:
local myname = ...
at the beginning. That picks up the first argument
supplied
when the module chunk is called; if it is called by
require(),
Ah, I thought module() is now the recommended way.
It is, so take anything I say with a grain of salt.
But anyway, out of curiosity, what would be the usage
scenario for not hardcode the module name ? As my
limited usage of require() is just to load another
file which is hardcoded in the file system anyway ?
Well, for example, I might want to have two different
versions of a module, say mymodule_v4 and mymodule_v5
both kicking around at the same time.
I can do that by saying:
local mymodule = require"mymodule_v4"
in any code which wants to use mymodule_v4; there's no global
pollution and the contents of the file mymodule_v4.lua don't
need to know that I've renamed the file.
Of course, most of my code will have:
local mymodule = require"mymodule"
in it, so I have to make that work, too. I can't use a symlink
because the module system identifies modules names in the package
cache with the filename; however, I can create a file mymodule.lua
with the single line:
return require"mymodule_v5"
and everything will work nicely.
I could also use a catchall loader which tried to figure
that out for me, perhaps by doing a file listing.