lua-users home
lua-l archive

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


On Fri, May 6, 2011 at 9:12 AM, Bernd Eggink <monoped@sudrala.de> wrote:
> Hi all,
>
> I'm new to Lua and presently trying out some things. I'm especially
> interested in indirect access to variables and functions. No problems with
> variables, but the following puzzles me:
>
> --------------------------------------------------
> function afunc(x)
>    print("afunc(" .. tostring(x) .. ") called")
>    return x
> end
>
> function indirect(str)
>    local result = loadstring(str)()
>    print(tostring(result))
> end
>
> x = 42
> indirect("afunc(x)")
> --------------------------------------------------
>
> The output is:
>
> afunc(42) called
> nil
>
> Can anybody explain why the output is nil instead of 42?

You aren't returning the result of your call to afunc(x). Try this instead:

indirect("return afunc(x)")

-- 
- Patrick Donnelly