Lua Modules
When running Lua scripts the module(..., seeall) generates an error, I'm running HAH version 283, but have had the same problem on other versions as well. Take the following test script :-
module(..., package.seeall)
function hello()
print("Hello")
end
This generates the following error :-
# lua test1.lua
lua: test1.lua:1: bad argument #1 to 'module' (string expected, got nil)
stack traceback:
[C]: in function 'module'
test1.lua:1: in main chunk
[C]: ?
And running Lua interactively :-
# lua
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
> require "test1"
> a.hello()
stdin:1: attempt to index global 'a' (a nil value)
stack traceback:
stdin:1: in main chunk
[C]: ?
Checking the Lua wiki, where I got the test code from, I can't see anything wrong with what I'm doing.
Can anyone else help ?
Thanks
Karl
Your module has the namespace test1 - a module is always imported into a namespace that is the filename.
So you would need to do this
> require"test1"
> test1.hello()
OR import the module into a new namespace.
> a=require"test1"
> a.hello()
Now for something more fancy you can import a function from a module and rename it in the local namespace.
> world=require"test1".hello
> world()
You've got to keep reading that LUA manual of yours :)
Brett
Karl,
If test1.lua is a module you will always get an error if you try to run it from the command line directly.
This is equivalent in windows to asking to execute a .DLL it makes no sense? What does this mean to execute a block LIBRARY code?
There is no bug its just your misunderstanding of what you are doing.
Consider the following file and its contents.
x.lua
module(...,package.seeall)
function hello()
print("Hello")
end
hello()
In this instance hello() is a x module initializer that will only be invoked when the module is required for the first time. You can't run this module directly from the command line by invoking it. Again I can demonstrate this using the command line.
$ lua
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
> require"x"
Hello
>
The function hello is executed when the module is brought into the _G (global) namespace for the very first time.
Brett
Karl. My scripts start as follows: module(...,package.seeall) I.e. no space after the , Not sure if it's important but I dont get errors using this. Garry