lua-users home
lua-l archive

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


I have a file like this:

  -- foo.lua
  require 'bar'
  local bar = bar
  module('foo')

  function jim()
    bar.go()
  end

  Jim = jim( )

  -- gobs of other code here

I wanted to split foo.lua into a lot of manageable files, with foo.lua loading them. So I added this:

  for _,fileName in pairs( subfiles ) do
    -- yes, I added loadfile and setfenv as local variables
    local fileChunk = loadfile( fileName )
    setfenv( fileChunk, _M )
    fileChunk( )
  end

While setfenv lets me call jim() from my sub file code, it does not give me access to bar, or any other local variables I set up prior to opening the module.

What's the best way to accomplish this? I'd prefer to minimize the overhead of setup in all the subfiles to...well, ideally to zero. I'd like to just copy/paste code from foo.lua into them, add the subfilename to the list, and have it just work.

Must I package.seeall the foo module? I've been avoiding that for some odd feeling of purity, but it would be nice to remove crap like: local runtime ,setmetatable ,ipairs,select,error,table,math,rawget,string,unpack,loadfile,setfenv = runtime ,setmetatable ,ipairs,select,error,table,math,rawget,string,unpack,loadfile,setfenv