[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: How to upgrade string.gsub() from 5.0.2 to 5.1 (beta)
- From: "Henderson, Michael D" <michael.d.henderson@...>
- Date: Mon, 19 Dec 2005 13:58:58 -0700
I'm updating some 5.0.2 scripts to use the 5.1 (beta) and running into
an issue with string.gsub(). I have seen messages in the list describing
similar issues, but I didn't understand what the base issue was or how
to correct it.
Basically, we're using lua scripts to translate some templates into
batch files. The calling lua scripts set global variables (such as
"withWarning = true;") as needed, then call a translation function
(xgen_TranslateTemplate) that locates the template script, loads it,
runs it, and returns the translated text as a single string. In 5.0.2,
the string returned had all of the %{ lua code }% fragments resolved. In
5.1 (beta), the string returned has none of those fragments resolved.
The output string is exactly the same as the input string.
The translation function is pretty simple:
--======================================================================
=======
--
function xgen_TranslateTemplate (jobType)
local template = xgen_Locate(jobType);
if not template then
error("\n\nerror:\tunable to locate template '" .. jobType ..
"'\n");
end;
io.input(template);
local script = io.read("*all");
return (string.gsub(script, "%%{(.-)}%%", function (x)
local f = loadstring( x );
if (f) then
return f();
else
error("\n\nerror:\tinvalid template expression\n\t -> " .. x);
end;
end));
end;
The template scripts have statements such as the following in them:
Exit%{ if withWarning then return " withWarning"; end; }%