Reputation: 15660
I'm trying to learn how to use Lua modules. I've been reading the following manual:
http://lua-users.org/wiki/ModulesTutorial
Unfortunately, I can't even get the first example working! I've done the following:
Created a "mymodule.lua" file which looks like this:
local mymodule = {}
function mymodule.foo()
print("Hello World!")
end
return mymodule
Then from the command line, within the folder where the mymodule.lua file resides, I tried to do the following:
mymodule = require "mymodule"
But I get the following error message:
myserver:/usr/share/x/research/# mymodule = require "mymodule"
-ash: mymodule: not found
This works:
myserver:/usr/share/x/research/# local mymodule = require "mymodule"
But then when I try to run the foo() method it fails like so:
myserver:/usr/share/x/research/# mymodule.foo()
-ash: syntax error: bad function name
myserver:/usr/share/x/research/#
And I guess this makes sense because I declared mymodule as local instead of global on the command line.
I guess my question is why can't I declare the module globally on the command line.
The manual says that I should be running this from an "interactive interpreter". I am using a standard commandline / terminal window in linux... could this be the issue? I usually have to prefix all lua commands with "lua ".
Any suggestions would be appreciated.
Upvotes: 1
Views: 793
Reputation: 80931
lua is not your shell. You need to run that code from inside the lua interpeter not at your shell prompt.
myserver:/usr/share/x/research/# lua
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
> mymodule = require "mymodule"
> mymodule.foo()
Upvotes: 2