lua-users home
lua-l archive

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


Cool, thanks for your help.

The reason I asked is I want to try to mimic "eval" functionality, found in some other scripting languages, such as perl.

Lua has loadstring, but this operates in the global environment, and afaik/remember, perl puts more context into its eval.

Interestingly, using the debug library helps me accomplish this goal ... but according to the advise here, and in the lua manual, using debug functionality for non-debugging purposes isn't the wisest decision, which makes sense.

All I'm really going for here is tighter syntax, as I'm trying to adapt lua into a sort of higher level language to suit our purposes.

For instance, we have a trigger object, which will fire when a certain event occurs.  One specialization of the trigger is a conditional version, which fires when a particular condition is true.  Creating a conditional trigger looks something like this:

TCond(function() return a > b end)

but I'd like to shorten it to:

TCond[[a > b]]

What I'd ultimately like to do is have that condition specified as a field in a high level tool, possibly with a user interface. 

But I'm running into trouble, because loadstring can't access local variables 'a' and 'b' in the environment.

Anyone know a way of accomplishing this?

> Is it possible to get an environment with local variables present too?
>
> I'm trying:
>
> function flarp()
> ??? local myLocal = "hello"???
> ??? local myEnv = getfenv()
> ??? print(myEnv.myLocal) -- (prints nil)
> end
> flarp()
>
>
> ... but myEnv.myLocal is still nil.? Anyone know a way of doing this??
> Or is it impossible?

You can only do it with the debug library, which is not recommended for
production code (as well as being quite slow, and somewhat unreliable
since it won't work if the compiled code has been stripped.)