executing dostring on 'local' level

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

executing dostring on 'local' level

Roman Vanicek
Hi all,

is there a way do execute dostring in Lua on the 'local' level? The example
goes like this:

a = 0; -- global 'a'

function test()
  local a = 0; -- local 'a'
  dostring("a=1;"); -- now dostring will set global 'a' to 1 just as there
were no local 'a'
  print(a); -- this will print 0, since local 'a' has not been touched
end;

test();
print(a); -- this will print 1, since dostring did set the global 'a'

Maybe this behavior is intentional, but in this case I would feel good if
someone would explain to me why it is so.


Roman Vanicek
[hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: executing dostring on 'local' level

Luiz Henrique de Figueiredo
>is there a way do execute dostring in Lua on the 'local' level?

No.

>Maybe this behavior is intentional, but in this case I would feel good if
>someone would explain to me why it is so.

dostring runs a chunk. Chunks run in the global level. It'd be hard to have
them run in a local scope. Why do you want to do this?
--lhf