Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

problems with reading data from cfg file

by pitbull3000 (Beadle)
on Sep 10, 2001 at 01:15 UTC ( #111329=perlquestion: print w/replies, xml ) Need Help??

pitbull3000 has asked for the wisdom of the Perl Monks concerning the following question:

hi all you noble men, i just try to read in some data from a config file with following code and try to connect to a mysql database, but i always get the error, script could not connect to database.
#!/usr/bin/perl use strict; use CGI; use CGI::Carp qw(fatalsToBrowser); use DBI; use HTML::Entities; my $db_config_file = "db.cfg"; my %db_config = (); open CONFIG, ("< $db_config_file") or die "Konnte Datenbank Konfigurat +ions Datei nicht &ouml;ffnen! $!"; my @config_pairs = <CONFIG>; close CONFIG; foreach (@config_pairs) { chomp $_; my ($key, $value) = split/:/, $_; $db_config{$key} = $value; } my $database = $db_config{'database'}; my $db_server = $db_config{'db_server'}; my $db_user = $db_config{'db_user'}; my $db_pass = $db_config{'db_pass'}; my $dbh = DBI->connect("dbi:mysql:$database:$db_server", "$db_user", " +$db_pass") or die "Konnte Datenbankverbindung nicht herstellen: $!";
if i read in the config file with the following code
#!/usr/bin/perl use strict; use CGI; use CGI::Carp qw(fatalsToBrowser); use DBI; use HTML::Entities; my $db_config_file = "db.cfg"; my %db_config = (); open CONFIG, ("< $db_config_file") or die "Konnte Datenbank Konfigurat +ions Datei nicht &ouml;ffnen! $!"; my @config_pairs = <CONFIG>; close CONFIG; foreach (@config_pairs) { chomp $_; my ($key, $value) = split/:/, $_; $db_config{$key} = $value; } my $database = $db_config{'database'}; my $db_server = $db_config{'db_server'}; my $db_user = $db_config{'db_user'}; my $db_pass = $db_config{'db_pass'}; my $query = new CGI; print $query->header(); print $query->start_html(); print $database . "<br>"; print $db_server . "<br>"; print $db_user . "<br>"; print $db_pass . "<br>"; print $query->end_html();
it prints out the correct values->
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML Basic 1.0//EN" "http://www.w3.org/TR/xhtml-basic/xhtml-basic10.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US"><head><title>U +ntitled Document</title> </head><body>db29970540 <br>db07.puretec.de <br>p7397271 <br>8vskBBPH<br></body></html>
what is the error i do? update-> the config file looks like this:
database:xxxxxx db_server:yyy.yyyyy.yy db_user:xxxxxx db_pass:yyyyyy

Replies are listed 'Best First'.
Re: problems with reading data from cfg file
by Jazz (Curate) on Sep 10, 2001 at 01:55 UTC
    What would be more helpful than "Konnte Datenbankverbindung nicht herstellen: $!" (which, according to babelfish means "Data base connection could not manufacture") would be to use DBI's $DBI::errstr, which will give you the exact connection problem.

    my $dbh = DBI->connect("dbi:mysql:$database:$db_server", "$db_user", "$db_pass") or die "Error: $DBI::errstr $!\n";

    I really, really hope that you didn't enter the real login info in your post. But if that was the real info (change it!), the $DBI::errstr was "Unknown MySQL Server Host (db07.puretec.de)".

    Jasmine

Re: problems with reading data from cfg file
by pitbull3000 (Beadle) on Sep 10, 2001 at 02:16 UTC
    no dont worry jazz, this is not my login data
Re: problems with reading data from cfg file
by pitbull3000 (Beadle) on Sep 10, 2001 at 02:45 UTC
    i think i figured it out why i always get an error. the config file is written on a win32 box, and as i was told right now there is more than just \n at the end of the line, there is also a \r there, so i have to get rid of this also...
      chomp will remove only the newlines based on the current value of $\ which is the OUTPUT_RECORD_SEPARATOR (in English)

      If the file was written on win32 (where $\ defaults to \n\r) and then uploaded to unix (where $\ defaults to \n), chomp only catches the \n and misses the \r.

      So you can do this:

      foreach ( @config_pairs ) { chomp; s/\r//; # more stuff }

      Jasmine

        yep, thanks jazz. already put some nice regex into my loop, and now it just works fine.... ;.D

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://111329]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2022-12-07 07:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?