lua-users home
lua-l archive

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


On Thu, Aug 20, 2015 at 11:03 AM, Milind Gupta <milind.gupta@gmail.com> wrote:
> In Lua 5.3 I see the following
>
> --- Module file begin
> xml = require("LuaXml")
> local xml = xml
>
> local M = {}
> package.loaded[...] = M
> _ENV = M
>
> -- Module code here
> xml.load("File.xml")
>
> --- Module file end
>
> Here LuaXml.lua is something like
> local xml = require("LuaXml_lib")     --- dll file
> --- Module code
> return xml
>
>
> The following does not work and gives error that trying to index a nil value
> on the load statement pointing to the C module
>
> --- Module file begin
> local M = {}
> package.loaded[...] = M
> _ENV = M
>
> xml = require("LuaXml")
>
> -- Module code here
> xml.load("File.xml")    -- ERROR attempt to index nil value in
> LuaXml_lib.load
>
> --- Module file end
>
>
> The following gives the error that specified procedure not found
>
> --- Module file begin
> local M = {}
> package.loaded[...] = M
> _ENV = M
>
> xml = require("LuaXml_lib")
>
> -- Module code here
> xml.load("File.xml")    -- ERROR attempt to index nil value in
> LuaXml_lib.load
>
> --- Module file end
>
> Please can anyone help me understand why the errors are happening and what
> would be the right way to load another module inside a module successfully.

In the first, "xml" is local.

In the second, xml is converted, just like every non-local symbol, to
_ENV['xml'].  And you just set _ENV to be an empty table.

Such is the magic of _ENV.

As for the right way to do it... there could be several.  One would be
to create and assign locals for everything you will need to import.

-Parke