################################################# #!/usr/bin/perl #InventoryReport.pl ###Good Perl practice: use warnings; use strict; ###CPAN module finds files recursively use File::Find; ###Open a file for output using the handle OUT open (OUT, ">InventoryFile.txt"); ###Top-level directory to report on: my $directory = '//InstallBox/C'; ###invoking File::Find, printing path, then filename: ##Explain to them what \&wanted means find (\&wanted, $directory); sub wanted { print OUT "$File::Find::dir/"; print OUT "$_\n"; } __END__ ################################################# ################################################# #!/usr/bin/perl #CompareReport.pl ###good Perl practice: use warnings; use strict; ###invoke the CPAN module: use List::Compare; ###Open CDiffs.txt for ouput using the handle named OUT open (OUT, ">CDiffs.txt"); ###Open OldInventory.txt for input using the handle FILE1 open (FILE1, "OldInventory.txt"); ###Open NewInventory.txt for input using the handle FILE2 open (FILE2, "NewInventory.txt"); ###idiomatic: lowercase each pathname string after loading the arrays— ###(this is a Windows system, might not want to “lc” for Unix): #read the entire file into an array (this is called slurping) my @file1 = <FILE1>; #for readability I would re-write '$_ = lc for @file1;' as ##Loop over every line of the array @file1. ##foreach puts the current item in the list in to the ##default variab +le $_. foreach (@file1) { #set the variable to a lowercase version of itself $_ = lc($_); } #Lather rinse repeat comments for file2 my @file2 = <FILE2>; $_ = lc for @file2; ###load the files for List::Compare: my $lc = List::Compare->new(\@file1, \@file2); ###List::Compare features to identify unique elements in each file: my @file1only = $lc->get_Lonly; my @file2only = $lc->get_Ronly; ###talk to the user: print OUT "IGNORING FILES WITH Tmp OR Temp IN PATHNAME.\n"; print OUT "IGNORING FILES IN THE RECYCLE BIN.\n"; print OUT "IGNORING ALL HISTORY FILES.\n"; print OUT "LOWERCASING ALL FILE AND PATHNAMES BEFORE COMPARING.\n"; ###selection criteria is a set of easy Regular Expressions: foreach my $file1(@file1only) { #Explain this line .. to even an advanced user who hasn't programmed.. + this could be confusing. unless (($file1 =~ "tmp") or ($file1=~"temp") or ($file1 =~ "recyc +ler") or ($file1 =~ "history") +) { print OUT $file1; } } ###separator is a couple of newlines: print OUT "\n\n"; ###talk to the user about the second file: print OUT "IGNORING FILES WITH Tmp OR Temp IN PATHNAME.\n"; print OUT "IGNORING FILES IN THE RECYCLE BIN.\n"; print OUT "IGNORING ALL HISTORY FILES.\n"; print OUT "LOWERCASING ALL FILE AND PATHNAMES BEFORE COMPARING.\n"; print "\n\n"; #Same as above ..splain this a bit more.. ###selection for the second inventory file: foreach my $file2(@file2only) { unless (($file2 =~ "tmp") or ($file2=~"temp") or ($file2 =~ "recyc +ler") or ($file2 =~ "history") +) { print OUT $file2; } } __END__ ##############################################
In reply to Re: Code review for magazine article?
by Grygonos
in thread Code review for magazine article?
by McMahon
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |