in reply to Handling different sections in config files
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
|
|---|