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..


In reply to Reading configuration file with only v5.8 by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.