lua-users home
lua-l archive

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


On 15 April 2013 15:12, Hisham <h@hisham.hm> wrote:
> On 15 April 2013 06:48, Philipp Janda <siffiejoe@gmx.net> wrote:
>> Am 11.04.2013 21:58 schröbte Petite Abeille:
>>
>>>
>>> On Apr 11, 2013, at 9:39 PM, Philipp Janda <siffiejoe@gmx.net> wrote:
>>>
>>>> which sets _ENV directly
>>>
>>>
>>> Out of curiosity, what issue would that entail from your perspective?
>>
>> It limits you to Lua 5.2.
>
> Does it?
>
> -- PA_module.lua ----------
> function module( aName, ... )
>   local aModule = package.loaded[ assert( aName ) ]
>   if type( aModule ) ~= 'table' then
>     aModule = {}
>     aModule._M = aModule
>     aModule._NAME = aName
>     aModule._PACKAGE = aName:sub( 1, aName:len() - (
> aName:reverse():find( '.', 1, true ) or 0 ) )
>     package.loaded[ aName ] = aModule
>     for anIndex = 1, select( '#', ... ) do
>       select( anIndex, ... )( aModule )
>     end
>   end
>   return aModule
> end
> --------------------
>
> -- mymod.lua ----------
> local print = print
> local module = module
>
> local _ENV = module("mymod")
> function blo()
>    print("I'm Blo!")
> end
>
> local _ENV = module("argh")
> function groo()
>    print("Grooooo")
> end
> --------------------
>
> -- main.lua ----------
> local mymod = require("mymod")
> local argh = require("argh")
> mymod.blo()
> argh.uff()
> assert(not pcall(argh.blo))
> --------------------
>
> ]/Programs/Lua/5.2.0/bin/lua -lPA_module main.lua
> I'm Blo!
> Uff
>
> ]/Programs/Lua/5.1.5/bin/lua main.lua
> I'm Blo!
> Uff

(Oops, I just hit "Send" too fast. The argh module evidently had two
functions and I stripped away the wrong one when shortening the
example for the email. function groo() ... end should of course be
function uff() print("Uff") end -- you get the idea.)

-- Hisham
http://hisham.hm/