This snippet of code will read file and ignore all lines that start with '#' (ignore comment lines)
open(RFILE, "< $filename") or die "Error open $filename: $!\n";
while (<RFILE>) {
chomp; #remove end of line
s/^#.*//; #no leading '#'
s/\s+#.*//; #no multiple white space end with '#'
s/^\s+//; #no leading white space
s/\s+$//; #no trailing white space
next unless length;
my ($key,$val) = split(/\s*=\s*/,$_,2);
$CONF{$key} = $val;
}
close(RFILE);
Example configuration file:
# my network configuration
ip = 192.168.1.1
netmask = 255.255.255.0
# old gateway = 192.168.1.252
gateway = 192.168.1.254
So,
$CONF{'ip'} has a value '192.168.1.1'
$CONF{'netmask'} has a value '255.255.255.0'
$CONF{'gateway'} has value '192.168.1.254
No comments:
Post a Comment