in reply to problem path stored in hash

From the piece of code you gave us I can't reproduce your problem:
my %config = (workingDir => '/root/working/'); my $filename = 'test.pl'; my $fullpath = $config{'workingDir'}.$filename; print "Dir: $config{'workingDir'}\n"; print "File: $filename\n"; print "Full path: $fullpath\n";

Produces the output

Dir: /root/working/ File: test.pl Full path: /root/working/test.pl

Which is exactly what I expected.

So the error is likely in a part of the code that you don't show us, which is unfortunate because it means we can't help you.

Perl 6 - links to (nearly) everything that is Perl 6.

Replies are listed 'Best First'.
Re^2: problem path stored in hash
by awsiv (Initiate) on Jun 25, 2010 at 09:52 UTC
    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
      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!

      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);