lua-users home
lua-l archive

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


On Mon, Apr 14, 2014 at 12:06 PM, Enrique Garcia Cota <kikito@gmail.com> wrote:
>
>
>>
>> Looks pretty thorough, good job!  The only thing I saw that might be
>> worth mentioning is some modules monkey-patch globals, and should only
>> be run once -- but they export/expose nothing so they return boolean
>> 'true'.  You can return anything in a module of course -- the value
>> becomes referenced from package.loaded.module_name = <return value>.
>> If a module returns false or nil, I believe it will be loaded from the
>> file on every require().
>
>
>
> I didn't think that was worth mentioning explicitly. "Monkey-patch modules"
> are (or should be) infrequent - I suppose I could add them as an example in
> rule #6.
>
> That said, why should I mention that "they should be run once"? When I
> require a module twice, the second require will not "run" it again, afaik.
>
> Regards,
>
> Enrique ( kikito / @otikik )

If a module returns a boolean-false value, then require() will load
the script from the file it finds in each package.path/package.cpath's
and run what it compiles -- every time.  /Usually/ modules return a
table that contains functions it exposes/exports.  Tables are
boolean-true values, so require() doesn't recompile and re-run that
file, it just returns the table from the previous run. :>