lua-users home
lua-l archive

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


On Fri, Nov 8, 2013 at 8:29 AM, Marc Lepage <mlepage@antimeta.com> wrote:

> I want to read in the file, manipulate some of the members (like name, val),
> and then write it out again. But I want to preserve the functions (onA, onB)
> as they are.
>
> Any tips for how to do this?
>
> I guess I can easily write out the data structures and known members by just
> writing them out (since I know them) and then try to grab the raw text of
> the functions from the original input file and write that out in the
> appropriate place. Is that a good idea? Maybe the debug library can help?

I may be misunderstanding what you're after, but if you rewrite your
script so that each part you want to keep unchanged is assigned to its
own variable in long string format, e.g.,

local bas = [[
onA = function(x)
            return doSomething(x)
        end]]

... in Lua 5.2 you can execute the value stored in bas using load.

And you'd still have the variable name and its value to concatenate
into the string for the new file unchanged.

I'm not certain how to do this in Lua 5.1; I recall that load's
behavior was changed somewhat in 5.2.

Paul

-- 
[Notice not included in the above original message:  The U.S. National
Security Agency neither confirms nor denies that it intercepted this
message.]