[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: perl + Konfigurationsdatei



High, high ...
* Hans-Dietrich Kirmse <hd.kirmse@gmx.de> schrieb am [28.03.10 22:46]:
> Hallo Kai,
> 
> Kai Wilke schrieb:
> >Hallo allerseits,
> >
> >ich habe ein Problem mit Perl und kenne mich auch nicht in Perl aus.
> >Ich möchte das ein gefundenes Perl-Script die Variablen aus einer
> >Konfigurationsdatei einliest und nicht im Script definiert sind.
> >
> >Nun habe ich nat. gegoogelt und auch das Perl-Kochbuch gefunden.
> 
> online?

Ja, in deutsch, und konqueror zeigt hier auch alles komisch an
(Debian/unstable), bzw. Google ist auch komisch.
http://books.google.de/books?id=nsyK2ANiCjgC&pg=PA347&lpg=PA347&dq=perl+Konfigurationsdatei+einlesen&source=bl&ots=Wk9OAaSLkr&sig=6OhMUqAXAB_78UH0MSTLNF3Ls_Y&hl=de&ei=UjWvS9fZMcue_AaB1tXFDw&sa=X&oi=book_result&ct=result&resnum=4&ved=0CA4Q6AEwAw#v=onepage&q=perl
Konfigurationsdatei einlesen&f=false

> 
> >Ich habe aber irgendwie ein Verständnis-Problem, bei dem Thema.
> >Jedenfalls funktionieren die Beispiele bei mir nicht so richtig (vllt.
> >zu blöd).
> >
> >-----------------------cut----------------------------
> >Perl Code:
> >### Globals - you can change these as needed
> >my $conf_file = "/etc/kwtools/kwdjbdns-update.conf";
> >
> >### reads the configuration file & makes a hash of what's in there.
> > [...]
> >	  print "$var";
> >	  print "$value";
> >  }
> >  close CONFFILE;
> >}
> >
> >&read_conf_file;
> >
> >-----------------------cut----------------------------
> >Nun kann ich zwar die Konfigurationsdatei einlesen und auch mittels
> >"print" ausgeben aber nicht auswerten, bzw. Variablen belegen.
> 
> dieser Codeschnipsel liest die Datei ein und gibt diese Werte aus - mehr
> nicht. Wenn du die Werte als Rückgabewerte der Subroutine haben willst, dann
> musst du statt der beiden print-Anweisungen die key/Value-Paare in einen hash
> aufnehmen.
> 
> also z.B. so:
> 
> 
> ### 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";
> }
> 

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. 

Das Script fängt mit folgendem Variablen an, die in einer
Konfigurationsdatei stehen sollen, allerdings im Format
VAR = Wert.

# Domain name
my $domain_name        = "netzworkk.de";
#
# DHCPD lease file
my $lease_file         = "/var/lib/dhcp3/dhcpd.leases";
#
# where does tinydns stuff live?
my $tinydnspath        = "/etc/tinydns";
#
# tinydns text database files
my $dhcp_dnsfile       = "$tinydnspath/root/dhcp.$domain_name";
my $static_dnsfile     = "$tinydnspath/root/static.$domain_name";
#
# number of seconds to check the lease file for updates
my $update_freq        = 30;
#
my $debug = 0;


Sorry, Kiste
-- 
#######################################################################
Netzworkk                   grml - Linux Live CD fuer Sysadmins
Kai Wilke                   http://grml.org
kiste@netzworkk.de
http://www.netzworkk.de
http://netzworkk.berlios.de

Attachment: signature.asc
Description: Digital signature


Reply to: