This is not the nastiest perl I’ve ever done…but dang.

This is a stanza where, given a comma separated list of keywords to match on, find the values after the colon. The values could be empty and at the end of a line. I would be delighted to find an even simpler way to do this.

   my $matcher       = " (".join('|', keys %option_map)."):";
   my @matches       = grep( /$matcher/, @lines);
   for my $match (@matches) {
      my @parts      = ($match =~ m/( *[^ ]+):( \S+ [^ #]*)(?! #|\S+:)/g);
      for (my $i=0; $i < @parts; $i+=2) {
         $option     = $parts[$i];
         $option     =~ s/^\s*(.*)\s*$/$1/;
         if ( defined $option_map{ $option } ) {
            my $value = $parts[ $i + 1 ];
            $value   =~ s/^\s*(.*)\s*$/$1/;
            $option_map{ $option } = $value;
         }
      }
   }

And that’s pretty darned terse for that solution.

Advertisements