Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.


In reply to Handling different sections in config files by pzbagel

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-26 07:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found