Viren
Viren

Reputation: 5962

Why do I not see print inside my lua function

My question has multiple parts but firstly here is my sample lua code (test.lua):

local traceback = debug.traceback

local inspect = require('inspect')

local foo = "function nop(); print('this is war'); return true; end"

local f = loadstring(foo)

local result = f()

print(result)

local status, val= xpcall(function () return f() end,debug.traceback)

print('status .. ' .. tostring(status))

print(val)

So,

  1. When I run local result = f(). I still see the result value as nil

  2. When the function f is executed. Why don't I see the print o/p .

Upvotes: 3

Views: 285

Answers (1)

Etan Reisner
Etan Reisner

Reputation: 80921

The chunk of code in foo which you load into f doesn't return any values.

It defines a function but that's it.

If you want to return the function from the chunk when it is run you need to add return nop to the end of the string.

Upvotes: 4

Related Questions