One of the first examples in the Lua book is a function that's saved in a file called lib1.lua. The file works as expected when loaded via dofile:
% lua
> dofile("lib1.lua")
> twice(3)
6.0
But the file is not found when loaded via the -l option:
% lua -i -llib1.lua -e "print(twice(3))"
Lua 5.4.6 Copyright (C) 1994-2023 Lua.org, PUC-Rio
lua: module 'lib1.lua' not found:
no field package.preload['lib1.lua']
no file '/usr/share/lua/5.4/lib1/lua.lua'
no file '/usr/share/lua/5.4/lib1/lua/init.lua'
no file '/usr/lib64/lua/5.4/lib1/lua.lua'
no file '/usr/lib64/lua/5.4/lib1/lua/init.lua'
no file './lib1/lua.lua'
no file './lib1/lua/init.lua'
no file '/usr/lib64/lua/5.4/lib1/lua.so'
no file '/usr/lib64/lua/5.4/loadall.so'
no file './lib1/lua.so'
no file '/usr/lib64/lua/5.4/lib1.so'
no file '/usr/lib64/lua/5.4/loadall.so'
no file './lib1.so'
stack traceback:
[C]: in function 'require'
[C]: in ?
lib1.lua is in the current folder where the script is being executed:
% ls
lib1.lua
Why is the -l option not finding lib1.lua?
Just use -l lib1
.
I guess the book you are reading is Programming in Lua first edition, unfortunately it is outdated. That edition was written for Lua 5.0 and since Lua 5.1, the default search path no longer needs the .lua
extension, and dot .
in the module name will also be replaced with a slash /
by default.