lua-users home
lua-l archive

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


On Thursday 03 February 2011 22:40:55 Geoff Leyland wrote:
> 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")

Thanks Geoff,

I think it does help. So I define my new function print() as taking argument 
str, so obviously when the program is run str is "Hello World". Then I pass 
that str to oldprint(), which is a reference to print(), which has already 
been defined as taking an argument, so no further definition or declaration 
needed. 

Does that about sum it up?

This thing really hurt my head, and now I have another one that I'll ask 
later.

Thanks. I think this cleared it up.

SteveT

Steve Litt
Recession Relief Package
http://www.recession-relief.US
Twitter: http://www.twitter.com/stevelitt