Module std.package
Additions to the core package module.
The module table returned by std.package also contains all of the entries
from the core package table. An hygienic way to import this module, then, is
simply to override core package locally:
local package = require 'std.package'
Manage package.path with normalization, duplicate removal,
insertion & removal of elements and automatic folding of '/' and '?'
onto package.dirsep and package.path_mark, for easy addition of
new paths. For example, instead of all this:
lib = std.io.catfile('.', 'lib', package.path_mark .. '.lua') paths = std.string.split(package.path, package.pathsep) for i, path in ipairs(paths) do -- ... lots of normalization code... end i = 1 while i <= #paths do if paths[i] == lib then table.remove(paths, i) else i = i + 1 end end table.insert(paths, 1, lib) package.path = table.concat(paths, package.pathsep)
You can now write just:
package.path = package.normalize('./lib/?.lua', package.path)
Functions
| find (pathstrings, patt[, init=1[, plain=false]]) | Look for a path segment match of patt in pathstrings. |
| insert (pathstrings[, pos=n+1], value) | Insert a new element into a package.path like string of paths. |
| mappath (pathstrings, callback, ...) | Call a function with each element of a path string. |
| normalize (...) | Normalize a path list. |
| remove (pathstrings[, pos=n]) | Remove any element from a package.path like string of paths. |
Tables
| package | Make named constants for package.config
(undocumented in 5.1; see luaconf.h for C equivalents). |
Types
| mappathcb (element, ...) | Function signature of a callback for mappath. |
Functions
- find (pathstrings, patt[, init=1[, plain=false]])
-
Look for a path segment match of patt in pathstrings.
Parameters:
- pathstrings
string
pathsepdelimited path elements - patt string a Lua pattern to search for in pathstrings
- init
integer
element(not byte index!) to start search at.
(default 1)Negative numbers begin counting backwards from the last element - plain
boolean
unless false, treat patt as a plain
(default false)string, not a pattern. Note that if *plain* is given, then *init* must be given as well.
Returns:
the matching element number(not byte index!) and full text
of the matching element, if any; otherwise nilUsage:
i, s = find(package.path, '^[^' .. package.dirsep .. '/]')
- pathstrings
string
- insert (pathstrings[, pos=n+1], value)
-
Insert a new element into a
package.pathlike string of paths.Parameters:
- pathstrings
string
a
package.pathlike string - pos
integer
element index at which to insert value, where
nis
(default n+1)the number of elements prior to insertion - value string new path element to insert
Returns:
-
string
a new string with the new element inserted
Usage:
package.path = insert(package.path, 1, install_dir .. '/?.lua')
- pathstrings
string
a
- mappath (pathstrings, callback, ...)
-
Call a function with each element of a path string.
Parameters:
- pathstrings
string
a
package.pathlike string - callback mappathcb function to call for each element
- ... additional arguments passed to callback
Returns:
-
nil, or first non-nil returned by callback
Usage:
mappath(package.path, searcherfn, transformfn)
- pathstrings
string
a
- normalize (...)
-
Normalize a path list.
Removing redundant
.and..directories, and keep only the first instance of duplicate elements. Each argument can contain any number ofpathsepdelimited elements; wherein characters are subject to/and?normalization, converting/todirsepand?topath_mark(unless immediately preceded by a%character).Parameters:
- ... path elements
Returns:
-
string
a single normalized
pathsepdelimited paths stringUsage:
package.path = normalize(user_paths, sys_paths, package.path)
- remove (pathstrings[, pos=n])
-
Remove any element from a
package.pathlike string of paths.Parameters:
- pathstrings
string
a
package.pathlike string - pos
integer
element index from which to remove an item, where
n
(default n)is the number of elements prior to removal
Returns:
-
string
a new string with given element removed
Usage:
package.path = remove(package.path)
- pathstrings
string
a
Tables
- package
-
Make named constants for
package.config(undocumented in 5.1; see luaconf.h for C equivalents).Fields: