You can execute a sequence of chunks by giving them all as arguments
to the stand-alone interpreter, with the -l option. For instance, if
you have a file a with a single statement x=1 and another file b with
the statement print(x), the command line
prompt> lua -la -lb
will run the chunk in a, then the one in b, which will print the
expected 1.
The above is from the following link: https://www.lua.org/pil/1.1.html. Yet, when I was trying it out, I got a syntax error.
So, in file a.lua, I have only one line, which is a=1. Then in file b.lua, I have also only one line print("the value of a is:",a) . Then,
:~$ lua -i -la -lb
Lua 5.2.4 Copyright (C) 1994-2015 Lua.org, PUC-Rio
the value of a is: true
>
:~$
:~$ lua -la -lb
the value of a is: true
Lua 5.2.4 Copyright (C) 1994-2015 Lua.org, PUC-Rio
>
Why did it print out "the value of a is: true"? rather than "the value of a is: 1"?
Any comments are greatly appreciated.
The error is because you are use a = 1 in your file a.lua, unlike in the example where a.lua contains x = 1 and b.lua contains print(x).
Using a rather then x means your using the same a variable being written to when -la completes, changing it to true.
This happens because the option -l name is equal to name = require("name"). When require completes, on a file that returns no result to it, require will return true.
your command lua -la -lb would look something like this to lua:
a = require("a") --this returning true after it completes
b = require("b") --this printing the value of `a` which will always be `true`
Sources:
Similar question answered on Lua-Users: command-line -l option issue
Egor Skriptunoff's Comment on this question
Related
I have already correctly installed Lua, LuaJIT and Luarocks (I can run each of them in the terminal without any errors too). I'm trying to run the command luarocks install luaffi but it is returning an error
Error: No results matching query were found for Lua 5.4
To check if it is available for other Lua versions, use --check-lua-versions.
So I tried running luarocks install luaffi --check-lua-versions which also returns the following:
Checking if available for other Lua versions...
Checking for Lua 5.1...
Checking for Lua 5.2...
Checking for Lua 5.3...
Error: No results matching query were found for Lua 5.4.
luaffi is not available for any Lua versions. "
I'm completely lost, I cannot use ffi in my code because of this (It returns module "ffi" not found).
After the build (See: https://github.com/zhaozg/lua-ffi ) you can use Lua 5.1 to test it.
€ cat luajit_sleep.lua
--[[
Lua JIT ffi Example - Implementing a Lua sleep() function from C
]]
local ffi = require("ffi")
ffi.cdef[[void Sleep(int ms); int poll(struct pollfd *fds, unsigned long nfds, int timeout);]]
local sleep
if ffi.os == "Windows" then
function sleep(s) ffi.C.Sleep(s*1000) end
else
function sleep(s) ffi.C.poll(nil, 0, s*1000) end
end
return sleep
€ /bin/lua
Lua 5.1.5 Copyright (C) 1994-2012 Lua.org, PUC-Rio
> sleep = require('luajit_sleep')
> sleep(1) -- CPU friendly sleep() function :-)
It uses (lsof | grep lua)...
/usr/local/lib/lua/5.1/ffi.so
EDIT - Found the Command to install: https://luarocks.org/modules/colesbury/luaffi
Do: luarocks install --server=https://luarocks.org/dev luaffi --verbose
Also goes to...
os.execute: chmod '0755' '/usr/local/lib/lua/5.1/ffi.so'
Results: 1
1 (number): 0
luaffi scm-1 is now installed in /usr/local (license: BSD)
Using string.format() (which supposedly defers to C's sprintf())) to format a number in LuaJIT rounds differently than every other Lua interpreter I've tried:
$ lua -v
Lua 5.4.1 Copyright (C) 1994-2020 Lua.org, PUC-Rio
$ lua -e 'print(string.format("%.4f", 32.90625))'
32.9062
$ luajit -v
LuaJIT 2.1.0-beta3 -- Copyright (C) 2005-2020 Mike Pall. http://luajit.org/
$ luajit -e 'print(string.format("%.4f", 32.90625))'
32.9063
Why does this happen and how can I make it stop? I'd like to tell LuaJIT to round the same direction every other Lua interpreter does.
I am trying to make one lua file require another. I am following this guide: http://lua-users.org/wiki/ModulesTutorial
My basic test, which should be a trivial hello world, does not work, and I can't figure out why.
Here's a console log which shows all files and all errors:
C:\Users\TestUser\Desktop\LuaTest>dir
Volume in drive C has no label.
Volume Serial Number is XXXX-XXXX
Directory of C:\Users\TestUser\Desktop\LuaTest
11/15/2017 03:03 PM <DIR> .
11/15/2017 03:03 PM <DIR> ..
11/15/2017 02:53 PM <DIR> Bar
11/15/2017 03:04 PM 92 BazModule.lua
11/15/2017 02:53 PM <DIR> Foo
11/15/2017 03:08 PM 139 main.lua
2 File(s) 231 bytes
4 Dir(s) 253,774,073,856 bytes free
C:\Users\TestUser\Desktop\LuaTest>lua main.lua
lua: main.lua:1: module 'BazModule' not found:
no field package.preload['BazModule']
no file 'C:\dev\LuaDist\bin'
no file '.\BazModule.dll'
no file 'C:\dev\LuaDist\bin\..\lib\lua\BazModule.dll'
no file 'C:\dev\LuaDist\bin\..\lib\lua\loadall.dll'
stack traceback:
[C]: in function 'require'
main.lua:1: in main chunk
[C]: ?
C:\Users\TestUser\Desktop\LuaTest>type main.lua
local baz = require("BazModule")
baz.Baz()
local bar = require("Bar.BarModule")
bar.Bar()
local foo = require("Foo.FooModule")
foo.Foo()
C:\Users\TestUser\Desktop\LuaTest>type BazModule.lua
local BazModule = {}
function BazModule.Baz()
print("Hello Baz!")
end
return BazModule
C:\Users\TestUser\Desktop\LuaTest>lua -v
Lua 5.1.5 Copyright (C) 1994-2012 Lua.org, PUC-Rio
The expected output should be
Hello Baz!
Hello Bar!
Hello Foo!
But it can't find any of the files adjacent to main.lua and I don't understand why.
require searches in directories listed in package.path (for Lua files) and package.cpath (for compiled libraries).
Your error message…
lua: main.lua:1: module 'BazModule' not found:
no field package.preload['BazModule']
no file 'C:\dev\LuaDist\bin'
no file '.\BazModule.dll'
no file 'C:\dev\LuaDist\bin\..\lib\lua\BazModule.dll'
no file 'C:\dev\LuaDist\bin\..\lib\lua\loadall.dll'
indicates the paths that require searched in. It seems that package.path is completely empty, or maybe there's a single malformed path pattern in there. (Which would be C:\dev\LuaDist\bin.)
The way the search for a module foo.bar works is that ? is substituted by foo/bar (or foo\bar – depending on OS) and so ./?.lua would find ./foo/bar.lua.
So the way to fix this is to (a) fix the place where you (or something that you installed) are/is mangling the package.path (via environment variable, startup script, …?) and/or (b) add the current directory to the search path.
$ lua -v -e "print(os.date('%l'))"
Lua 5.2.2 Copyright (C) 1994-2013 Lua.org, PUC-Rio
lua: (command line):1: bad argument #1 to 'date' (invalid conversion specifier '%l')
stack traceback:
[C]: in function 'date'
(command line):1: in main chunk
[C]: in
If so, where am I supposed to submit a bug report?
It's not a bug in Lua. os.date uses format, as described by strftime C function provided by your compiler (I mean the compiler that Lua was compiled with). It is known that some compilers (MSVC, for instance, which just outright crashes when provided with certain patterns) do not provide all patterns.
I've looked over the Lua & LuaFileSystem Docs and have yet to find a way to create a new file, I also scouted around on here but to the same end.
As a note, the solution I'm looking for has to be OS neutral to ensure portability, but I'm happy to get different answers for different systems.
Example (writing "Hello World" into test.txt):
$ lua
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
> file = io.open("test.txt", "w")
> file:write("Hello World")
> file:close()
> ^D
$ cat test.txt
Hello World
See also: Lua IO tutorial