lua-users home
lua-l archive

Re: Modules with standalone main program

[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.

AltStyle によって変換されたページ (->オリジナル) /