Well, like I said, if you declare those variables and functions local inside the script, then they will be automatically cleared when the script exits. Say you have a Lua file that consists of this:
From a Lua prompt, execute call dofile("test1.lua")
. After that executes, call print(a)
and the result will be 3.
Then, modify the script file to this:
From a new prompt, use dofile to execute that file then again call print(a)
. The result this time now will be nil, since the variable a is local to the script.
If you want a bit more control over the permanence of some things, you can use tables to your advantage. In the script, declare all functions and variables and assign them to be members of a table, rather than lurking in the global space:
f=function(g) print(g) end
After that script executes, the only thing in global state will be the localspace
table. Then, you can easily wipe everything the script declared simply by setting localspace
to nil; everything it contains will be sent to the garbage heap.
In fact, many references will recommend that you use levels of nesting, rather than dumping everything in the global environment directly. With enormous programs that have lots of data, it can make a difference, since Lua data structures reduce down to a lot of map-type data structures, and searching a set of nested structures can require overall fewer operations than searching one big global map.