Strange Lua parsing problem

Here’s an odd one.

If I use the line:

 if (string.len(incomingPDU) < 3)

it fails the parse check on Luup startup, but if I use:

 if (string.len(incomingPDU) &lt; 3)

It works fine. My code contains other statements such as:

 if (retries > 3)

which work perfectly!

What is it about the less than sign?

It is a known issue. I believe due to xml. You have to use < and not < when writing lua code.

  • Garrett

Thanks

This only turns up if you are embedding Lua code inside an XML file (which might happen if you are sticking the Lua inline in your I_*.xml file). Characters which are special to XML (& <, and under some circumstances > " ') need to be escaped because the XML parser does unescaping before your Lua code is extracted.

Don’t like it? There are three workarounds.

One, use a separate L_*.lua file for your Lua. No escaping necessary.

Two, rewrite x < y as y > x. Greater-than doesn’t need to be escaped; > is only there for symmetry with the necessary <. Lua doesn’t use the ampersand character in programs so you won’t ever need to write &.

Three, use CDATA sections, a little-known part of the XML spec: <!CDATA[[ if (x < y) then luup.log(“foo”) end ]]>

Thanks futzle - very informative. :slight_smile: