There are many areas for improvement, and I think that you better just try to improve it step by step, checking it to make sure that it works every time.
What pops up on me the most is the reuse of hard-coded IP addresses 67.176.10.2 through 67.176.10.9 . You associate some data with each of them (e.g. @r1 and the like). Thus, it would be useful for you to consider the hash.
"But wait, I can only put the scalars in a hash value!" True, therefore, you need to use links (the tutorial is here ).
Here is an example of how you can simplify this first big procedure:
sub task1_2_2 { # ... my %ip_address_to_r = ( "67.176.10.2" => \@r1, "67.176.10.3" => \@r2, "67.176.10.4" => \@r3, "67.176.10.5" => \@r4, "67.176.10.6" => \@r5, "67.176.10.7" => \@r6, "67.176.10.8" => \@r7, "67.176.10.9" => \@r8, ); # ... foreach ($datax->findnodes($syspath)) { next unless $_->getName() ne "contents"; # Go to next iteration of loop if we have "contents" my $v = join(" = ", $_->getName(), $_->to_literal); push @{$ip_address_to_r{$ip}}, $v; }
Pay attention to two things:
- We are constructing a hash associating IP addresses with array references (using the link operator
\ ). This link will point to the same array, even if it is modified using push operations. - In the last line of the
for loop, we get the link as a scalar (because scalars are links). Then we βplayβ the link using the @ operator, which returns the base array. Then push works as you expected.
Look for ways to use associative arrays and references to your benefits. Then you can search in far fewer lines of code, and you don't need as many if in your loops. Also look for general conditions that apply in all cases, and set them out of the internal legend and place them at the top of the loop.
source share