lua-users home
lua-l archive

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


Aaron,

Thanks for your reply.  I would have never deduced that from reading the
documentation.  I think I have risen to my level of incompetence...

After I replied to you last time, I decided that I should interpret the
ellipsis literally.  I put it in dummy2 and dummy2 started displaying the
parameters I was trying to send it.  Now I have to determine how to identify
those parms discretely.  I will try to do this before asking for help!

Thanks again for the assistance.

-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br
[mailto:lua-bounces@bazar2.conectiva.com.br] On Behalf Of Aaron Brown
Sent: Thursday, September 06, 2007 1:34 PM
To: Lua list
Subject: Re: Subroutine arguments not right yet

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/










CONFIDENTIALITY NOTICE:  This E-mail message and all attachments, which
originated from Sealy Management Company Inc, are intended solely for the
use of the intended recipient or entity and may contain legally privileged
and confidential information.  If the reader of this message is not the
intended recipient, you are hereby notified that any reading, disclosure,
dissemination, distribution, copying or other use of this message is
strictly prohibited.  If you have received this message in error, please
notify the sender of the message immediately and delete this message and all
attachments, including all copies or backups thereof, from your system.  You
may also reach us by phone at 205-391-6000.  Thank you.


-- 
No virus found in this incoming message.
Checked by AVG Free Edition. 
Version: 7.5.485 / Virus Database: 269.13.6/991 - Release Date: 9/5/2007
2:55 PM











CONFIDENTIALITY NOTICE:  This E-mail message and all attachments, which originated from Sealy Management Company Inc, are intended solely for the use of the intended recipient or entity and may contain legally privileged and confidential information.  If the reader of this message is not the intended recipient, you are hereby notified that any reading, disclosure, dissemination, distribution, copying or other use of this message is strictly prohibited.  If you have received this message in error, please notify the sender of the message immediately and delete this message and all attachments, including all copies or backups thereof, from your system.  You may also reach us by phone at 205-391-6000.  Thank you.