You might want to consider something like below (you can test online at http://www.lua.org/cgi-bin/demo):
bar = {}
bar.name = 'test'
bar['123.com'] = function(self) print(self.name) end
bar2 = setmetatable({}, {
__index = function(t, key)
return function(...)
return bar[key](bar, ...)
end
end
})
bar2['123.com']()
-- output: test
You can also change bar itself to behave in a similar way, only you must do it before you assign any values - e.g.:
bar = {}
local privatekey = {}
setmetatable(bar, {
__index = function(t, key)
local value = rawget(t, privatekey)[key]
if type(value) ~= 'function' then
return value
end
return function(...)
return value(t, ...)
end
end,
__newindex = function(t, key, value)
rawset(t, privatekey, rawget(t, privatekey) or {})
rawget(t, privatekey)[key] = value
end
})
bar.name = 'test'
bar['123.com'] = function(self) print(self.name) end
bar['123.com']()
-- output: test
bar['x']rather thanbar.x). I doubt the parser will ever be extended to handle this case.object[selector](object)anyway. In what other scenario would you want a method named "123.com"?table[selector]anyhoo. *shrug* In any case, I'm a huge fan of Lua and try to use it for everything I can, but if you have a web scraping chore I'd highly recommend checking out Mechanize (Perl, Python, Ruby, perhaps others) which is like having a GUI-less programmable browser with a queryable DOM. Make stuff like this a breeze. I wish Lua was part of the club that has all this library support, but alas (I blame it's module system >.>).