5

Is it possible to prevent a lua script from failing when a require fails to find the script required?

3 Answers 3

10

This is basic usage

if pcall(require, 'test') then
    -- done but ...
    -- In lua 5.2 you can not access to loaded module.
else
    -- not found
end

But since Lua 5.2 it is deprecated set global variables when load library you should use returned value from require. And using only pcall you need :

local ok, mod = pcall(require, "test")
-- `mod` has returned value or error 
-- so you can not just test `if mod then` 
if not ok then mod = nil end
-- now you can test mod
if mod then
  -- done
end

I like this function

local function prequire(m) 
  local ok, err = pcall(require, m) 
  if not ok then return nil, err end
  return err
end

-- usage
local mod = prequire("test")
if mod then 
  -- done
end
5
  • 1
    The last one is really sweet. Language itself should be having something like that. The thing I don't like is that pcall will be called on every prerequire.
    – Leri
    Jul 26, 2013 at 9:56
  • 1
    I do not think this is big problem. cfunction pcall call cfunction require.
    – moteus
    Jul 26, 2013 at 9:59
  • I would like to see this function in standard library too. Also as function that allows load one library from list (bit = vrequire("bit32", "bit")).
    – moteus
    Jul 26, 2013 at 10:03
  • AFAIK Lua is open-source. I hope someone will add that and make pull request. If I was little bit free, I could play with it but unfortunately business stuff is more important. :(
    – Leri
    Jul 26, 2013 at 10:06
  • 1
    Lua is open source but development in close. Best way is write purpose on lua-l mail list. There no official open repository. In paste week there were Lua 5.3-work1 that add integers to Lua.
    – moteus
    Jul 26, 2013 at 10:09
5

In Lua errors are handled by pcall function. You could wrap require with it:

local requireAllDependenciesCallback = function()
    testModul = require 'test';
    -- Other requires.
end;

if pcall(requireAllDependenciesCallback) then
    print('included');
else
    print('failed');
end

Demo

Note: pcall is really expensive and should not be actively used. Make sure, you really need to mute require fails.

3
  • Thanks PLB, that works. When you say it shouldn't be actively used, is that purely from a performance perspective or are there other issues?
    – MarkNS
    Jul 26, 2013 at 9:20
  • @MarkNuttall-Smith Performance only. If you decide to use pcall for this. I'd suggest creating some function that will be handling all requires to avoid too much pcalls. E.g.: local requireFiles = function() -- requires here. end; and pass it to pcall.
    – Leri
    Jul 26, 2013 at 9:29
  • Performance is not a concern for me here actually, because it's just during start up of the app that I will be doing this. I wanted to be able to either find dependencies in a user specific directory, or a shared location. Thanks again.
    – MarkNS
    Jul 26, 2013 at 9:46
1

Instead of using pcall, you add can your own loader at the end of the list of loaders and make it so that your loader never fails but instead returns a special value, such as a string. Then you can use require normally and just check its return value. (Loaders are now called searchers in 5.2.)

1
  • But this is influences to all modules. And this is may be big surprise that require return invalid value.
    – moteus
    Jul 26, 2013 at 12:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.