Your config file seems to map quite well to a nested hash table.
You will probaby need 2 kinds of parsers:
1. The simple name = value pair parser, which should be easy to write
with regex
2. The nest {} parser, which you probably can use a recursive function
to parse deeply
The other (easier) way is to analyse if the .ini format will work just
as well. It will look like the following:
[Main]
DEBUG = OFF
THRESHOLD = 4
SERVER = .
USER = BATCH
PASSWORD = PASSWORD
Over Write = OVER WRITE
[BATCH TASK]
TASK NAME = The Task
SLEEP TIME SECOND = 10
This will make your parsing much much easier.
Michael Higgins wrote:
>> Bill Luebkert wrote:
>>
>> Michael Higgins wrote:
>>
>>> Hello, Gurus --
>>>
>>> I'm stuck finding the best approach to parsing a config file.
>>>
>>> Some configuration items are grouped, nested, in matching
>>>
>> braces '{}'. Some
>>
>>> are item = value.
>>>
>>> I need to modify and replace three sections that are in this format
>>> item{.....item{....}}, capturing up to the matched brace.
>>>
>>> Any suggestions?
>>>
>> Sample config file as currently is:
>>
>
> Like this:
>
> DEBUG = OFF
> THRESHOLD = 4
> SERVER = .
> USER = BATCH
> PASSWORD = PASSWORD
> Over Write = OVER WRITE
> BATCH TASK{
> TASK NAME = The Task
> SLEEP TIME{
> SECOND = 10
> }
> }
> . . .
>
> and similar. This first '{' starts nested data structures like above. They
> all match up.
>
>
>> Sample config file that you want to change to instead of
>> brace version:
>>
>>
>
> Actually, I think I want to parse this one into a perl structure, change a
> few items and write it back out mostly unchanged. Right now, I'm pulling the
> bits to change with a bunch of substrs and regexen. It's ugly, but it will
> work. I think I should be able to get something more elegant, like a bunch
> of hash and array refs, automagically. ;-)
>
>
>> Any pertinent rules in detail.
>>
>
> I don't know, exactly. Lines are either 'ITEM = VALUE' or begin a config
> block like 'ITEM{' -- the same rules inside the block. The braces all match
> up but 'ITEM = VALUE' can appear inside the blocks too.
>
> Basically, I thought to use something like Parse::RecDescent or
> Text::Balanced... or something else?
>
> Thanks for any help or suggested approach.
>
> Cheers,
>
> --
> . . . . . .... . . . .. .. . . . .``.
> .`. .`. . . .` . . . . .` .` . .`. . `.
> . ` . . .`. .`` .```. . . .. . .. . . `. `.
> . . . . `. .... . . . `..` `..` . . . `..`
>
>
>
> _______________________________________________
> ActivePerl mailing list
> [email protected]
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
>
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs