lua-users home
lua-l archive

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

Jeff Wise wrote:

Thank you for your reply.  Unfortunately, I have no clue
what you are telling me.

There are two issues here.  The main one is how to pass
arguments to a file that is executed after loading it with
loadfile.  Here's how to do it, using what's called a vararg

-- Beginning of the file 1.lua.
print("1.lua's arguments are:", ...)
-- End of 1.lua.

-- Beginning of the file 2.lua.
-- This is more verbose than normal to make clear what's
-- going on:
local Func, ErrStr = loadfile("1.lua")
if Func then
 Func("grape", "pear")
 print("Compilation error: " .. ErrStr)
-- End of 2.lua.

Now the command line

 lua 1.lua apple banana

results in

 1.lua's arguments are:  apple   banana

and the command line

 lua 2.lua

results in

 1.lua's arguments are:  grape   pear

For more on vararg expressions, see the manual, particularly

The other issue is this:

An examination of "loadfil4" reveals that the last 3
groups of statements are testing multiple ways to invoke
"dummy2".  It totally escapes me why there is no output
from dummy2 on call #2.  This is confusing to me.

Ignoring some details, a function call in Lua takes one of
these three forms:


(Note that this rule is recursive, in that FUNCTION-CALL
itself must be one of these three.)

Call #1 and call #3 from your previous message are examples
of form 2:

print("Call # 1 --------------------------------")
assert(loadfile(pgm))(arg1, arg2)  --(arg1, arg2))

print("Call # 3 --------------------------------")
assert(loadfile("C:\\lua\\source\\dummy2.lua"))("apple", "banana")

"assert(loadfile(pgm))" is the FUNCTION-CALL and "arg1,
arg2" is the ARGUMENTS.  The function created by loadfile()
is only called after assert() returns it.

Call #2, on the other hand, just loads a file (converting it
to a function) and asserts that the loadfile succeeded; it
never calls the function returned by loadfile:

print("Call # 2 --------------------------------")

To call it with no arguments, you'd want:


For more on this, see the syntax chart in the manual:

Hope that helps,