Just like
poolpi said, File::Spec is the way to go. Substituting a directory segment of a path can be annoying, because paths tend to be specified in different formats. For example: File::Find under windows returns the results with a slash appended, even though the path was specified using backslashes. In such cases you have a mixed syntax of slashes and backslashes. If you use File::Spec, you won't have to worry about the specific syntax of a path. Example:
use File::Spec;
# Example with mixed syntax
my $old_dir = "D:\\Windows\\programs1";
my $new_dir = "D:\\Windows/programs2";
my $old_fn = "D:/Windows/programs1/a.txt";
my $rel_path = File::Spec->abs2rel($old_fn, $old_dir);
my $new_fn = File::Spec->catfile($new_dir , $rel_path);
print "New Filename: $new_fn\n";
# or combine both:
my $new_fn2 = File::Spec->catfile($new_dir , File::Spec->abs2rel($old_
+fn, $old_dir));
print "New Filename2: $new_fn2\n";