3

I have some code which uses LuaFileSystem. However not all systems it will be run on have LuaFileSystem installed. I would like to check if it is installed, and only run the code if it is. Something like this (but this fails and states lfs is a null value)

local lfsExists, lfs = pcall(function () require "lfs" end)
if lfsExists then
    local lastUpdateTime = lfs.attributes( mapFilePName ).modification
end
David
  • 151
  • 1
  • 11

1 Answers1

3

That pcall-ed function doesn't return any values. Drop , lfs.

Also you don't need the anonymous function.

local lfsExists = pcall(require, "lfs")

Or to use the return value from require instead of the (implicit) global.

local lfsExists, lfs = pcall(require, "lfs")
Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
  • ... or add `return` before the `require`. Your solution without the anonymous function already returns the `lfs` module table as the second value, btw. – siffiejoe Feb 05 '15 at 23:03
  • Yes, I chose not to fix the unnecessary function case. The point about the return from directly calling `require` is a good one though. – Etan Reisner Feb 05 '15 at 23:10