# perl -slw use strict; use re 'eval'; use Data::Dumper; use vars qw[%Config]; # A global hash to contain the data my $re_pair = qr{ # key=value [[#;]comment text\n] (?{ our ($key, $value, $comment)=() }) # local vars (\w+) (?{ $key = $^N }) # Capture & save the keyname = (\w+) (?{ $value = $^N }) # Capture & save the value (?: \s+ # obligatory whitespace [#;] # oblig. comment card - either or ([^\n]+) (?{ $comment = $^N }) # Capture & save the comment \n )? # zero or one \s* # If we got here, we have key/name comment?, so save them (?{ $Config{$section}{$key} = [ $value, $comment ] }) }x; my $re_section = qr{ # consist of [label] pairs* (?{ our ($section)=undef }) # Init the section label [[] ([^]]+) []] # Capture the label # Save it. Create a key to hold the pairs (?{ $section = $^N; $Config{$section} = undef }) # zero or more pair sounded by optional while space. \s* $re_pair* \s* }x; my $re_file = qr{ # A file consists of 1 or more sections $re_section+ }x; my $data = do{ local $/; } . $/; # slurp the file $data =~ m[$re_file]; # parse it print Dumper \%Config; # Use it __DATA__ [Section1] key1=value1 key2=value2 #comment key3=value3 [Section2] key4=value4 key5=value5 ;Comment2 [Section3]