разбор ini-файла
Oct. 20th, 2011 04:42 pmПростейший ini-файл:
Или в виде функции:
# Comment
Param1 = Value1
[Section1]
Param2 = Value2
LongParam = Very \
Long \
Param
[Section2]
Param3 = Value3
# perl -MData::Dumper -0777 -lne 's#\#.*?\n##gm; s#\\\n\s+##gsm; my $sect = "__DEFAULT__";
while (m#(?:(?\w+)\s*=\s*(?.+?)\s+$|\[(?\w+)\])#gm) {
$sect = $+{sect} || $sect; $hash{$sect}->{$+{name}} = $+{value} if exists $+{name}
} print Dumper \%hash' /tmp/a.ini
$VAR1 = {
'Section1' => {
'LongParam' => 'Very Long Param'
},
'Section2' => {
'Param3' => 'Value3'
},
'__DEFAULT__' => {
'Param1' => 'Value1'
}
};
#
Или в виде функции:
sub ini ($) {
local $_ = do { local $/; open F, $_[0] and };
s#\#.*?\n##gm; s#\\\n\s+##gsm;
my $sect = "__DEFAULT__";
my %hash;
while (m#(?:(?\w+)\s*=\s*(?.+?)\s+$|\[(?\w+)\])#gm) {
$sect = $+{sect} || $sect;
$hash{$sect}->{$+{name}} = $+{value} if exists $+{name}
}
return \%hash;
}