Lua Modules

4 replies [Last post]
BoxingOrange
Offline
United Kingdom
Joined: 11 Jun 2010

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

garrydwilms
Offline
United Kingdom
Joined: 31 Mar 2011
Try this?

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

brett
Offline
Providence, United States
Joined: 9 Jan 2010
Its just a namespace issue

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

BoxingOrange
Offline
United Kingdom
Joined: 11 Jun 2010
It's the module

Brett, I take on board what you said, however I think that only applies to the second example that I posted, I believe the first example should run without error. Take this code :-

module(...,package.seeall)

function hello()
  print("Hello")
end

hello()

Before running this I would have expected the output to be "Hello", without the quotes.  What I actually get is :-

lua: hello.lua:1: bad argument #1 to 'module' (string expected, got nil)
stack traceback:
        [C]: in function 'module'
        hello.lua:1: in main chunk
        [C]: ?
#

The error is related to the module command, if I comment that line out I get the expected results.

 

This output shows that the package module seems to be working :-

# lua
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> table.foreach(package, print)
preload table: 0x1000ce80
loadlib function: 0x10009060
loaded  table: 0x10009a60
loaders table: 0x1000cfe0
cpath   ./?.so;/usr/lib/lua/5.1/?.so;/usr/lib/lua/5.1/loadall.so;
config  /
;
?
!
-
path    ./?.lua;/usr/share/lua/5.1/?.lua;/usr/share/lua/5.1/?/init.lua;/usr/lib/lua/5.1/?.lua;/usr/lib/lua/5.1/?/init.lua
seeall  function: 0x10009020

 

It does seem odd that it's not affecting others, ie they can run your sample lua scripts without the same error.  I'll try a clean reflash and see if that helps, although I do think I've done this in the past without success.

brett
Offline
Providence, United States
Joined: 9 Jan 2010
You can't execute a module

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

 

Hardware Info