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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |