my($lookahead); nextline(); while (hasline()) { if ($lookahead=~ /^\s*$/) { nextline(); next; } die "fileformat error: $lookahead" if $lookahead!~ s/^\[([^]]+)\].*/$1/; SWITCH: for ($lookahead){ /^Section1$/ && do { nextline(); section1(); last SWITCH; }; /^Section2$/ && do { nextline(); section2(); last SWITCH; }; do { die "unhandled section: $_" }; } } sub section1 { while (hasline()) { return if $lookahead=~ /^\[/; print "S1: ",$lookahead; nextline(); } } sub section2 { while (hasline()) { return if $lookahead=~ /^\[/; print "S2: ",$lookahead; nextline(); } } sub nextline { $lookahead=; } sub hasline { return defined $lookahead; } __END__ [Section1] var1=one var2=two var3=three [Section2] user1:pass1:50 user2:pass2:51