SPUG: parsing a config file

Todd Wells toddw at wrq.com
Fri Jan 7 15:37:58 CST 2000

Hello, I have a regular expression question for you gurus.  I haven't done
any Perl programming in a couple months and my skills are showing it.  

I would like to parse variables from a data file, and the data file is
created by another app, so I can't change the file format.  Accurately
parsing this file is of utmost importance.  At the bottom of this mail, you
can see the format.

I'd love to have a routine that could parse these settings more easily.
Right now I'm sure I'm doing this the hard way, and I'd like to be more

I'm currently reading all the lines of the file into the array @lines.  Then
I parse as such (partially complete example):

        for ($i = 0; $i < @lines; $i++)
                if ($lines[$i] =~ /Domain =/) 
                        $domain_servers = $lines[++$i] =~ 
                                s/servers = (.*);/$1/;
                        $domain_status = $lines[++$i] =~
                                s/status = (.*);/$1/;

Note I've avoided using a foreach because within the routine I want to look
ahead at the next line(s).  I'm sure there's a magic Perl variable that will
tell me which array position I'm at during a foreach, but I couldn't figure
out what the correct variable is.  Forgive that the formatting in this email
didn't come through as cleanly as I would've liked.

I know Perl will let me parse easier than this, but my regular expression
skills are pretty rusty.  It'd be great if it could auto-name the variables
based on what's on the left side of the equals sign, but I could hard-code
variable names if necessary. The other thing I'm not sure how to deal with
are the nested brackets -- for instance, see that the Restart section is
enclosed by {} but it also has a nested {} inside of it.  And I don't need
variables called "Domain" or "Restart" (which are actually section names),
but it'd be great if it could set the variables enclosed in that section.

I'd like this to be more flexible than the way I've started to do it here,
because it may be that unanticipated variables could pop up and my simple
line incrementing may get out-of-synch with the way the config file is
actually formatted.

- Todd

Here's the general config file format:

	Domain = {
		servers = ();
		status = Enabled;
	Log = {
		easLogLevel = Standard;
		log = On;
	Restart = {
		Condition = 0;
		Enabled = NO;
		RestartTime = {
			CalenderFormat = "%d/%m/%Y %I:%M %p";
			FromTime = "18/07/1997 03:00 AM";
			ToTime = "18/07/1997 06:00 AM";
		Schedule = 0;
	Security = {
		administrator = Administrators;
		domain = MINE;
		manager = Users;
		security = Off;
		user = Users;

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    POST TO: spug-list at pm.org        PROBLEMS: owner-spug-list at pm.org
 Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
 SUBSCRIBE/UNSUBSCRIBE: Replace ACTION below by subscribe or unsubscribe
        Email to majordomo at pm.org: ACTION spug-list your_address

More information about the spug-list mailing list