global versus local in plugin

Does anything in a plugin need to be global? I have a simple plugin that does a call_delay and has a few actions. Nothing beyond that. When defining the functions and variables, does anything need to be global? Or should everything be local? It’s not clear to me if the call_delay needs any special handling, or the startup/initialization functions and other action functions need to be global.

Side question: when call an lua function with no variables, do you need the “()”? I’ve seen the startup/initialization in the implementation XML without the parentheses, but it looks a little odd to me.

thanks for any information on these.

Yes. Any of the callback functions require the name of a global function which will handle the callback. This is true for call_delay(), call_timer(), register_handler(), job_watch(), variable_watch(), …

Otherwise, make everything local.

Side question: when call an lua function with no variables, do you need the "()"? I've seen the startup/initialization in the implementation XML without the parentheses, but it looks a little odd to me.

What’s in the XML is just a name, it’s not the actual call to the function, it’s not Lua.

Yes, you always need the (), unless you are using " ", ’ ', or [[ ]] for string parameters, or {} for tables:

print "hello"
print 'there'
print [[yes, this works]]
local x = table.concat {1,2,3}
print (x)

Ah, okay. But is there a difference between the XML startup tag, and the run tag inside an action? The latter appears to be true lua, correct?

The reason I’m asking is that I want to place the action code in a L_xxx.lua file so I am planning on placing a function call (only) in the I_xxx.xml file and have the body of the function in the L file. So I think I need it as “myFunc()” as the call in the run tag. Is that right? Thanks.

Yes, the action is Lua, and you are making a GOOD CHOICE in deciding to put most of your code in a .lua file. Easier to edit, easier to debug.

You may actually need to pass some of the parameters on to your run function.

Can you elaborate on this? The action lua doesn’t have access to everything defined in the rest of the file and startup?

The Lua code in the , , … tags is called as a function with specific named parameters.

See here: http://wiki.micasaverde.com/index.php/Luup_Declarations

You may want to pass these on to your own functions in the .lua file.