As the title says... Chat on...

User avatar
By TerryE
#19156 I've just uploaded another tranche of edits and content.
User avatar
By Nubble
#21663 Hi Terry,
A really, really great write up!! Thank you so much for this. Ive finished my second read of it.

I have a question on volatile modules and specifically the volatile function. You posted up some code:
Code: Select all  local module  ...  -- this is a situation where using an upvalue is essential!
  return function (csocket)
    package.loaded[module]=nil
    module = nil
    -- . . .
  end


and I mostly understand all of this except for the very first line "local module ... --comment"
When i compile this in the esp i get a lua error: stdin:1: spl.lua:1: unexpected symbol near '...'
I cannot seem to determine why this would be. Any help please??

Here is the full volatile module/function:
Code: Select alllocal spl ...

return function split(inputstr, sep)
        if sep == nil then
            sep = "%s"
        end
        local t={}
        local i=1
        for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
            t[i] = str
            i = i + 1
        end
        package.loaded['spl']=nil
        spl=nil
        return t
end
User avatar
By TerryE
#21787 Durr,, my bad. There's an = missing. Require passes a single argument: it's name, so the tuple ... which the varargs reference is a list with a single variable in it -- the name, so if your file is "spl.lua" then if the first assignment is
Code: Select alllocal module  = ...

will set the local variable module to "spl". The require() will set package.loaded.spl to reference the returned function, preventing its garbage collection, hence
Code: Select allpackage.loaded[module]=nil

will remove this reference so once all other references go out of scope, the memory used by the module can be garbage collected.

I hope this explanation helps. :D