lua-users home
lua-l archive

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

> I find a this code .
>   lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2");
> I know that this code means get a value PROMPT("> ") or PROMPT2(">> ") .
> But why does it use underscore [ _ ] front of global names ?

Look at the rest of the function:

  static const char *get_prompt (lua_State *L, int firstline) {
    const char *p;
    lua_getglobal(L, firstline ? "_PROMPT" : "_PROMPT2");
    p = lua_tostring(L, -1);
    if (p == NULL) p = (firstline ? LUA_PROMPT : LUA_PROMPT2);
    lua_pop(L, 1);  /* remove global */
    return p;

It attempts to read the globals _PROMPT or _PROMPT2, and if they don't exist, it uses the defaults defined in C (LUA_PROMPT and LUA_PROMPT2).

This is also defined in the Lua manual (at least in 5.1) [1]:

  The primary prompt is the value of the global variable _PROMPT,
  if this value is a string; otherwise, the default prompt is used.
  Similarly, the secondary prompt is the value of the global variable _PROMPT2.

You can try this easily yourself:

  Lua 5.1.4  Copyright (C) 1994-2008, PUC-Rio
  > _PROMPT="first: "
  first: _PROMPT2="next: "
  first: for i=1,2 do
  next: print(i)
  next: end