Trying to use Lua module fails on the command line - lua

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.

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()

Related

Why lua require won't search current directory?

say I've two files test.lua and m.lua in a folder, in test.lua as:
require("m")
then I run this file, howerver it raise an error:
lua: /Users/xx/works/scripts/test.lua:43: module 'm' not found:
no field package.preload['m']
no file '/usr/local/share/lua/5.3/m.lua'
no file '/usr/local/share/lua/5.3/m/init.lua'
no file '/usr/local/lib/lua/5.3/m.lua'
no file '/usr/local/lib/lua/5.3/m/init.lua'
no file './m.lua'
no file './m/init.lua'
no file '/usr/local/lib/lua/5.3/m.so'
no file '/usr/local/lib/lua/5.3/loadall.so'
no file './m.so'
As you can see the line no file './m.lua' appears but this is not true. ./m.lua exists, and file permission stuff is OK. If I hardcode the path:
package.path = package.path..';'..'/Users/xx/works/scripts/?.lua'
require('m')
It will work as expected.
What should I do to make lua search current directory fisrt (like python's import) when require a module
The current directory is the directory where you launch lua from.
The command line is missing in your example, if you used lua test.lua then it shoud work, if you used lua works/scripts/test.lua then it will not work.

How To Require Lua Socket?

I'm very new to lua development with file manipulation, and now trying to import the lua socket package into my project according to this post, but I can't run even the code below.
I guess the error message indicates I need to import not only the socket.lua but also .\socket\core (probably .dll, since it doesn't have core.lua), while a reply at the post suggested importing only the file.
I'm stuck in just the beginning... What do I have to do for the next step?
local function main()
local socket = require("socket")
end
main()
Exception in thread "main" com.naef.jnlua.LuaRuntimeException: ...n32.win32.x86_64\workspace\TestForCiv\src\socket.lua:13: module 'socket.core' not found:
no field package.preload['socket.core']
no file '.\socket\core.lua'
no file 'C:\Program Files\Java\jre1.8.0_151\bin\lua\socket\core.lua'
no file 'C:\Program Files\Java\jre1.8.0_151\bin\lua\socket\core\init.lua'
...(a bunch of no file errors continues)
Edit: I added the folder structure. Even I add the .dll file it returns the same error.
I don't know the details of you configuration, but try this
require ("src.socket")
you should require a module from the root path of the lib

Torch lua requires not finding the files

My directory structure looks like this:
--trian.lua
--util/
-- misc.lua
-- io.lua
-- init.lua
In my train.lua I am using require to include the files in util directory as follows:
require 'util.misc'
require 'util.io'
require 'util.init'
When I execute train.lua, misc and io are loaded but init is not getting loaded. I am not sure how to debug this issue. Could some please help me with the issue?

Lua on redis error: Script attempted to access unexisting global variable 'require'

This is the test script I am trying to execute:
local cjson = require "cjson"
local json_text = '[ true, { "foo": "bar" } ]'
local value = cjson.decode(json_text)
return 'Decoded: ' .. value
Its giving a strange response:
evalsha 76b573109be38414056b58c749016a56052063bd 0
(error) ERR Error running script (call to f_76b573109be38414056b58c749016a56052063bd): #enable_strict_lua:15: user_script:1: Script attempted to access unexisting global variable 'require'
How come the compiler isn't able to understand the keyword "require"? I am using redis 3.0.5 (running Lua 5.1)
require is not available within Redis, libraries are preloaded. Just remove the first line of your script and it should work.

module in F# interactive startup file

I have a finit.fsx file to be load at fsi.exe startup like:
#r xxxx
type xx = xxx
module Util =
let cd path = xxxx
...
After start fsi.exe, it is loaded as
namespace FSI_0002
module Util = begin
val cd : string -> unit
...
end
The problem is how can I use module util? I cannot open Util or use cd directly.
To further puzzle me, if I put a namespace at the top line like namespace Test, In fsi.exe, it is loaded as
namespace FSI_0002.Test
val cd : string -> unit
...
Where is the Module Util? I then have to open Test;; then Util.cd.
Is there a way to define module in the F# startup script and auto open the module? thanks.
I believe you can explicitly specify that all code in the .fsx file is in some module (say, Main) and add the AutoOpen attribute to the module:
[<AutoOpen>]
module Main
let foo = 10
When you then #load the file, the foo value should be visible at the top-level.
[<AutoOpen>]
module Util =
let cd path = ...

Resources