lua-users home
lua-l archive

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


2018-02-26 22:46 GMT+02:00 albertmcchan <albertmcchan@yahoo.com>:
> On Feb 26, 2018, at 3:06 PM, Roberto Ierusalimschy <roberto@inf.puc-rio.br> wrote:
>
>>>
>>> if __name__ == '__main__':
>>>   print "running code standalone"
>>>
>>> my lua equivalent:
>>>
>>> if not package.loading "mod" then
>>>   print "running code standalone"
>>> end
>>
>> This looks like a fail to me:
>>
>>  package.loading = function () return true end; dofile("mod.lua")
>
> you are not playing fair :-(
>
> Deleting my package.loading function and replacing
> with another that always eval to true ?

You changed the rules first by monkey-patching 'require'. Why should
the undocumented 'package.loading' be a holy cow if the
well-documented 'require' has already been slaughtered?

There is a canonical way to have your own package loader: just put it
in 'package.preload'. You are not monkey-patching then, you are
exploiting a documented mechanism that has been supplied for the very
purpose.