pzbagel has asked for the wisdom of the Perl Monks concerning the following question:
I have a configuration file which is broken down into sections, each section is different and gets parsed by a different sub. All these subs are called from a general configuration parsing sub(&getconf). My problem is that the section-specific sub is the one that reads the header line for the next section and so the case statement in the parent sub never gets to read that input from the filehandle and can't figure out which sub to call to parse the next section of the file..
Simplified it looks like this:
sub getconf { while(<CONF>) { chomp; CASE: { /^\[Section1\]$/ && do{&parse_section1(\*CONF); last CASE;}; # More calls like the above }; } } sub parse_section1 { $CONF=shift; while(<$CONF>) { if (/^\[.*\]$/) { #Oops, we reached another section! #Now the next section delimiter is in $_ #Can I put it back in <$CONF>? return; } #Otherwise do something with the input } }
Now my input files look like this:
[Section1] var1=one var2=two var3=three [Section2] user1:pass1:50 user2:pass2:51
My initial idea is to somehow put $_ back into the filehandle in the parse_section1() sub before returning so that the parent sub can read it itself. I could swear I saw that described somewhere but searching several perl resources (on|off)-line didn't lead anywhere. Is this something that can be done?
If not, does anyone see a perlish solution to this problem. I know I can check a variable in getconf or something and return the line from parse_section1() to that variable, but I was wondering if there is something a little less mundane out there.
Thanks in advance.
Peter
P.S. please excuse any minor syntax errors in my code, I wanted to trim it down to just the essentials to describe the problem I have.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Handling different sections in config files
by BrowserUk (Patriarch) on May 15, 2003 at 04:11 UTC | |
Re: Handling different sections in config files
by Skeeve (Parson) on May 15, 2003 at 06:56 UTC | |
Re: Handling different sections in config files
by CukiMnstr (Deacon) on May 15, 2003 at 04:38 UTC | |
Re: Handling different sections in config files
by hacker (Priest) on May 15, 2003 at 14:04 UTC | |
Re: Handling different sections in config files
by Nitrox (Chaplain) on May 15, 2003 at 12:26 UTC | |
Re: Handling different sections in config files
by pzbagel (Chaplain) on May 15, 2003 at 06:58 UTC |