2011-08-17 80 views
4

爲了成爲一個更好的腳本編寫者,有沒有辦法讓下面的代碼少用幾行?perl高效代碼

sub task1_2_2 { 
    #Make sure syslog configuration is correct 
    my @r3r = my @r4r = my @r5r = my @r6r = my @r7r = my @r8r = ("name = 67.176.10.200","facility-override = local3"); 
    my @r1r = ("name = 67.176.10.200","facility-override = local3","source-address = 67.176.255.1"); 
    my @r2r = ("name = 67.176.10.200","facility-override = local3","source-address = 67.176.255.2"); 
    my (@r1,@r2,@r3,@r4,@r5,@r6,@r7,@r8,%seen,@result1,@result2,@result3,@result4,@result5,@result6,@result7,@result8,$results,$item,$ii); 
    my $pass = "pass"; 
    my $xinfo = shift; 
    my $ip = shift; 
    my $data = XML::LibXML->load_xml(string => $xinfo); 
    my $datax = XML::LibXML::XPathContext->new($data); 
    my $syspath = '/configuration/system/syslog/host/*'; 
    foreach ($datax->findnodes($syspath)) { 
    if($ip eq "67.176.10.2" && $_->getName() ne "contents") {my $v = join " = ",$_->getName(),$_->to_literal;push @r1,$v}; 
    if($ip eq "67.176.10.3" && $_->getName() ne "contents") {my $v = join " = ",$_->getName(),$_->to_literal;push @r2,$v}; 
    if($ip eq "67.176.10.4" && $_->getName() ne "contents") {my $v = join " = ",$_->getName(),$_->to_literal;push @r3,$v}; 
    if($ip eq "67.176.10.5" && $_->getName() ne "contents") {my $v = join " = ",$_->getName(),$_->to_literal;push @r4,$v}; 
    if($ip eq "67.176.10.6" && $_->getName() ne "contents") {my $v = join " = ",$_->getName(),$_->to_literal;push @r5,$v}; 
    if($ip eq "67.176.10.7" && $_->getName() ne "contents") {my $v = join " = ",$_->getName(),$_->to_literal;push @r6,$v}; 
    if($ip eq "67.176.10.8" && $_->getName() ne "contents") {my $v = join " = ",$_->getName(),$_->to_literal;push @r7,$v}; 
    if($ip eq "67.176.10.9" && $_->getName() ne "contents") {my $v = join " = ",$_->getName(),$_->to_literal;push @r8,$v}; 
    } 
    @seen{@r1} = (); if($ip eq "67.176.10.2") {foreach $item (@r1r) { push(@result1, $item) unless exists $seen{$item}; }} 
    if(@result1) { 
       foreach $ii (@result1) {$results .= "On R1 Syslog $ii ,is either missing or incorrect configuration was done\n";} 
       } 
    %seen=(); 
    $item=$ii = undef; 
    @seen{@r2} = (); if($ip eq "67.176.10.3") {foreach $item (@r2r) { push(@result2, $item) unless exists $seen{$item}; }} 
    if(@result2) { 
       foreach $ii (@result2) {$results .= "On R2 Syslog $ii ,is either missing or incorrect configuration was done\n";} 
       } 
    %seen=(); 
    $item=$ii = undef; 
    @seen{@r3} = (); if($ip eq "67.176.10.4") {foreach $item (@r3r) { push(@result3, $item) unless exists $seen{$item}; }} 
    if(@result3) { 
       foreach $ii (@result3) {$results .= "On R3 Syslog $ii ,is either missing or incorrect configuration was done\n";} 
       } 
    %seen=(); 
    $item=$ii = undef; 
    @seen{@r4} = (); if($ip eq "67.176.10.5") {foreach $item (@r4r) { push(@result4, $item) unless exists $seen{$item}; }} 
    if(@result4) { 
       foreach $ii (@result4) {$results .= "On R4 Syslog $ii ,is either missing or incorrect configuration was done\n";} 
       } 
    %seen=(); 
    $item=$ii = undef; 
    @seen{@r5} = (); if($ip eq "67.176.10.6") {foreach $item (@r5r) { push(@result5, $item) unless exists $seen{$item}; }} 
    if(@result5) { 
       foreach $ii (@result5) {$results .= "On R5 Syslog $ii ,is either missing or incorrect configuration was done\n";} 
       } 
    %seen=(); 
    $item=$ii = undef; 
    @seen{@r6} = (); if($ip eq "67.176.10.7") {foreach $item (@r6r) { push(@result6, $item) unless exists $seen{$item}; }} 
    if(@result6) { 
       foreach $ii (@result6) {$results .= "On R6 Syslog $ii ,is either missing or incorrect configuration was done\n";} 
       } 
    %seen=(); 
    $item=$ii = undef; 
    @seen{@r7} = (); if($ip eq "67.176.10.8") {foreach $item (@r7r) { push(@result7, $item) unless exists $seen{$item}; }} 
    if(@result7) { 
       foreach $ii (@result7) {$results .= "On R7 Syslog $ii ,is either missing or incorrect configuration was done\n";} 
       } 
    %seen=(); 
    $item=$ii = undef; 
    @seen{@r8} = (); if($ip eq "67.176.10.9") {foreach $item (@r8r) { push(@result8, $item) unless exists $seen{$item}; }} 
    if(@result8) { 
       foreach $ii (@result8) {$results .= "On R8 Syslog $ii ,is either missing or incorrect configuration was done\n";} 
       } 
    if($results) { return $results; } elsif(!$results) { return $pass } 
} 

我將會寫很多這些子程序。 基本上我通過一個路由器循環運行這個子程序,如果有什麼東西不符合我的期望我想返回什麼路由器是不正確的,缺少什麼。代碼的工作原理,但它感覺非常冗長,因爲我沒有編寫很長的代碼。 在此先感謝您的任何反饋意見。

回答

10

有很多需要改進的地方,我認爲你最好是試着逐步改進它,測試它以確保它每次都能正常工作。

跳到我身上的東西最多的是重複使用硬編碼的IP地址67.176.10.267.176.10.9。您將某些數據與其中的每個數據相關聯(如@r1等)。因此,你會被建議考慮散列。

「但是等等,我只能把標量放在散列值中!」是的,所以你需要使用referencestutorial here)。

這裏是你如何能簡化第一大常規的例子:

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; 
    } 

注意兩件事情:

  1. 我們構造一個哈希IP地址引用數組關聯(使用\參考運營商)。該引用將指向相同的數組,即使它與push操作一起更改。
  2. for循環的最後一行,我們檢索引用作爲標量(因爲標量是引用)。然後我們使用@運算符「引用」引用,該運算符返回底層數組。然後push按照您的預期工作。

尋找方法使用關聯數組和引用來獲得優勢。然後,您可以使用更少的代碼行進行查找,並且在循環中不需要這麼多的if語句。同時尋找在所有情況下都適用的常見條件,並將這些條件列入內部條件並將其置於循環的頂部。

+1

我知道引用是要走的路,只是以前從未使用過它們,並閱讀了我沒有上下文的文檔,因此我無法用我的項目對其進行可視化。感謝您的解決方案,我會盡力回覆。 – salparadise

+2

您可能會發現[perlreftut](http://perldoc.perl.org/perlreftut.html)更好的引用介紹。 –

+0

@格蘭特麥克萊恩:謝謝,我忘了那一個:-( –