lua-users home
lua-l archive

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

2011/10/19 Patrick Mc(avery <>:
> On 11-10-19 10:42 AM, Patrick Mc(avery wrote:
>> { snip}
>>> No need to change the language.  Formulate conventions for the
>>> directory structure of the library containing the modules, write a
>>> program (by all means call it `setup.lua`) that generates a module
>>> loader for each subtree, and the difference will be as insignificant
>>> as this:
>>> python:
>>> import library.module as mylib
>>> lua:
>>> mylib = (require "library").module
>>> Dirk
> That would be gold! Would there be a performance penalty? Could this also
> co-exist with the 5.2 mechanisms?

The 5.2 mechanisms are that there are no mechanisms :-)
Your module goes into a file, you keep everything local, and
return either a single function or a table of functions.  The "require"
function returns that value so you can assign it to a variable whose
name you choose.

Nothing stops your module file from putting stuff in the global
environment, but it defeats the whole point of the exercise.

> Sorry on second thought, is there a way to do something like this(and
> please keep in mind I have not touched Python for 3 years)

> file
> some foo function
> import * foo
> now I can call foo
It's either "import foo" or "from foo import *"

> That is, without having the use of functions in tables forced on me?

If `foo.lua` defines and returns one value (say a function) only, you
can just say

foo = require "foo"     -- works like Python's "import foo"

and call it immediately.  If it defines several things you want, you
need to put them into a table, yes.  I use a function `import`, so I
can say in that case:

dpl = require "dplutils"   -- this is my personal library
import = dpl.import
import (require "foo")   -- works like Python's "from foo import *"