Using comments in code meant to be published seems really ugly to me (could just be me). Myself, I much prefer seeing the whole body of code (with each line preceeded with its line number), followed by a detailed line-by-line description of what is going on. It is so much easier to follow this way. So you might do something like this (I doubt my explanations are satisfactory, I just threw this together in a matter of 15 minutes or so):
1. #!/usr/bin/perl -w 2. 3. use strict; 4. use File::Find; 5. 6. open( my $report, '>', 'InventoryFile.txt' ) 7. or die "open failed: $!"; 8. 9. find( 10. sub { print $report "$File::Find::dir/$_\n" }, 11. '//InstallBox/C' 12. ); 13. 14. close( $report ) or die "close failed: $!";
Line 1 is the shebang line. The '-w' switch enables special warnings that will tell us if we do something that may not be desired. Line 3 enables strictures to enforce better coding practices. Line 4 pulls in the File::Find module so we can easily traverse the file system. Lines 6-7 open our report file where we will store our findings. Lines 9-12 recursively traverses the file system located at '//InstallBox/C' (presumeably the hard drive of a remote Windows© machine on our network). The find() method is declared by the File::Find module we pulled in on line 4. Line 10 tell File::Find what do do with each file or directory we find on the file system. For this script, we simply output the path of the file or directory to our report file. Line 14 simply closes our report file to signal we are done with it.
1. #!/usr/bin/perl -w 2. 3. use strict; 4. use List::Compare; 5. 6. my $warning = <<'END_WARNING'; 7. IGNORING FILES WITH Tmp OR Temp IN PATHNAME. 8. IGNORING FILES IN THE RECYCLE BIN. 9. IGNORING ALL HISTORY FILES. 10. LOWERCASING ALL FILE AND PATHNAMES BEFORE COMPARING 11. 12. 13. END_WARNING 14. 15. open( my $diffs, '>', 'CDiffs.txt' ) 16. or die "open failed: $!"; 17. open( my $old_inv, '<', 'OldInventory.txt' ) 18. or die "open failed: $!"; 19. open( my $new_inv, '<', 'NewInventory.txt' ) 20. or die "open failed: $!"; 21. 22. my @old_inv = map { chomp; lc } <$old_inv>; 23. my @new_inv = map { chomp; lc } <$new_inv>; 24. 25. my $compare = List::Compare->new( \@old_inv, \@new_inv ); 26. 27. print $diffs $warning; 28. 29. for ( $compare->get_Lonly() ) { 30. print $diffs $_, "\n" 31. unless ( /(?:te?mp|recycler|history)/i ); 32. } 33. 34. print $diffs "\n\n", $warning; 35. 36. for ( $compare->get_Ronly() ) { 37. print $diffs $_, "\n" 38. unless ( /(?:te?mp|recycler|history)/i ); 39. } 40. 41. close( $new_inv ) or die "close failed: $!"; 42. close( $old_inv ) or die "close failed: $!"; 43. close( $diffs ) or die "close failed: $!";
Line 1 is the shebang line. The '-w' switch enables special warnings that will tell us if we do something that may not be desired. Line 3 enables strictures to enforce better coding practices. Line 4 pulls in the List::Compare module, which allows us to calculate the differences between two lists. Lines 6-13 declare a simply warning we will output to our report file. Lines 15-20 open up the two report files we have previously generated at two different moments in time using the script in listing 1.1 (author note: listing 1.1 would be the first script that generates our reports). The third file opened is for output. This is where we will store our report of differences. Lines 22 and 23 organize our inventory report files into two separate arrays - one list for each version of the report. Line 25 creates an object of the List::Compare module, passing in our two (potentially different) report lists. Line 27 outputs our warning from lines 6-13 to the report file to alert the user of what is happening. Lines 29-32 generate our difference report from the point of view of the older inventory list. We skip file paths containing the strings 'tmp', 'temp', 'recycler', and/or 'history' as we are not interested in such files. Line 34 reprints our warning to the report file to remind the user of what is going on. Lines 36-39 generate our difference report again, but this time from the point of view of the newer inventory list. This way we receive a second view of how things have changed. Finally, lines 41-43 close all of the files we have used. And that is all!
In reply to Re: Code review for magazine article?
by Anonymous Monk
in thread Code review for magazine article?
by McMahon
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |