Dear roboticus,

Thanks a mil for your explanation, it is much appreciated. Thank you very much.

Now, back to coding.

As said before, the script laid out here is just one part of a larger script ( since this is in a different post I am not sure where to put it, I hope the forum admins will forgive me. ). Anyway, I tried creating a subroutine from the code discussed and implementing it into another script.

As far as I can observe, I think I surely have been wrong about putting the comparison below the other subroutine. My thoughts were that there should be a comparison summary in each folder. This also leads to the next issue I (usually) have with subroutines, I am not sure which arguments to pass to it and which arguments should be returned. I read up on subs, but somehow I cannot apply the underlying principle from the book(s) to present code ( I guess that is a matter of practice ).

So like I said, the present code looks for the paths specified in the text file and replaces some entities, while also creating a backup file. After that a comparison of each .xml and .back file in the different folders should be carried out, with an individual report in each folder, summarising what has been replaced or not. The code has been anonymised for well you know why...

In case I failed to mention details about the purpose or the code itself, please do let me know ( apologies in advance )

Thanks a mil in advance for your comments

Kind regards

C.
use 5.014; use strict; use warnings; use Path::Tiny qw/ path /; use POSIX(); use autodie qw/ close /; use File::BOM; use Carp::Always; use Data::Dump qw/ dd /; use Encode qw(encode decode); use File::Glob; use Text::Diff; Main( @ARGV ); exit( 0 ); sub Main { #my( $infile_paths ) = @_; #if run via command line my( $infile_paths ) = 'C:\dev\test_paths.txt'; chomp $infile_paths; my @paths = GetPaths( $infile_paths ); for my $path ( @paths ){ RetrieveAndBackupXML( $path ); CompareAndCheckForReplacements( $path ); } return @paths; } ## end sub Main sub GetPaths { use File::BOM; ## my @paths = path( shift )->lines_utf8; my @paths = path( shift )->lines( { binmode => ":via(File::BOM)" } + ); s/\s+$// for @paths; # "chomp" return @paths; } ## end sub GetPaths sub RetrieveAndBackupXML { my( $directory ) = shift; ## same as shift @_ ## my $date = POSIX::strftime( '%Y-%m-%d', localtime ); #suffix + for the backup-file, e.g. 2014-08-01 my $bak = "$date.bak"; my @xml_files = path( $directory )->children( qr/\.xml$/ ); for my $file ( @xml_files ) { Replace( $file, "$file-$bak" ); } } ## end sub Main # Fix xml entities and create a copy of the original file before editi +ng sub Replace { my( $in, $bak ) = @_; path( $in )-> copy( $bak ); #create a copy of $in with the ending +specified in $bak my $infh = path( $bak )->openr_raw; my $outfh = path( $in )->openrw_raw; while( <$infh> ) { s{&}{&amp;}g; # In some very rare cases does not match as inte +nded, thus file comparison added s{\s>\s}{&gt;}g; s{\s<\s}{&lt;}g; print $outfh $_; } close $infh; close $outfh; } ## end sub Replace sub CompareAndCheckForReplacements{ my( $directory ) = shift; ## compare files to check where replacements were made #open log-file to write results to open my $FH, '>', "file_difference_report" or die $!; #retrieve xml file name and trim file extension my @base_file_names_xml = map { s/\.xml$//; $_ } glob('*.xml'); my @base_file_names_bak = glob('*.bak'); #cutting off file extension to use file name only, extension for #comparing .xml and .bak added by code below; for my $file_name ( @base_file_names_xml ) { if ( ! -e "$file_name.xml" ){ print "$file_name.xml: Not present ... not interesting fil +e?\n"; next; } if ( ! -e "$file_name.xml.bak" ){ print "$file_name: no backup, so probably not changed\n"; next; } # If we get here, we have a .bak and a .xml file, so make anot +her # program to compare them for us: my $output = diff "$file_name.xml", "$file_name.xml.bak"; print $FH "\n\n===== $file_name changes =====\n"; print $FH $output; print $FH "\n\n"; } }

In reply to Re^10: comparing contents of two arrays and output differences by PitifulProgrammer
in thread comparing contents of two arrays and output differences by PitifulProgrammer

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.