lua-users home
lua-l archive

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


On Wed, Jun 9, 2010 at 8:44 AM, Tomas Lundell <tomas.lundell@gmail.com> wrote:
> We're considering moving to Lua 5.2 upon release. While it's looking
> promising, the biggest hurdle for us is the abolition of getfenv.
>
> We're using getfenv to simulate Python's package system, such that:
>
> import 'hello.world'
>
> imports a lua file into the variable 'world' of _this_ environment.
>
> One could imagine the implementation would look something like this:
>
> function import(fname)
>     ...
>     local this_env = {}
>     loadin(fname, this_env)
>     local calling_env = getfenv(2)
>     calling_env['world'] = this_env
> end
>
> Is this possible to emulate using Lua 5.2 without having to use:
>
> world = import 'hello.world'

A syntactic idea:

_ENV:import "hello.world"

Then your import function receive the caller's environment in the
first argument...

-- 
- Patrick Donnelly