I am just entering to the perl world, I got a task to replace multiple xml file in a folder using perl, I tried some of the perl one line code but it does not helped me out, I need a perl code which replace multiple text files in a selected folder. I tried this below post from stackoverflow Replace values for multiple XML files in a folder using perl but it also not helped me. Kindly be gentle because I am new, I furnish my tried code from above stackflow post showing error, please see and suggest solution.
my $dir = ***D:\Perl***;
my $d = opendir();
map {
if (
-f "$dir/$_"
&& ($_ =~ "\.xml$")
) {
open (my $input_file, '<', ) or die "unable to open $input_file $!\n";
my $input;
{
local $/; #Set record separator to undefined.
$input = <$input_file>; #This allows the whole input file to be read at once.
}
close $input_file;
$input =~ s/Comment//g;
open (my $output_file, '>', "$dir/$_") or die "unable to open $output_file $!\n";
print {$output_file} $input;
close $output_file or die $!;
}
} readdir($d);
closedir($d);
error
syntax error at hello3.pl line 10, near "=~ "\.xml$""
Global symbol "$dir" requires explicit package name at hello3.pl line 23.
Global symbol "$output_file" requires explicit package name at hello3.pl line 23.
syntax error at hello3.pl line 28, near "}"
Global symbol "$d" requires explicit package name at hello3.pl line 28.
Global symbol "$d" requires explicit package name at hello3.pl line 29.
Execution of hello3.pl aborted due to compilation errors.
XML files are in the folder D:\Perl\
1.xml
2.xml
3.xml
codes in each xml files are follow below
<?xml version="1.0">
<root>
<!--This is my comment line 1-->
<subtag>
<element>This is 1.xml file</element>
</subtag>
</root>
mapinstead offoreachrecently?