lua-users home
lua-l archive

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


Doh, newbie mistake.  I put in pcall(require('zlib')), when it should
have been: pcall(require, 'zlib')

Thanks for setting me straight,
Dan


On Mon, May 9, 2016 at 9:25 AM, Matthew Wild <mwild1@gmail.com> wrote:
> On 9 May 2016 at 17:19, Dan Christian <danchristian65@gmail.com> wrote:
>> How do I make require() not cause a stack trace when a module is not found?
>
> ----------------------
>
> local have_library, library = pcall(require, "mylibrary")
>
> -- Do something if the library was not found, if you want
> if not have_library then
>     print("Library was not found. Error:", library)
> end
>
> -- ... --
>
> if have_library then
>     -- Use the library
>     library.foo("bar")
> end
>
> ----------------------
>
> You said pcall didn't work. Easy mistake to make is:
> pcall(require("mylibrary")). Which is wrong, after you think about it
> more :)
>
> Regards,
> Matthew
>