c:\dev>perl search_and_replace.pl Global symbol "$directory" requires explicit package name at search_and_replace.pl line 33. Execution of search_and_replace.pl aborted due to compilation errors. #### c:\dev>perl search_and_replace.pl Path::Tiny paths require defined, positive-length parts at search_and_replace.pl line 33 #### c:\dev>perl search_and_replace.pl Variable "$path" is not imported at hornbach_mit_verz_suche.pl line 34. (Did you mean &path instead?) Global symbol "$path" requires explicit package name at search_and_replace.pl line 34. #### c:\dev>perl search_and_replace.pl ': Filename too long at search_and_replace.pl line 34. #### use 5.014; use strict; use warnings; use File::Slurp qw(:all); use Path::Tiny qw/ path /; use POSIX(); use autodie qw/ close /; my $infile_paths = 'C:\temp\test_folder\paths.txt'; my @paths = read_file $infile_paths, { binmode => ':utf8' } or die $!; chomp(@paths); # say for @paths; foreach my $path (@paths){ Main($path), } Main( @ARGV ); exit( 0 ); sub Main { my $directory = ""; my $date = POSIX::strftime( '%Y-%m-%d', localtime ); my $bak = "$date.bak"; my @xml_files = path( @paths )->children( qr/\.xml$/ ); for my $file ( @xml_files ) { Replace( $file, "$file-$bak" ); } } ## end sub Main sub Replace { my( $in, $bak ) = @_; path( $in )->move( $bak ); my $infh = path( $bak )->openr_raw; my $outfh = path( $in )->openrw_raw; while( <$infh> ) { s{&}{&}g; ## will match more than what you want fix it s{&amp;}{&}g; s{\s>\s}{>}g; s{\s<\s}{<}g; print $outfh $_; } close $infh; close $outfh; } ## end sub Replace