ocehb: (Default)
[personal profile] ocehb
Простейший 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;
}

Date: 2011-10-20 07:46 pm (UTC)
avysk: (Default)
From: [personal profile] avysk
Традиционно с багами:

$ 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
Sequence (?\...) not recognized in regex; marked by <-- HERE in m/(?:(?\ <-- HERE w+)\s*=\s*(?.+?)\s+$|\[(?\w+)\])/ at -e line 2.

Profile

ocehb: (Default)
ocehb

January 2021

S M T W T F S
     12
345 6789
10111213141516
17181920212223
24252627282930
31      

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Feb. 6th, 2026 10:54 am
Powered by Dreamwidth Studios