in reply to problem in substitution

($file2 = $file1) =~ s/\Q$dir1/$dir2/;

Since you have backslahes inside of the pattern you need use \Q to disable pattern metacharacters.

Also you don't need any modifiers (esg) for the regexp, excepting i probably.

see perldoc perlre