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

I have a hash which store the working dir path which I want to combine with the file in that dir:

my $fullpath = $config{'workingDir'}.$filename; print "Dir: $config{'workingDir'}\n"; print "File: $filename\n"; print "Full path: $fullpath";
this gives me:
Dir: /root/working/
File: test.pl
Full path: test.plorking/
somehow the directory name is being overwritten.. Please help.

Replies are listed 'Best First'.
Re: problem path stored in hash
by moritz (Cardinal) on Jun 25, 2010 at 09:38 UTC
    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.
      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.

        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);
Re: problem path stored in hash
by mje (Curate) on Jun 25, 2010 at 10:02 UTC

    Your code does not seem to produce that result. However, I notice if you overlay "test.pl" on top of the string "/root/working/" you get "test.plorking/".

Re: problem path stored in hash
by JediWizard (Deacon) on Jun 25, 2010 at 16:01 UTC

    I'd bet dollars to donut's you have a carriage return character ("\r") at the end of $config{'workingDir'}. Try:

    $config{workingDir}=~s/\s+$//g;

    Edit: The carriage return could also be at the front of $filename, but it seems far more likely it is at the end of $config{workingDir}. My guess: the hash %config is probably read in from a file created on a windows system, but the code ir running in *NIX. Windows uses: "\r\n" at the end of a line, *NIX only expects "\n", and hence when reading a line in from the file you are left with an extra "\r" at the end. Another solution would be to run the dos2unix command on the config file to remove the extra "\r" characters.


    They say that time changes things, but you actually have to change them yourself.

    —Andy Warhol