lua-users home
lua-l archive

Re: is setfenv useless?

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


On Mar 05, 2006, at 16:15, mr. Aleph wrote:
While there are no possibility to clone functions,
local aFunction = function( aName )
 return ( "%s! Yes, we can!" ):format( aName )
end
local aFunctionCopy = loadstring( string.dump( aFunction ) )
print( aFunction )
print( aFunction( "Bob" ) )
print( aFunctionCopy, aFunction == aFunctionCopy )
print( aFunctionCopy( "Bob" ) )
print( aFunctionCopy( "Bob" ) )
> function: 0x119350
> Bob! Yes, we can!
> function: 0x119520 false
> Bob! Yes, we can!
 setfenv (debug.setfenv) remains almost useless.
Perhaps esoteric, but not useless. Anytime you use loadstring, setfenv will come handy. setfenv is also a useful way to 'mark' functions for various devious purpose.
Cheers
--
PA, Onnay Equitursay
http://alt.textdrive.com/

AltStyle によって変換されたページ (->オリジナル) /