lua-users home
lua-l archive

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


On 4/02/2011, at 4:30 PM, Steve Litt wrote:

> Hi all,
> 
> Why does this work?
> 
> #!/usr/bin/lua
> 
> do
> 	local oldprint = print
> 	print = function(str)
> 		oldprint("======================================")
> 		oldprint(str)
> 		oldprint("======================================")
> 	end
> end
> 
> print("Hello World")
> os.exit(0)
> 
> I understand everything except the magical working of argument str. How does 
> the oldprint(str) between the lines of equal know that the str arg in the 
> function declaration refers to the argument of the original print()? Looks 
> like magic to me, and I'm going to have to explain it?

Does this help?

local oldprint = print
function newprint(str)
  oldprint("======================================")
  oldprint(str)
  oldprint("======================================")
end

newprint("Hello World")

anyprint = newprint

anyprint("Hello World")

print = newprint

print("Hello World")