in reply to Lazy config files?

If you don't find a module that fits the bill, this might.

#! perl -slw package Tie::Hash::Config; use Carp; use Data::Dumper; use Tie::Hash; my %fhs; sub TIEHASH { die "Usage: tie my \%config, '\$class', filename or handle" unless @_ == 2; my( $class, $config ) = @_; open $config, '<', $config or die "Can't open $config: $!" unless ref $config eq 'GLOB'; my $self = bless {}, $class; $fhs{ $self } = $config; return $self; } sub FETCH{ my( $self, $key ) = @_; return $self->{ $key } if exists $self->{ $key }; my $fh = $fhs{ $self }; seek $fh, 0, 0; local $/ = ''; # paragraph mode my %section; while( <$fh> ) { m/ ^ \[ ( [^]]+ ) \] \s* /gcsx and $1 eq $key or next; ($section{ $1 } = $2) =~ s[\s*$][]s while m[ ^ ( [^=]+? ) \s* = \s* ( .+? ) (?= \Z | (?: ^ [^=\s]+ \s* = ) ) ]smxg; } return $self->{ $key } = \%section; } return 1 if caller; package main; use Data::Dumper; scalar <DATA>; seek DATA, 0, 0; tie my %config, 'Tie::Hash::Config', \*DATA; print Dumper $config{ "section$_" } for 1 .. 4; __DATA__ [section1] key1 = value1 key2 = value 2 key3 = value 3 [section2] key1 = value1 key2 = value2 some more stuff here key3 = value3 [section3] key1 = value1 key2 = value2 key3 = This is a whole paragraph of value the only restriction is that it mustn't contain an equals sign (=) surrounded by whitespace. [section4] key 1 = keys can contain spaces too. key2=value2 key3= =

To try it out, download the code and run it as a script or put it into site\Tie\Hash\Config.pm and use it from your own script as

use Tie::Hash::Config; tie my %config, 'Tie::Hash::Config', 'your.cfg'; print $config{section}{key}; # or my $section = $config{section}; print "$key => $value\n" while my($key, $value) = each %( $section }; # etc

Sections are loaded on demand.

If it is of use and interest, I could clean it up and document it for posting.


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
If I understand your problem, I can solve it! Of course, the same can be said for you.