zoukankan      html  css  js  c++  java
  • perl 没有关键文件句柄引起的逻辑错误

    <pre name="code" class="sql">flow02:/home/tomcat> cat m1.pl 
    #!/usr/bin/perl 
    use POSIX;
    my $dir  = '/home/tomcat';
    my $file = '1.log';
    $mon_file = "$dir/$file";
    my $SDATE = strftime("%Y%m%d%H%M%S",localtime());
    $UPLOADPATH = "/data/swap/mbanklog/$IPMACHINE-$SDATE$file";
    ##########监控关键字,以空格隔开################
    @warn_arr = qw/Exception 555/;
    foreach $a (@warn_arr) {
        undef @err_info;
        open( C, "<", "$a.tmp" );
        while (<C>) {
            $count = $_;
        }
    print "=========================================================================================================================
    ";
    print "######监控异常关键字:      "$a"---数量=$count
    ";
        open( A, "<", "$mon_file" ) || die "$!
    ";
        $i   = 0;
        $num = 0;
        $b = 0;
        while (<A>) {
            $num++;
            if ( $_ =~ /$a/i ) { $i++; $b = $num }
        }
        print "######$b is $b######
    ";
            if ( $i != 0 && $i == $count && defined($count) ) {
            open( B, "<", "$mon_file" ) || die "$!
    ";
            while (<B>) {
            if  (($. >= "$b" -2) and  ($. <= "$b" + 2) ){push( @err_info, $_ ) };
            };
            print "######@err_info is   @err_info
    ";
    #close B;
        }
        open( C, ">", "$a.tmp" );
        print C ("$i
    ");
    }
    
    
    flow02:/home/tomcat> cat 1.log 
    4444
    2223
    5555
    java.lang.NullPointerException]
    4
    java.lang.NullPointerException
    5
    8
    
    
    flow02:/home/tomcat> perl m1.pl 
    =========================================================================================================================
    ######监控异常关键字:      "Exception"---数量=2
    
    $i is 2
    $b is 6
    ######$b is 6######
    ######@err_info is   java.lang.NullPointerException]
     4
     java.lang.NullPointerException
     5
     8
    
    =========================================================================================================================
    ######监控异常关键字:      "555"---数量=1
    
    $i is 1
    $b is 3
    ######$b is 3######
    ######@err_info is    ###这个地方没有打印第3行的上下2行,为什么呢?
    
    $i 表示匹配关键字的次数
    
    
    
    产生原因为没有关键文件句柄B;
    
    flow02:/home/tomcat> perl m1.pl 
    =========================================================================================================================
    ######监控异常关键字:      "Exception"---数量=2
    
    $i is 2
    $b is 6
    ######$b is 6######
    ######@err_info is   java.lang.NullPointerException]
     4
     java.lang.NullPointerException
     5
     8
    
    =========================================================================================================================
    ######监控异常关键字:      "555"---数量=1
    
    $i is 1
    $b is 3
    ######$b is 3######
    ######@err_info is   4444
     2223
     5555
     java.lang.NullPointerException]
     4
    
    为什么呢?没有关闭文件句柄,行号累加了。
    
    flow02:/home/tomcat> perl m1.pl 
    =========================================================================================================================
    ######监控异常关键字:      "Exception"---数量=2
    
    $i is 2
    $count is 2
    
    ######$b is 6######
    $. is 1
    $. is 2
    $. is 3
    $. is 4
    $. is 5
    $. is 6
    $. is 7
    $. is 8
    ######@err_info is   java.lang.NullPointerException]
     4
     java.lang.NullPointerException
     5
     8
    
    =========================================================================================================================
    ######监控异常关键字:      "555"---数量=1
    
    $i is 1
    $count is 1
    
    ######$b is 3######
    $. is 9
    $. is 10
    $. is 11
    $. is 12
    $. is 13
    $. is 14
    $. is 15
    $. is 16
    ######@err_info is 


    
                                        
    
  • 相关阅读:
    int 和 Integer 有什么区别?
    内部类可以引用它的外部类的成员吗?有没有什么限制?
    为什么Java不支持运算符重载?
    生命周期内create和mounted的区别?
    JSP有哪些动作?分别是什么?
    vue解除双向绑定?
    实现一个函数功能:sum(1,2,3,4..n)转化为 sum(1)(2)(3)(4)…(n)?
    新旧生命周期?
    vue异步组件?
    XML文档约束有哪几种?有什么区别?
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13351249.html
Copyright © 2011-2022 走看看