Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm looking to read a configuration file with a bare installation of perl 5.8.

Whilst installing modules isn't 100% out of the question I would much rather not do so. The operating system is specifically GNU/Linux, and no other platform is going to be required ever. (I know this because the operations that the perl script manages make no sense under none-Linux platforms.)

I have code to parse a configuration file with the basic 'key = value' format, but now I need to have multiple values for each key so I am looking at alternatives. I see several configuration file parsers available via CPAN but I'm trying to keep to core perl. (Some my ask why and tell me to just install the module - that is a valid response, and I can see the motivation behind it.)

My current code looks something like this:

=head2 readConfig Read the configuration file specified, return a hash of the options read. =cut sub readConfig { my ($file) = ( @_ ); my %CONFIG; open( FILE, "<", $file ) or die "Cannot read config file '$file' - + $!"; while (defined(my $line = <FILE>) ) { chomp $line; # Skip comments next if ( $line =~ /^([ \t]*)\#/ ); # Skip blank lines next if ( length( $line ) < 1 ); # Strip trailing comments. if ( $line =~ /(.*)\#(.*)/ ) { $line = $1; } # Find variable settings if ( $line =~ /([^=]+)=([^\n]+)/ ) { my $key = $1; my $val = $2; # Strip leading and trailing whitespace. $key =~ s/^\s+//; $key =~ s/\s+$//; $val =~ s/^\s+//; $val =~ s/\s+$//; # Store value. $CONFIG{ $key } = $val; } } close( FILE ); return( %CONFIG ); }

Is there anything in the perl core which will save me the job? So far I tried running 'locate -i config | grep pm' with no joy..

The obvious solution I can think about is storing an array reference in the hash..

Replies are listed 'Best First'.
Re: Reading configuration file with only v5.8
by GrandFather (Saint) on Dec 22, 2005 at 01:44 UTC

    Depends on what you want. If you just want to save and restore configuration then Storable is core and works well for that job.


    DWIM is Perl's answer to Gödel
Re: Reading configuration file with only v5.8
by Tanktalus (Canon) on Dec 22, 2005 at 03:25 UTC

    Um ... I'll bite. Why can't you install anything from CPAN?

    while (<FILE>) { chomp; s/^\s+//; s/#.*//; s/\s+$//; next unless length; if (/^(.*?\S)\s*=\s*(.*)$/) { push @{$CONFIG{$key}}, $val; } }

    The only thing that makes sense to me is storing an array reference in the hash if you're going to do it by hand. I took the liberty of making a few minor changes.

Re: Reading configuration file with only v5.8
by salva (Canon) on Dec 22, 2005 at 09:29 UTC
    well, you can write your configuration file in Perl and just do "./myapprc" to read it.

    And you can even use Safe to limit what can be done inside it.

Re: Reading configuration file with only v5.8
by Mandrake (Chaplain) on Dec 22, 2005 at 06:18 UTC
    Config::ApacheFormat is a good bet if you ever feel to go for a CPAN configuration parser module
    Alternate way would be storing array reference in a hash as you said.
    Thanks
      I have used Config::ApacheFormat in a recent project and the experience was not completely positive. In my opinion its API is too simplistic and incomplete, focused in solving the common cases and if you want to do something no so common, you can't.

      For instance, I wanted to define which keywords and blocks were allowed inside each block (as inside <Foo> only Bar and Doz are allowed) but it was not supported and that was important to generate meaningful error reports.

      Or I wanted to set the duplicate_directives attribute by directive but I couldn't because it is global.