in reply to Re: problem path stored in hash
in thread problem path stored in hash

Hi, I am using a sub to read a config file, store it in a hash and then return the value.
sub get_config { my %config; open (CONFIG, "$CONFIG_FILE") or die "Error: $!\n"; while (my $line = <CONFIG>){ # verbose($line); chomp($line); if($line =~ m/WORKING_DIR=(.*)/){ $config{"workingDir"} = $1; verbose("Working Dir: ".$config{"workingDir"}."\n"); }elsif($line =~ m/INPUT_FILE=(.*)/){ $config{"inputFile"} = $1; verbose("Input File: ".$config{"inputFile"}."\n"); }elsif($line =~ m/MAP_FILE=(.*)/){ $config{"mapFile"} = $1; verbose("Map file: ".$config{"mapFile"}."\n"); } } close(CONFIG); return %config; }
The config file looks like this:
WORKING_DIR=/root/working/ INPUT_FILE=test.in MAP_FILE=test.map

Replies are listed 'Best First'.
Re^3: problem path stored in hash
by cdarke (Prior) on Jun 25, 2010 at 10:09 UTC
    I tried your code and the returned hash was:
    workingDir /root/working/ mapFile test.map inputFile test.in
    which looks fine to me. One possibility is that you night have a "\r" in the data (did the config file come from Windows?) or in a print statement instead of a "\n".

    Update: It is possible to reproduce yur problem with a "\r" appended to the WORKING_DIR in your config file.
      Hi, thanks a lot.It really is the '\r'!, I copied the file from windows to linux VM .

      chomp() or chop() didn't remove it. now i'm using regex.
      Cheers!
        If you want chomp to remove the "\r" then:
        local $/ = "\r\n";
        in your subroutine before you read the config file. chop only removes the last character, regardless of what it is.
Re^3: problem path stored in hash
by jethro (Monsignor) on Jun 25, 2010 at 10:58 UTC

    There could be (invisible) control-characters, for example backspaces, in your config file. To see if your strings have any control-characters in them, you might use Data::Dumper:

    use Data::Dumper; $Data::Dumper::Useqq=1; print Dumper(\%config);