lua-users home
lua-l archive

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


I'm looking at Listing 23.1 on page 209 in PiL, 2nd edition. I wrote the test program below. The program works the way I expect it to, except that it does not list the values of TESTUP1 and TESTUP2. What am I missing?


---------START-------------------------------
local TESTUP1 = {}
local TESTUP2 = "up"
TESTGLOBAL = "global"

local function put(name, value)
  if type(value) == "string" then
    print("\t"..name..'="'..value..'"')
  else
    print("\t"..name..'='..tostring(value))
  end
end

local function test(arg)
  local a, b = 1, 2
  local testvar = "local"

  print "Local variables:"
  for i = 1, math.huge do
    local local_name, local_value = debug.getlocal(1, i)
    if not local_name then break end
    put(local_name, local_value)
  end

  print "\nNon-local variables:"
  for i = 1, math.huge do
    local nonlocal_name, nonlocal_value = debug.getupvalue(test, i)
    if not nonlocal_name then break end
    put(nonlocal_name, nonlocal_value)
  end

  print "\nGlobal variables:"
  for global_name, global_value in pairs(getfenv(test)) do
    put(global_name, global_value)
  end
end

test()
---------END---------------------------------

Here's the output:

Local variables:
	arg=nil
	a=1
	b=2
	testvar="local"
	(for index)=5
	(for limit)=inf
	(for step)=1
	i=8

Non-local variables:
	put=function: 0x8a84e20
	test=function: 0x8a84d30

Global variables:
	string=table: 0x8a80910
	xpcall=function: 0x8a80318
	package=table: 0x8a80d98
	tostring=function: 0x8a80270
	print=function: 0x8a80388
	os=table: 0x8a82470
	unpack=function: 0x8a802e0
	require=function: 0x8a81298
	getfenv=function: 0x8a7ffb0
	setmetatable=function: 0x8a801f8
	next=function: 0x8a800d0
	assert=function: 0x8a7ff00
	tonumber=function: 0x8a80238
	io=table: 0x8a81d30
	rawequal=function: 0x8a803c0
	collectgarbage=function: 0x8a7ff38
	arg=table: 0x8a83e98
	getmetatable=function: 0x8a80148
	module=function: 0x8a80ff0
	rawset=function: 0x8a80430
	TESTGLOBAL="global"
	math=table: 0x8a83038
	debug=table: 0x8a83a38
	pcall=function: 0x8a80108
	table=table: 0x8a80468
	newproxy=function: 0x8a80a18
	type=function: 0x8a802a8
	coroutine=table: 0x8a80a80
	_G=table: 0x8a7f450
	select=function: 0x8a7f478
	gcinfo=function: 0x8a7ff78
	pairs=function: 0x8a7fa00
	rawget=function: 0x8a803f8
	loadstring=function: 0x8a80098
	ipairs=function: 0x8a7f9a0
	_VERSION="Lua 5.1"
	dofile=function: 0x8a7fff0
	setfenv=function: 0x8a801c0
	load=function: 0x8a80060
	error=function: 0x8a80028
	loadfile=function: 0x8a80188