lua-users home
lua-l archive

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


It was thus said that the Great Dirk Laurie once stated:
> 2017-09-06 10:26 GMT+02:00 Daurnimator <quae@daurnimator.com>:
> > At the moment, package.path and package.cpath are strings containing
> > semicolon separated lists.
> > I'd like to propose that they are transitioned to be sequences to
> > allow for easier programmatic manipulation.
> >
> > The environment variables LUA_PATH, LUA_PATH_5_4, LUA_CPATH, etc
> > wouldn't change: they'd just need to be parsed when the package
> > library is loaded.
> >
> > In the next release we can allow package.path and package.cpath to be
> > strings for compatibility.
> > However the construct `package.path = package.path ..
> > ";/my/path/?.lua" would break and need to be changed to
> > `table.insert(package.path, "/my/path/?.lua")`
> > If this breakage is too much, perhaps we could have three phases:
> >   - phase 1. we allow table form, but still use string form on lua
> > command line application load.
> >   - phase 2. we change the default to be table form; string form is deprecated
> >   - phase 3. we remove support for string form.
> 
> local path = {}
> package.path:gsub("[^;]*,function(x) path[#path+1]=x end

  Well, fix that up a bit:

local path = {}
package.path:gsub("[^;]*",function(x) path[#path+1]=x end)

Lua 5.1 returns:

path =
{
  [1] = "./?.lua",
  [2] = "",
  [3] = "/usr/local/share/lua/5.1/?.lua",
  [4] = "",
  [5] = "/usr/local/share/lua/5.1/?/init.lua",
  [6] = "",
  [7] = "/usr/local/lib/lua/5.1/?.lua",
  [8] = "",
  [9] = "/usr/local/lib/lua/5.1/?/init.lua",
  [10] = "",
}

while Lua 5.3 will return:

"path" =
{
  [1] = "/usr/local/share/lua/5.3/?.lua",
  [2] = "/usr/local/share/lua/5.3/?/init.lua",
  [3] = "/usr/local/lib/lua/5.3/?.lua",
  [4] = "/usr/local/lib/lua/5.3/?/init.lua",
  [5] = "./?.lua",
  [6] = "./?/init.lua",
}

Perhaps a better version:

local path = {}
package.path:gsub("[^;]+",function(x) path[#path+1]=x end)

Works better, but I'm still bothered by that ";", which *could* be changed
by an implementation.  Perhaps:

local path = {}
package.path:gsub(
	"[^%" .. package.config:sub(3,3) .. "]+",
	function(p) path[#path + 1] = p end
)

  -spc (There ... that's better)