- 论坛徽章:
- 0
|
本帖最后由 东方补白 于 2010-07-30 12:31 编辑
正在看别人写的代码,有一段不太明白,请教一下大家。
下面一段代码的目的是处理从文本中读取出来的内容,如果在某行匹配了</rpc-reply>字符串,就删除其之后的内容。
但是如下的部分是不是多了一个'{'?- for (my $j = $i +1; $j < 0; $j++) {
- {
- delete $outArray[$j];
- }
- $i = 0;
- }
复制代码 还是说这种写法等同于如下代码,即$i=0是在for循环之外。- for (my $j = $i +1; $j < 0; $j++) {
- delete $outArray[$j];
- }
- $i = 0;
复制代码 完整的部分
- sub session_cmd {
- my ($session, $command) = @_;
- my $out = '';
- my $err = '';
- my $exit = '' ;
- my @outArray;
- my $i;
- my $counter = 0;
- my $nodata = 1;
- my $rc;
- my $foundCmd = 0;
- $session->clear_accum();
- print $session "$command\n";
- $rc = $session->expect( $timeout, '-re', $prompt, '-re', 'vty\)#' );
- if (!defined($rc)) {
- $err = "never got prompt from host!\n";
- undef $out ;
- $exit = "";
- print SINK "Timed out in $timeout seconds for $command\n";
- # print SINK "timed out on command: $command\n";
- print "timed out on command: $command\n";
- }
- else
- {
- $nodata = 0;
- # print SINK "Recieved response in $timeout seconds for $command\n";
- $out = $session->exp_before;
- @outArray = split( /\n/, $out);
- for ( $i = 0; $i < 5; $i++) {
- $foundCmd = 1 if ($outArray[$i] =~ /$command/);
- $foundCmd = 1 if ($outArray[$i] =~ /syntax\s+error/) ;
- }
- if ( $command =~ /xml/i)
- {
- delete $outArray[0];
- for ( $i = -5; $i < 0; $i++) {
- if ($outArray[$i] =~ /<\/rpc-reply>/)
- {
- for (my $j = $i +1; $j < 0; $j++) {
- {
- delete $outArray[$j];
- } # end for
- $i = 0;
- } # end for ( $i =
- } # end if ($outArray
- }
- }
- $out = join("\n", @outArray);
- }
- my $retryCounter = 0;
- while ( ($retryCounter < 5) && (0 == $foundCmd))
- {
- $rc = $session->expect( 1, '-re', $prompt, '-re', 'vty\)#' );
- if (!defined($rc)) {
- print "DEBUG: no residual data $retryCounter\n";
- }
- else
- {
- $nodata = 0;
- # print SINK "Recieved response in $timeout seconds for $command\n";
- my $out1 = $session->exp_before;
- my @out1Array = split( /\n/, $out1);
- for ( $i = 0; $i < 5; $i++) {
- $foundCmd = 1 if ($out1Array[$i] =~ /$command/);
- $foundCmd = 1 if ($out1Array[$i] =~ /syntax\s+error/) ;
- }
- if ( $command =~ /xml/i)
- {
- delete $out1Array[0];
- for ( $i = -5; $i < 0; $i++) {
- if ($out1Array[$i] =~ /<\/rpc-reply>/)
- {
- for (my $j = $i +1; $j < 0; $j++) {
- {
- delete $out1Array[$j];
- } # end for $j
- $i = 0;
- } #end for (my $j = $i +1; $j < 0; $j++) {
- } # end if ($out1Array[$i] =~ /<\/rpc-reply>/)
- } # end for ( $i = -5; $i < 0; $i++) {
- } #end if ( $command =~ /xml/i)
- $out1 = join("\n", @out1Array);
- $out = $out . "\n" . $out1;
- }
- $retryCounter = $retryCounter +1;
- } # end while $retryCounter
- # $out = $session->exp_before;
- #print SINK "Recieved response (unknown)in $counter seconds for $command\n";
- if ($foundCmd)
- {
- print SINK "DEBUG: retryCounter: $retryCounter found command $command\n";
- print "DEBUG: retryCounter: $retryCounter found command $command\n";
- }
- else
- {
- print SINK "DEBUG: $prompt did not find command $command\n";
- print "DEBUG: $prompt did not find command $command\n";
- }
- $session->clear_accum();
- return ($out, $err, $exit);
- }
复制代码 多谢各位了。 |
|