Curious Gorge 21 Junior Poster in Training

Hey-ya I'm learning Lua and kind of understanding it but I just don't understand some of the code on this Particularly the code inserted into this post. I would just like someone to comment it and explain what its doing so that I may understand. The page referenced lacks comments on that particular snippet. I apologize if this isn't appropriate for this website... I couldn't find any place to post this that was popular enough to get a response quick enough to my liking. I also have about a billion different web forum accounts to keep track of and one more just seemed like too much :P

Hopefully someone knows Lua enough to help me :)

local M = {}
do
    local globaltbl = _G
    local newenv = setmetatable({}, {
        __index = function (t, k)
            local v = t[k]
            if v == nil then return globaltbl[k] end
            return v
        end,
        __newindex = M,
    })
    if setfenv then
        setfenv(1, newenv) -- for 5.1
    else
        _ENV = newenv -- for 5.2
    end
end

local function private()
    print("in private function")
end

function foo()
    print("Hello World!")
end

function bar()
    private()
    foo()
end

return M