lua-users home
lua-l archive

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


2013/4/2 Gaspard Bucher <gaspard@teti.ch>:
> Hi there,
>
> I am preparing a first release of some core parts of lubyk and cannot decide
> what is expected of "require" concerning global environment:
>
> ========= A (alter global)
> local foo = require 'lub'
> print(foo, lub)
> --> table, table
>
> ========= B (do not touch global)
> local foo = require 'lub'
> print(foo, lub)
> --> table, nil
>
> ========= C (do not touch global)
> require 'lub'
> print(lub)
> --> nil
>
> B and C are the same (just two common ways of using "require"). I feel that
> not touching global creates unexpected behavior in "C".
>
> I feel "A" is the best solution but would like some community feedback on
> this...

Lua 5.1 says A, Lua 5.2 says B and C.

You can always in case C say "local foo = require 'lub'" later.
The package will not be re-loaded, it will in effect just do
'local foo = package.loaded.lub'.

This behaviour should not be unexpected, it is documented.
Read 'require' in the Lua 5.2 reference manual.