Re: perl + Konfigurationsdatei
Am Sonntag, den 28.03.2010, 23:38 +0200 schrieb Kai Wilke:
> Und wie benutze ich jetzt das? Die Ausgaben sind sehr schön, ich weiß
> nicht wie ich das zuweisen soll. Habe den halben Tag schon damit
> verbracht, nur weil ich das Script nicht sonderlich ändern will.
>
> foreach my $key (keys %meine_daten) {
> if ($key = domain_name ) {
> $domain_name = $2;
> }
> }
>
> funktioniert nicht. Ich möchte halt folgende Variablen aus einer
> Konfigurationsdatei belegen.
Also bei mir funktioniert es (und ich habe auch nicht daran gezweifelt
obwohl ich selber perl erst lerne).
--- config.pl ---
#!/usr/bin/perl -w
use strict;
### Globals - you can change these as needed
my $conf_file = "kwdjbdns-update.conf";
### reads the configuration file & makes a hash of what's in there.
sub read_conf_file {
my %data = ();
unless (open(CONFFILE,$conf_file)) {
#`logger -t dns_update error opening configuration file`;
print STDERR "Can't open configuration file\n";
exit 1;
}
while (<CONFFILE>) {
chomp;
s/#.*//;
s/^\$+//;
s/\s+$//;
next unless length;
my ($var, $value) = split(/\$*=\$*/, $_, 2);
$data{$var} = $value;
}
close CONFFILE;
return %data;
}
my %meine_daten = &read_conf_file;
# zur Kontrolle den Hash ausgeben
foreach my $key (keys %meine_daten) {
print $key, ' ', $meine_daten{$key}, "\n";
}
--- end ---
--- kwdjbdns-update.conf ---
# Domain name
domain_name = "netzworkk.de"
# DHCPD lease file
lease_file = "/var/lib/dhcp3/dhcpd.leases"
# where does tinydns stuff live?
tinydnspath = "/etc/tinydns"
# tinydns text database files
dhcp_dnsfile = "$tinydnspath/root/dhcp.$domain_name"
static_dnsfile = "$tinydnspath/root/static.$domain_name"
# number of seconds to check the lease file for updates
update_freq = 30
debug = 0
--- end ---
Die Subroutine liefert einen Hash mit Key/Value-Paaren.
Die Keys bekommen den Namen der Variablen und darauf liegen die
zugeordneten Werte.
mfG Sascha
PS: zum Perl lernen empfehle ich
http://www.perlboard.de/perlguide/Inhalt.html
Reply to: