[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Subroutine arguments not right yet
- From: "Aaron Brown" <arundelo@...>
- Date: Thu, 6 Sep 2007 14:33:57 -0400
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
expression:
-- 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")
else
print("Compilation error: " .. ErrStr)
end
-- 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
http://www.lua.org/manual/5.1/manual.html#2.5.9
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:
form 1: VARIABLE(ARGUMENTS)
form 2: FUNCTION-CALL(ARGUMENTS)
form 3: (EXPRESSION)(ARGUMENTS)
(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 --------------------------------")
assert(loadfile("C:\\lua\\source\\dummy2.lua"))
To call it with no arguments, you'd want:
assert(loadfile("C:\\lua\\source\\dummy2.lua"))()
For more on this, see the syntax chart in the manual:
http://www.lua.org/manual/5.1/manual.html#8
Hope that helps,
--
Aaron
http://arundelo.com/