为了成为一个更好的脚本编写者,有没有办法在更少的行中生成以下代码?
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 }
}
Run Code Online (Sandbox Code Playgroud)
我将编写很多这些子程序.基本上我通过一个路由器循环运行这个子程序,如果某些东西与我期望的东西不匹配,我想返回路由器不正确和缺少什么.代码有效,但感觉非常冗长,因为我没有编写很长时间.提前感谢您的任何反馈.
Pla*_*ure 10
有很多方面需要改进,我认为你最好只是逐步改进它,测试它以确保它每次都有效.
即跳出我最多的是重复使用的硬编码IP的事情解决了67.176.10.2
通过67.176.10.9
.您将一些数据与每个数据相关联(如同@r1
等).因此,建议您考虑使用哈希值.
"但是等等,我只能把标量放在哈希值中!" 是的,所以你需要使用引用(这里的教程).
这是一个如何简化第一个大例程的例子:
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;
}
Run Code Online (Sandbox Code Playgroud)
注意两件事:
\
引用相关联(使用引用运算符).即使更改了push
操作,此引用也将指向同一个数组.for
循环的最后一行,我们将引用检索为标量(因为标量是引用).然后我们使用@
运算符"取消引用"引用,该运算符返回底层数组.然后push
就像你期望的那样工作.寻找使用关联数组和引用优势的方法.然后,您可以在更少的代码行中进行查找,并且if
在循环中不需要这么多语句.还要寻找在所有情况下都能保持的常见条件,并将这些条件纳入内部条件并将它们置于循环的顶部.