lua-users home
lua-l archive

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


2014-04-14 18:23 GMT+02:00 Coroutines <coroutines@gmail.com>:
> On Mon, Apr 14, 2014 at 9:19 AM, Dirk Laurie <dirk.laurie@gmail.com> wrote:
>> 2014-04-14 18:03 GMT+02:00 Coroutines <coroutines@gmail.com>:
>>> On Mon, Apr 14, 2014 at 8:55 AM, Dirk Laurie <dirk.laurie@gmail.com> wrote:
>>>> 2014-04-14 17:29 GMT+02:00 Coroutines <coroutines@gmail.com>:
>>>>
>>>>> If a module returns false or nil, I believe it will be loaded from the
>>>>> file on every require().
>>>>
>>>> Not quite. The go/nogo test is whether there is an entry for the module
>>>> in package.loaded.
>>>>
>>>> $ lua
>>>> Lua 5.2.3  Copyright (C) 1994-2013 Lua.org, PUC-Rio
>>>>> package.loaded.lpeg = "bazinga"
>>>>> lpeg = require"lpeg"
>>>>> =lpeg
>>>> bazinga
>>>>
>>>
>>> I thought it checked package.loaded.module_name for a boolean-true
>>> value and returned that, otherwise it loaded the module (if it exists)
>>> from the search paths, no?  If you return nil/false it'll be loaded
>>> from the file every time?
>>>
>>
>> The test is simply on the existence of the key in the table.
>> Whether it was put there by `require` is immaterial. If the
>> next `require` finds no key, it reloads, otherwise not.
>>
>>> package.loaded.lpeg = "false"
>>> lpeg = require"lpeg"
>>> return lpeg
>> false
>>> package.loaded.lpeg = nil
>>> lpeg = require"lpeg"
>>> return lpeg
>> table: 0x91c6de0
>>
>
> Okay, so it's simply a:
>
> if package.loaded[modname] == nil then
>     package.loaded[modname] = load_from_paths(modname)
> end
>
> return package.loaded[modname]

Bazinga! My so-called demonstration had:

    package.loaded.lpeg = "false"

which beautifully prints the same as if I had coded

    package.loaded.lpeg = false

Actually, you were right.

Have I just maybe been watching too many old BBT episodes lately?