use strict; use Data::Dumper; my %hash; my $section = '__none__'; while () { next if /^#/; chomp; if ( /^\[([^\]]+)\]$/ ) { $section = $1; } elsif ( /^([^=]+?)\s*=\s*(.*)$/ ) { $hash{$section}{$1} = $2; } } die Dumper( \%hash ); __DATA__ # This is a configuration file. Section = none [TITLE] Name = John Location = USA [HEAD] Name = James Location = Canada [BODY] Name = Mayer Location = UK #### $VAR1 = { 'TITLE' => { 'Location' => 'USA', 'Name' => 'John' }, 'BODY' => { 'Location' => 'UK', 'Name' => 'Mayer' }, '__none__' => { 'Section' => 'none' }, 'HEAD' => { 'Location' => 'Canada', 'Name' => 'James' } };