|
Am 10.08.2015 um 21:37 schröbte Milind Gupta:
Hi,
Hi!
I am trying to understand how does the require function invoke package.searchpath and package.searchers. Does a searcher called searchpath function? If yes then that means the searcher is responsible for reading the paths and deciding which one is a lua fie (for example the lua module searcher) and then passing the paths one by one to package.searchpath?
`require` calls the functions in `package.searchers` one after the other passing the module name as an argument until one of the searchers returns a function (the loaded chunk) or raises an error (e.g. if the module was found but contained syntax errors). If the searchers return a string instead, those error messages are concatenated and printed at the end if all searchers were unsuccessful.
`require` does not call `package.searchpath` directly, but a searcher may of course. All standard searchers except the first (the `package.preload` searcher) use the equivalent C code.
`package.searchpath` takes a path template list (usually `package.path` or `package.cpath`) and a module name, and creates a list of actual paths. It then tries those paths one by one, searching for a file that exists (is readable). If it finds one, it returns the path to that file. Otherwise it returns `nil` plus a string that can be returned from a searcher as an error message.
Thanks, Milind
Philipp