When coding with Lua, remember to think like a lunatic…

I recently experienced some strange behaviour from a Lua script I wrote to dynamically generate an XML string for a FreeSWITCH directory user, which used the string.format() function to populate a format string with values returned from an ODBC database connection. Being used to Python (and many other scripting languages), I assumed that an empty string would be evaluated to false, if used in a conditional expression. Not so with Lua….

From the Lua 5.1 reference manual:

The condition expression of a control structure can return any value. Both false and nil are considered false. All values different from nil and false are considered true (in particular, the number 0 and the empty string are also true).

You learn something new every day…

PS: No, this is not an April Fools’ joke…

Leave a Reply