use File::Spec; ... for my $key (keys %hash){ ... $outputfile = File::Spec->catfile($str,$outputfile); ... }