QUOTE(Sun751 @ 22 Jun, 2009 - 01:26 AM)

Below I have got bunch of code which checks either R_Cinformix or R_Coracle is defined, then run the respective deletion of element on same hash. I am just wondering if there is any more smart way to do this task in less code!!!
This can be slimmed down quite a bit by taking advantage of hash references. Since you already appear to be using hashrefs for everything, it just makes things that little bit easier:
CODE
my $target;
if ($R_Cinformix) {
$target = 'oracle';
} elsif ($R_Coracle) {
$target = 'informix';
} else {
die "Neither R_Cinformix nor $R_Coracle set!";
}
for my $hashref ($HR_Cwnt, $HR_Cunx) {
for my $id (keys %$hashref) {
delete $$hashref{$id} if $id =~ /\.$target/;
}
}
$target = uc $target;
for my $hashref ($HR_CScomponent, $HR_CWcomponent) {
for my $id (keys %$hashref) {
delete $$hashref{$id} if $id =~ /$target/;
}
}
If the literal . in the keys of the first two hashes isn't crucial to that regex, you could make the regexes case-insensitive (/$target/i) and combine the two "for my $hashref..." loops into a single loop across all four hashrefs.
A couple side notes based on this and your other recent posts:
- Your use of boolean flags seems a bit excessive. If multiple options are mutually exclusive, it's generally better to use a single variable to store the active value rather than a boolean flag for each possibility. e.g., In this case, a single $active_database would be cleaner and more extensible than separate $R_Cinformix and $R_Coracle unless there is a situation in which it would be valid for $R_Cinformix and $R_Coracle to both be true at the same time. (Given that this code pretty much boils down to "if informix is active, blow away all references to oracle, and vice-versa", I feel pretty safe in assuming that it will never be valid for both to be true.)
- Your regexes tend to be of the form /^.*text.*$/. Putting (non-capturing) .* at the beginning or end of a regex is ultimately meaningless, as /text/ alone will match "text" anywhere in the value being tested. /text/ alone is generally considered to be the stylistically better form, as it contains less visual noise (making the meaning clearer), plus it is significantly faster on some regex implementations, since the regex engine isn't wasting time trying to match the .* portions. (I have a feeling that perl's implementation is probably smart enough to optimize them away, but haven't benchmarked the two variations to verify their relative performance in Perl.)