lua-users home
lua-l archive

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


Yes, I do it. But the example was wonderful. I've never understood metatables and I think Luiz's message is the principle of all.

I understood we don't have values dettached from tables. Everything belongs from at least one table: _G.

When I inocently do: x = 3, I am doing _G.x = 3.

At first, I could not understand why the anonimous function receved two arguments. But the reason is that __index waits for a table and a ke and, even if we do x = 3, x is a key of the table _G and when I compared t and _G I conclude they are the same.

Using LOOP for oriented programming, I didn't know how two create properties. Now I think I understood. This, I get by means of __index.



----- Original Message ----- From: "HyperHacker" <hyperhacker@gmail.com>
To: "Lua list" <lua@bazar2.conectiva.com.br>
Sent: Saturday, May 29, 2010 2:17 PM
Subject: Re: Getting the value of environment variables


On Sat, May 29, 2010 at 09:27, Luiz Henrique de Figueiredo
<lhf@tecgraf.puc-rio.br> wrote:
Yes, use os.getenv(varname) like os.getenv("windir") or os.getenv("home")
on Linux.

Or automagically with

setmetatable(_G,{__index=function (t,i) return os.getenv(i) end})
print(home)



...Which is a terrible idea, basically emulating PHP's
REGISTER_GLOBALS that has lead to so many security issues over the
years.

Now, you could create a table like os.env or similar for that purpose,
or maybe even modify the index function to use a prefix
("print(os_home)"), but don't go messing with the global metatable
like that (especially without considering what's already in it).

--
Sent from my toaster.