Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.
 Entire forum ➜ MUSHclient ➜ General ➜ Avoiding Require Errors

Avoiding Require Errors

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Candido   USA  (78 posts)  Bio
Date Sat 10 Jul 2010 05:14 PM (UTC)
Message
So I made a plugin that works fine without lua socket, but also has some added functionality if you have lua socket. The problem is that if you don't have lua socket, require will spam you with the 'not found' error. I feel like there should be a simple way to check for the existence of an add on like lua socket before you require it, but I can't seem to figure it out. I tried the obvious,

if require("socket.http") then http = require("socket.http") end

but that still generates the error. Anyone have some insight?
Top

Posted by WillFa   USA  (525 posts)  Bio
Date Reply #1 on Sat 10 Jul 2010 07:52 PM (UTC)

Amended on Sat 10 Jul 2010 07:53 PM (UTC) by WillFa

Message
I believe you can pcall(require , "socket") and then check package.loaded.socket if it actually did.


pcall (require, "socket")

-- Do happy stuff


if package.loaded.socket then 
  -- do enhanced functionality stuff.
end



actually, if socket should be enough, but package.loaded may be the safer route.
Top

Posted by Candido   USA  (78 posts)  Bio
Date Reply #2 on Sat 10 Jul 2010 09:51 PM (UTC)
Message
Did the trick. Thanks.
Top

Posted by Nick Gammon   Australia  (23,120 posts)  Bio   Forum Administrator
Date Reply #3 on Sat 10 Jul 2010 09:55 PM (UTC)
Message
The pcall tells you if it succeeded or not, so this is more reliable:


ok = pcall (require, "socket"); 

if ok then 
  -- use socket library now
end -- if


- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,120 posts)  Bio   Forum Administrator
Date Reply #4 on Sun 11 Jul 2010 12:28 AM (UTC)
Message
You can also do this, to find what require returned:


ok, socket = pcall (require, "socket"); 

if ok then 
  -- use socket library now 

  -- the value "socket" is non-nil and is the same as
  -- package.loaded.socket 

end -- if


This effectively lets you test later on if the require worked (which is pretty-much what Willfa suggested).

This works because the pcall returns as its second (and subsequent) return values what the original function returned, on success.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


17,935 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.