[Home] [Downloads] [Search] [Help/forum]

Gammon Forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  General
. . -> [Subject]  Avoiding Require Errors
Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Avoiding Require Errors

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


Posted by Candido   USA  (78 posts)  [Biography] 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?
[Go to top] top

Posted by WillFa   USA  (525 posts)  [Biography] 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.
[Go to top] top

Posted by Candido   USA  (78 posts)  [Biography] bio
Date Reply #2 on Sat 10 Jul 2010 09:51 PM (UTC)
Message
Did the trick. Thanks.
[Go to top] top

Posted by Nick Gammon   Australia  (21,321 posts)  [Biography] 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
[Go to top] top

Posted by Nick Gammon   Australia  (21,321 posts)  [Biography] 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
[Go to top] 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.


4,784 views.

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

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

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

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at FutureQuest]