require behavior in modules

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

require behavior in modules

aryajur
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.

Thanks,
Milind


Reply | Threaded
Open this post in threaded view
|

Re: require behavior in modules

Parke
On Thu, Aug 20, 2015 at 11:03 AM, Milind Gupta <[hidden email]> 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

Reply | Threaded
Open this post in threaded view
|

Re: require behavior in modules

Parke
On Thu, Aug 20, 2015 at 11:15 AM, Parke <[hidden email]> wrote:
> 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.

Or maybe I am wrong in the above.  Just realized that I would expect
you to get an error when you call "require", as "require" is not in
_ENV.  But you say the error happens later.

-Parke

Reply | Threaded
Open this post in threaded view
|

Re: require behavior in modules

aryajur
In reply to this post by Parke
In the 1st require ran in the global _ENV. So it worked fine. In the second case _ENV is initialized as an empty table and _ENV['xml'] = require("LuaXml") but I don't understand what stops it from working. Same for the 3rd case why would it not load the dll file? The error is on the loading of LuaXml_lib which works fine if done in the global environment.

On Thu, Aug 20, 2015 at 11:15 AM, Parke <[hidden email]> wrote:
On Thu, Aug 20, 2015 at 11:03 AM, Milind Gupta <[hidden email]> 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


Reply | Threaded
Open this post in threaded view
|

Re: require behavior in modules

aryajur
In reply to this post by Parke


Or maybe I am wrong in the above.  Just realized that I would expect
you to get an error when you call "require", as "require" is not in
_ENV.  But you say the error happens later.

Sorry about that confusion. Require is not a problem since I also do local require = require before assigning _ENV=M. The error in the 3rd case is when require("LuaXml_lib") is called. It says it cannot find the specified procedure.

Reply | Threaded
Open this post in threaded view
|

Re: require behavior in modules

Parke
In reply to this post by aryajur
On Thu, Aug 20, 2015 at 11:03 AM, Milind Gupta <[hidden email]> wrote:
> --- Module file begin
> xml = require("LuaXml")
> local xml = xml
>
> local M = {}
> package.loaded[...] = M
> _ENV = M

Milind and I discussed this offlist.

It turns out that LuaXml depends on "xml" being defined in _G, as
happens in the first line of the above.

-Parke