Dear Members of the Perl Community,
I am new to this forum and quite new to programming. I picked up Perl to facilitate certain annoying tasks such as the following:
A given folder x contains a number of files.
The aim of the script is to open each file in the folder and replace the strings as shown in the code.
So far, the script opens the folder and reads the files into the array. The files are printed to my command line.
However, something seems to be wrong in the substitution section, since the files are not touched (same date of creation, etc.) and apparently no replacement has been effected and no backup files have been created, either.
I would be delighted if you could shed some light on my code, since I am not getting any error messages.
Thanks a mil in advance for your support. C.$^I = ".bak"; my $directory = "c:/temp/"; opendir( DIR, $directory ) or die "Unable to open dir!"; my @xml_files = grep( /\.xml$/, readdir(DIR) ); say "files found"; print "\n\n"; say for @xml_files; print "\n\n"; say "Replacing strings"; print "\n\n"; foreach my $file (@xml_files) { open( IN, "+>", $file ) or die $!; while( <IN> ){ $_ =~ s{&}{&}g; $_ =~ s{&amp;}{&}g; $_ =~ s{\s>\s}{>}g; $_ =~ s{\s<\s}{<}g; print IN $file; } close ( IN ); } closedir (DIR); say for @xml_files;
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |