From 9c691994d0e6cb60495e0cd447b6e3eb71721211 Mon Sep 17 00:00:00 2001 From: Austen Adler Date: Fri, 19 Aug 2016 11:23:25 -0400 Subject: [PATCH] Don't write .bak file, write only if there were changes --- process.pl | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/process.pl b/process.pl index 69de7c8..71dc6f0 100755 --- a/process.pl +++ b/process.pl @@ -10,21 +10,17 @@ foreach my $file (@ARGV) { # If it exists if (-e $file) { # Load the autoformatted data into $data - my $data = process_file($file); - open(MYOUTPUTFILE, "+>$file.bak") or die "Error opening output file $!"; - # Write the data to $file.bak - #TODO: This could be done with no .bak - print MYOUTPUTFILE $data; - close(MYOUTPUTFILE); - if (compare($file, "$file.bak") != 0) { - # If there were changes, then tell the user + my ($data, $old_data) = process_file($file); + # Check if original data matches new data, if it doesn't, write it + # If it matches (there were no changes made), do nothing + if (not $data eq $old_data) { + # Write out the data + open(MYOUTPUTFILE, "+>$file") or die "Error opening output file $!"; + print MYOUTPUTFILE $data; + close(MYOUTPUTFILE); + # Tell the user that we changed the file print "Processed $file\n"; } - # Replace the original file with this file - # Delete it - unlink $file; - # Overwrite the old one - rename "$file.bak", $file; } } # Creates the automatically formatted file @@ -33,8 +29,12 @@ sub process_file { open(MYINPUTFILE, "<$filename") or die "x $!"; # The formatted file my($data) = ""; + # The original file (used for comparison to see if changes were made) + my($old_data) = ""; # Loop through every line in the file while() { + # Add data to $old_data + $old_data .= $_; # Get rid of trailing whitespace s/\s*$//; my($line) = $_; @@ -54,5 +54,5 @@ sub process_file { my $basename = basename($filename); $data = "/**\n * \@file $basename\n * \@author Austen Adler (agadler)\n * TODO: Description\n */\n$data"; } - return $data; + return ($data, $old_data); }