Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

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

This is the classic parser look-ahead problem that gave rise to the C ungetch() function.

In this case, you would be better of using perl's nouce to read your file in paragraph mode (see perlvar:$/). Basically, if you set $/='', perl will read a paragraph (defined as a number of lines terminated by \n\n). You can then pass the entire section in to the appropriate sub, have it split it into lines and do it thing from there. Eg.

#! perl -slw use strict; sub section1{ # Split into lines my @lines = split'\n', $_[0]; print 'Section 1 got:', $_ for @lines; } sub section2{ print 'Section2', @_; } # placeholder code. sub getconf{ local $/= ''; # Set paragraph mode on *locally* while( <DATA> ) { section1( $_ ), next if /^\[Section1]/; section2( $_ ), next if /^\[Section2]/; } } getconf(); __DATA__ [Section1] var1=one var2=two var3=three [Section2] user1:pass1:50 user2:pass2:51

Output

Section 1 got:[Section1] Section 1 got:var1=one Section 1 got:var2=two Section 1 got:var3=three Section2[Section2] user1:pass1:50 user2:pass2:51

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller

In reply to Re: Handling different sections in config files by BrowserUk
in thread 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 rifling through the Monastery: (3)
As of 2024-04-19 21:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found