lua-users home
lua-l archive

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


In Lua 5 you use loadfile and xpcall.

The loadfile function loads a file but doesn't actually execute it,
rather it returns a function that executes the contents of a file. 

xpcall is a "protected call" function. I haven't used it much so you'll
want to check it in the manual. It returns true if the call succeeds and
false if it doesn't as its first argument.

Assuming you have a file called "test.lua" that causes and error when
loaded, this code will print out that error information:

  do
    local f = loadfile("test.lua")
    local msg
    xpcall(f, function(m) msg = m end)
    print(msg)
  end

You can capture the return value from xpcall to see if there was an
error, or just check to see if msg is nil or contains an error message.

  - Tom
>>> gdjose@yahoo.es 04/06/03 04:45 AM >>>
Hi, i'm newbie in LUA and it's my first post.
I need to get the returned error string (if error) from a dofile 
call, something like this.

err = lua_dofile("test.lua");

Anyone can help me?
Thanks in advance.