edit: i'm on ubuntu
So in the lua interpreter you can obviously call built in functions like
> functionName(functionArgs)
I want to make a new function that the lua interpreter recognizes every time I type it.
Is there a way that I can add my function to the list of natively recognized functions in the lua interpreter so that I don't have to call dofile() on the file I wrote it in and then run it from there.
TLDR I want to be able to type
> myNewFunction(functionArgs)
in the lua interpreter at any time and have the interpreter automatically know what function I am talking about.
If this is impossible is there at at least a way that I can run dofile(myFile) while I am in any directory and the lua interpreter would always be able to find my specific file containing my function?
Thanks for any help!
The thing to look at is the LUA_INIT
(or LUA_INIT_5_3
, …) environment variable:
When called without option
-E
, the interpreter checks for an environment variableLUA_INIT_5_3
(orLUA_INIT
if the versioned name is not defined) before running any argument. If the variable content has the format@filename
, thenlua
executes the file. Otherwise,lua
executes the string itself.
– https://www.lua.org/manual/5.3/manual.html#7
If you have a fixed list of functions, you can simply create a file (e.g. ${HOME}/.lua_init.lua
, on Windows maybe try %APPDATA%\something
or %USERPROFILE%\something
.) Then, put your functions in that file and set one of the LUA_INIT
environment variables pointing at that file, prefixing the file path with an '@'. Small example for unixoid OSen:
$ cd # just to ensure that we are in ${HOME}
$ echo "function ping( ) print 'pong' end" >> .lua_init.lua
$ echo 'export LUA_INIT="@${HOME}/.lua_init.lua"' >> .profile
$ source .profile
$ lua
Lua 5.3.3 Copyright (C) 1994-2016 Lua.org, PUC-Rio
> ping()
pong
(For Windows, see the comment by Egor Skriptunoff below.)
If you want to automatically load stuff from the current directory, that will be harder. A simple way would be to set up the above, then add e.g.
-- autoload '.autoload.lua' in current directory if present
if io.open( ".autoload.lua" ) then -- exists, run it
-- use pcall so we don't brick the interpreter if the
-- file contains an error but can continue anyway
local ok, err = pcall( dofile, ".autoload.lua" )
if not ok then print( "AUTOLOAD ERROR: ", err ) end
end
-- GAPING SECURITY HOLE WARNING: automatically running a file
-- with the right name in any folder could run untrusted code.
-- If you actually use this, add a whitelist of known-good
-- project directories or at the very least blacklist your
-- downloads folder, /tmp, and whatever else might end up
-- containing a file with the right name but not written by you.
into the LUA_INIT
file. Then, to get project/directory-specific functions to load automatically, create a .autoload.lua
that dofile
s or require
s files as needed, defines functions, ...
Fancier solutions (not requiring an extra file per folder) will be harder to do, but you can run arbitrary Lua code to build whatever you need.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With