lua-users home
lua-l archive

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


On Dec 2, 2013, at 5:13 AM, Graham Wakefield <wakefield@mat.ucsb.edu> wrote:

> I may be missing something, but a routine to print all globals is 
> 
> for k, v in pairs(_G) do
> 	print(k, v)
> 	-- insert recursion if desired here… 
> end
> 
Yes but I wanted to print only globals I had defined.  pairs(_G) will give all globals including the loaded libraries.  Using the table strict builds give only the globals declared after strict was run.




> (Obviously a fancier printer would memoize, handle loops, trace metatables, format prettily etc… but my point is that strict.lua isn't required to find globals.)
> 
> On Oct 31, 2013, at 10:09 AM, Jose Torre-Bueno <jtorrebueno@cox.net> wrote:
> 
>> On Oct 30, 2013, at 5:06 PM, Paige DePol <lual@serfnet.org> wrote:
>> 
>>> 
>>> I patched the parser and added a 'global' keyword for the explicit declaration of global variables, though instead of storing my table in _G I stored it in the registry.
>>> 
>>> -Paige
>> 
>> Given that strict had created the __declared table inside _G I was able to make a function to list all globals like so:
>> 
>> function listglobals()
>> 	local t = getmetatable(_G)
>> 	for i,_ in pairs(t.__declared) do
>> 		local ty = type(_G[i])
>> 		local sz = 0
>> 		if ty == 'table' then sz = #_G[i] end
>> 		printf('%20s,	%10s,	%4d',i, ty, sz)
>> 	end
>> end
>> 
>> Its remarkable how easy it is to do things like this in Lua.
>> 
>> 
>> 
>> Jose de la Torre-Bueno, Ph.D.
>> Empowered Energy Solutions Inc.
>> Intellectual Property & Technology Management
>> T (619) 977-0553
>> F (760) 295-7119
>> jtorrebueno@cox.net
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> 
> 
>