Search code examples
torch

the difference of include and require in torch


What's the difference between require and include in torch(lua)? what it did in the back when we call include or require? for example:

 include('util/test.lua')
 require('util/test.lua')

Solution

  • Torch include is simply Lua dofile as can be seen in the Torch source:

    function torch.include(package, file)
       dofile(torch.packageLuaPath(package) .. '/' .. file)
    end
    

    On the other hand, Lua require is used to load modules. Also see this answer.

    Roughly, require does the same job as dofile, but with two important differences. First, require searches for the file in a path; second, require controls whether a file has already been run to avoid duplicating the work. Because of these features, require is the preferred function in Lua for loading libraries.