lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 8/2/05, Javier Guerra <javier@guerrag.com> wrote:
> On Tuesday 02 August 2005 2:26 pm, William Trenker wrote:
> > What is the "proper" way to reload a module on the fly?
> 
> i think in your case it's easier to use loadfile() instead of require()
> 

Hi Javier,

I really like your idea of checking to see if the script has changed. 
The reason I was focussing on require() is that I get the benefits of
the built-in LUA_PATH searching and the dotted notation for module
names.

Taking an idea from Python, I wonder if it would be beneficial for the
new module system to extend the package.loaded concept to include
keeping some information about each loaded module -- such as the
module's full path?  Then one could expand on your reloading script
and interface it with the module system -- perhaps along the lines of
this pseudocode:

function reloadModuleIfChanged(moduleName)
    if not package.loaded[moduleName] then
        -- first time loaded: initialize module information
        require(moduleName)
        -- assume require has saved the module's path
        -- in package.info[moduleName].modulePath
        local info = package.info[moduleName]
        local t = lsf.attributes(info.modulePath).modification
        package.info[moduleName].lastT = t
    else
        -- module already loaded: check if module's
        -- source code has changed
        local info = package.info[moduleName]
        local attr = lfs.attributes (info.modulePath)
        local t = attr.modification
        if t > info.lastT then
            -- module's code has changed: reload it
            package.loaded[moduleName] = nil
            require(moduleName)
            info.lastT = attr.modification
        else
            -- module hasn't changed: do nothing
        end
    end
end

The above code sketch is probably not the most efficient way to do
this and is likely incomplete but I hope it paints the picture.

Thanks for the input,
Bill