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

Hello Perl Monks, I have a path in a variable as below $file = "c:/reports/check/test.txt"; i want to replace the forward slash / to backward slash. i.e on printing the value of $file, it should print c:\reports\check\test.txt. i have used the split function to extract all the dir names like
my( @dirs ) = split /\//, $filepath;
Now how do i go abt embedding a backslash \ between all the directories, how do i append value to a variable. Please help me.

Replies are listed 'Best First'.
Re: Appending values to variable.
by davorg (Chancellor) on Nov 16, 2005 at 11:45 UTC

    "join" is the opposite of "split".

    my $path = join '\\', @dirs;

    You can append data to a variable using the concaternation operator '.'

    $path = $path . '\\' . $file;

    You can also just use string interpolation.

    $path = "$path\$file";

    But your original problem can probably be most easily solved with the tr/// operator.

    $file =~ tr|/|\\|;
    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: Appending values to variable.
by xdg (Monsignor) on Nov 16, 2005 at 11:49 UTC

    I strongly suggest using either File::Spec or it's nicer wrapper Path::Class for manipulating file paths in a portable way. You'll save yourself a lot of headaches.

    use Path::Class; my $file = file($filepath,"test.txt");

    Doing it the hard way, you can recombine a split using join. Alternatively, you could change the slashes using a regex substitution and then append as normal.

    $filepath =~ s{/}{\}g; $file = $filepath . "\test.txt";

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re: Appending values to variable.
by Zaxo (Archbishop) on Nov 16, 2005 at 11:46 UTC

    my $newpath = join '\\', @dirs;
    Or you could have just substituted,
    $filepath =~ tr!/!\\!;
    The doubled backslashes are needed because it is the escape character in perl quoting.

    After Compline,
    Zaxo

Re: Appending values to variable.
by tirwhan (Abbot) on Nov 16, 2005 at 11:45 UTC

    You could use join

    $newpath=join("\",@dirs);

    or not use split in the first place and instead replace the slashes directly

    $filepath=~s{/}{\\}g;

    But note that usually when dealing with filepaths it is better not to manually munge but use the appropriate modules for that (see perldoc perlport and perldoc File::Spec for more).


    Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan
Re: Appending values to variable.
by pajout (Curate) on Nov 16, 2005 at 11:48 UTC
    perl -e 'my $var = q(abc/de/f); $var =~ s/\//\\/g; print $var;'
Re: Appending values to variable.
by prasadbabu (Prior) on Nov 16, 2005 at 11:45 UTC

    Hi nisha, You can do easily using regex.

    $filepath =~ s|/|\\|g;

    updated: Also you can make use of tr/// as davorg suggested

    Prasad

      Two small points:

      1. Your code doesn't compile. You need to escape the \ in the replacement string (s|/|\\|)
      2. There is no need to use the regex engine if you're replacing one character with another. That's what tr/// is for.
      --
      <http://dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg