in reply to Run a perl code on all input files and have the results in different output files

As a quick remedy until someone who knows better can help you, try opening the output file before the loop, and then APPEND the structure to the file inside the loop. I believe from a quick glance at XML::Dumper that pl2xml($perl, $filename) does not append but overwrites whatever the current contents of file with $perl. If you omit the $filename, you will get the xml string back, e.g. my $xmlstr = pl2xml($perl); (untested). So, open the output file before the loop: open($fh, '>:encoding(UTF-8)', $filename) or die "file, $!"; Then inside the loop, after decode(): $perl = $tap3->structure; my $xmlstr = $dump->pl2xml($perl); print $fh $xmlstr; OR simpler: print $fh $dump->pl2xml($tap3->structure); And after the loop close $fh; But perhaps you want a new $tap3 object to be created inside the loop each iteration?

Replies are listed 'Best First'.
Re^2: Run a perl code on all input files and have the results in different output files
by SaraMirabi (Novice) on May 12, 2020 at 09:02 UTC
    Is it possible to share the edited code with me,I didn't get unfortunately. yes, I need tap3. This code perfectly goes fine for one file, I just want to give a batch of similar code and input and get the outputs.
      my $filename="Taps/Output.xml"; my $fh; open($fh, '>:encoding(UTF-8)', $filename) or die "file, $!"; foreach my $file(glob 'Taps/DA*') { my $tap3 = TAP3::Tap3edit->new(); $files= basename($file); #<< are you sure this is needed/does it wo +rk? $tap3->decode($files) || die $tap3->error; print $fh $dump->pl2xml($tap3->structure); } close $fh;

      WARNING: This code reads just ONE tap file, decodes it and dumps it. It does that for all TAP files. Separately. I am not sure you will get correct output, because I am not sure you want to read and decode all files first or decode and dump each separately, as above code does.