in reply to Self Extracting Cabinet Files in Perl
Use hashes that are keyed by "os_type". As posted, you have about 4 times more lines of code than you really need, because you are using separate array/variable names for each OS. Not only does it take longer to create these unnecessary lines of code, it will also take much longer to maintain, fix, adapt, etc. Try something like this:
This way, when you need to add yet another OS to the list, you only need to change the one line that assigns values to the "@valid_os" array, and everything else is taken care of, without further ado.... my %installed; # these will be HoA my %hotfix; # where the hash keys are my %ready; # taken from @valid_os my $ninstalled = 0; foreach my $os (@valid_os) { my $path_os = $PATCHPATH . $os; opendir( DIR, $path_os ) or die "dir $path_os not found: $!"; while (my $_ = readdir(DIR)) { next if ( /^\.\.?$/ ); push( @{$installed{$os}}, $_ ); ### SEE NOTE BELOW $ninstalled++; } closedir DIR; } die "No hotfix files are installed in $PATCH_PATH\n" unless ($ninstall +ed); ... while ($db->FetchRow) { my %hotfix_record = $db->DataHash("os","swname"); push( @{$hotfix{$os}}, $hotfix_record{swname} ); } ... for my $os (@valid_os) { @{$ready{$os}} = Check_Array( @{$installed{$os}}, @{$hotfix{$os}} +); for my $hotfix ( @{$ready{$os}} ) { Add_Hotfix( $hotfix, $os ); } }
NOTE: The code in your OP was doing a "chomp" on the string that was returned by readdir(). NO, DO NOT DO THAT. Directories are not like text files; the file name string you get from a directory via readdir() does not have a line-feed at the end. This would probably explain the error message that you reported at the beginning ("file not found").
|
|---|