* UNIX 계열 운영체계(예를 들면 리눅스)에서는 빌드할때 config 화일에서 다음부분을 주석해제해주어야 loadlib()함수를 사용할 수 있다.
require 명령을 사용하려면 LUA_PATH 환경변수를 잘 설정해주어야한다. 포멧을 예를 들어 다음과 같이 지정하는데 유의할 것.
루아 코드를 C 호스트 프로그램에서 실행시킬때, lua_dofile()을 사용하지말고, luaL_loadfile()을 사용해라. 전자는 루아코드를 읽어들여 실행하고 종료하지만 후자는 실행하고 다시 C 호스트 프로그램으로 돌아온다.
테이블의 내용을 역순으로 반복실행할 경우 다음과 같이 한다. (이때에는 문자열 인덱스로 지정된 값은 무시된다)
함수를 선언할 때, 인자의 개수를 가변으로 받으려면 ... 연산자를 쓰면 된다. 다음의 예는 ...연산자로 인자를 지정한 함수의 예제이다. 주어진 인자들을 그대로 출력하는 내용이다. (arg는 기본적으로 설정된 전역 테이블이다. 1부터 순서대로 인자를 담고 있고, "n"인덱스에 인자의 개수를 담고 있다) 인자의 개수에 대해 가변으로 대응하는 함수를 만들때 유용하다.
(차후정리)
LOADLIB= -DUSE_DLOPEN=1 DLLIB= -ldl
export LUA_PATH="./?.lua;./?.lc;/usr/local/bin/?/?.lua;/lasttry"
s = {1, 2, 3, 4, 5}
for i = table.setn(s), 1, -1 do print(s[i]) end
function test(...) for i, v in arg do print (i,v) end end위 선언은 다음과 같이 실행된다.
> test(1, 20, "qk", 3030) 1 1 2 20 3 qk 4 3030 n 4
>In Lua4.0 if there was an error in the text passed to lua_dostring an error
>message would be output and control would be returned to the calling
>application. With Lua5.0alpha the program simply exists so the calling
>application can not do anything. Does lua_load have the same behaviour?
lua_load returns an error code and so does lua_pcall.
lua_dostring is now a convenience function and has been moved to lauxlib,
but you probably what luaL_loadbuffer (see below).
lua_load was introduced in 5.0 exactly to handle errors nicely (and also
for flexibility).
>Is this change likely to remain, and if so is there a simple way to execute
>potentially erroneus code without the risk of the program exiting.
Try this (suggested before here in lua-l)
void my_dostring(lua_State *L, const char *cmd)
{
if (luaL_loadbuffer(L, cmd, strlen(cmd), cmd) || lua_pcall(L, 0, 0, 0))
{
fprintf(stderr, "%s\n", lua_tostring(L, -1));
lua_pop(L, 1);
}
}
The point is that if the parsing or the execution fails than you're left with
an error message on top of the stack. The code above simply outputs it to
stderr, but of course you can do whatever is adequate for your application.
--lhf









![[http]](/wiki/imgs/http.png)
