lua-users home
lua-l archive

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


I made the following incorrect test script for Lua 4.0:
v = getenv("TestVar")
if v then
	f = openfile("GetEnv.txt")
	write(f, v)
	closefile(f)
end

I accidently ran it with Lua 5.0. I believe the compatibility library kicked
in (ie. I have set LUA_INIT to @F:\Languages\lua-5.0-work0\test\compat.lua),
but the result was a rather strange report:

F:\Languages\AutoIt> lua50 -c -f GetEnv.lua
F:\Languages\AutoIt\GetEnv.lua:1551124847: bad argument #2 to `?' (string
expected, got nil)
stack traceback:
   1-  [C]:[C]
   2-  F:\Languages\AutoIt\GetEnv.lua:1551124847: in main chunk

Note the unlikely line number and the stack trace...

Running it with Lua 4.0 gave the clue with a better error message (except I
prefer 'nil' to 'no value', say better dump):

F:\Languages\AutoIt> lua40 -c -f GetEnv.lua
error: bad argument #2 to `openfile' (string expected, got no value)
stack traceback:
   1:  function `openfile' [C]
   2:  main of file `GetEnv.lua' at line 3

I corrected the program by adding 'w' to openfile and it worked fine in both
versions.

I rewrote it to Lua 5.0 syntax (with deliberate same error):

v = os.getenv("SciTE_Home")
if v then
	f = io.open("GetEnv.txt")
	f:write(v)
	f:close(f)
end

If GetEnv.txt doesn't exists, I get the error:

F:\Languages\AutoIt> GetEnv.lua
F:\Languages\AutoIt\GetEnv.lua:4: attempt to index global `f' (a nil value)
stack traceback:
   1-  F:\Languages\AutoIt\GetEnv.lua:4: in main chunk

which is logical, since Lua 5.0 defaults to read mode if none is given, so
the open fails and returns nil.

If GetEnv.txt exists, my program does nothing. Perhaps the io library should
check the mode of opening and issue an error if the wrong function is
called. Or not. :-)

Regards.

-- 
--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--
Philippe Lhoste (Paris -- France)
Professional programmer and amateur artist
http://jove.prohosting.com/~philho/
--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--=#=--

GMX - Die Kommunikationsplattform im Internet.
http://www.gmx.net