Passing Parameters to Modules

Hi All,

I was wondering if there was a simple and efficient way to pass parameters to modules (like constructor methods in languages such as Java).

Before I say anything else, YES, I have done my homework by googling this. Specifically I visited pages including

http://lua.2524044.n2.nabble.com/require-passing-arguments-to-module-td6964798.html

and

http://stackoverflow.com/questions/9744693/how-can-i-pass-parameters-to-a-lua-file-when-loading-it-from-another-lua-file

which were somewhat insightful, but did not really get the job done for me.

Below is what I would like to achieve (which probably won’t compile):

<a.lua>

b = require("b", "foo","bar") print(b.var1) -- should print "foo" print(b.var2) -- should print "bar"

<b.lua>

local var1, var2 = ...

If you could provide actual functional code that would be great!

Thanks in advance! :smiley:

Can you describe your usecase a bit more? The below will work just as well, but I have a feeling there is more to what you are attempting to accomplish

--a.lua local b = require( "b" ) b.var[1], b.var[2] = "foo","bar" print("values of the b module are "..tostring(b.var[1]).." and "..tostring(b.var[2]))

--b.lua local b = {} b.var = {} return b

Ok
b.lua

local m = {}
m.var2 = “bar”
return m

a.lua

local b = require(“b”)
b.var1 = “foo”
print(b.var1)
print(b.var2)

Edit: just a note I would use code tags but I am on the go(on my iPhone)

Man Alex beat me by a minute

By the way for more help on this check out
https://coronalabs.com/blog/2012/08/28/how-external-modules-work-in-corona/

You can’t pass parametes to a module, that’s not how the mechanic works.

A module is basically a script in a separate file, that gets ‘executed’ and loaded into memory the first time you require it.

So, what do you do if you want to ‘configure’ the module?

I do this:

The Module (Totally Bogus But Hopefully you see where I’m going with the example)

-- -- sweet.lua -- local mode = 1 local sweet = {} function sweet.init( newMode ) mode = newMode end function sweet.doit( a, b ) if( mode == 1 ) then return a + b elseif( mode == 2 ) return a \* b else return 0 end end return sweet

Sample Use

local sweet = require "sweet" print( sweet.doit( 10, 15 ) ) sweet.init(2) print( sweet.doit( 10, 15 ) ) sweet.init(1) print( sweet.doit( 10, 15 ) )

Can you describe your usecase a bit more? The below will work just as well, but I have a feeling there is more to what you are attempting to accomplish

--a.lua local b = require( "b" ) b.var[1], b.var[2] = "foo","bar" print("values of the b module are "..tostring(b.var[1]).." and "..tostring(b.var[2]))

--b.lua local b = {} b.var = {} return b

Ok
b.lua

local m = {}
m.var2 = “bar”
return m

a.lua

local b = require(“b”)
b.var1 = “foo”
print(b.var1)
print(b.var2)

Edit: just a note I would use code tags but I am on the go(on my iPhone)

Man Alex beat me by a minute

By the way for more help on this check out
https://coronalabs.com/blog/2012/08/28/how-external-modules-work-in-corona/

You can’t pass parametes to a module, that’s not how the mechanic works.

A module is basically a script in a separate file, that gets ‘executed’ and loaded into memory the first time you require it.

So, what do you do if you want to ‘configure’ the module?

I do this:

The Module (Totally Bogus But Hopefully you see where I’m going with the example)

-- -- sweet.lua -- local mode = 1 local sweet = {} function sweet.init( newMode ) mode = newMode end function sweet.doit( a, b ) if( mode == 1 ) then return a + b elseif( mode == 2 ) return a \* b else return 0 end end return sweet

Sample Use

local sweet = require "sweet" print( sweet.doit( 10, 15 ) ) sweet.init(2) print( sweet.doit( 10, 15 ) ) sweet.init(1) print( sweet.doit( 10, 15 ) )