Search Members Help

» Welcome Guest
[ Log In :: Register ]

Mini-ITX Boards Sale, Fanless BareBones Mini-ITX, Bootable 1G DSL USBs, 533MHz Fanless PC <-- SALE $200 each!
Get The Official Damn Small Linux Book. DSL Market , Great VPS hosting provided by Tektonic
 

[ Track this topic :: Email this topic :: Print this topic ]

reply to topic new topic new poll
Topic: lua - dofile or require ?< Next Oldest | Next Newest >
humpty Offline





Group: Members
Posts: 655
Joined: Sep. 2005
Posted: May 01 2008,13:22 QUOTE

A question for lua users,

Is it better to use 'dofile' or 'require' ?
I'm writing server software with lighttpd  and sqlite and not compiling any modules, just plain script. Any advantages using 'require' in terms of speed or efficiency, cpu time, memory?

All pages requests pass through a main script, so globals play a large part e.g the session ID.

I've Googled a bit, but it's not too clear the differences.
tks in advance.
Back to top
Profile PM 
roberts Offline





Group: Members
Posts: 4983
Joined: Oct. 2003
Posted: May 01 2008,14:06 QUOTE

My understanding of the two has to do with namespaces.

dofile is chunk loading mechanism, that it is generates functions from a "chunk" of load code. This does not take into account any separation of variables into a separate namespace.

require is used for Lua modules. Modules are also "chunks" of Lua code but with a more formal namespace. All new global variables are stored in a namespace table. This table name is the same as the module name. This provides a much cleaner way to create functions without worry of variable name collisions. You would reference your modules functions via module_name.function_name  This mechanism allows for much wider use and sharing of function libraries. Lua Rocks are examples of modules that one would use require.
Back to top
Profile PM WEB 
humpty Offline





Group: Members
Posts: 655
Joined: Sep. 2005
Posted: May 01 2008,23:41 QUOTE

Is it then that a compiled script (luac) becomes a module ?
I notice that I can 'require' a plain script, does that also become
a 'module' once loaded?

I think 'require' might also handle repeated 'requires' much like #ifndef in C, though I think it might do no harm to do 'dofile' s
more than once.
Back to top
Profile PM 
mikshaw Offline





Group: Members
Posts: 4856
Joined: July 2004
Posted: May 02 2008,01:06 QUOTE

Quote
I think it might do no harm to do 'dofile's more than once.
As long as the subsequent scripts do not clobber the previous ones, as Robert said. When you dofile the file is added to the current script and has the ability to break things. As long as all files have unique names you should be ok.


--------------
http://www.tldp.org/LDP/intro-linux/html/index.html
Back to top
Profile PM WEB 
3 replies since May 01 2008,13:22 < Next Oldest | Next Newest >

[ Track this topic :: Email this topic :: Print this topic ]

 
reply to topic new topic new poll
Quick Reply: lua - dofile or require ?

Do you wish to enable your signature for this post?
Do you wish to enable emoticons for this post?
Track this topic
View All Emoticons
View iB Code