[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Modules with standalone main program
- From: Dirk Laurie <dirk.laurie@...>
- Date: Tue, 27 Feb 2018 05:01:34 +0200
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.