2015-07-06 84 views
-1

我对Perl很新。任何人都可以解释下面几行是什么意思?

if (-s $errorlog) { 
    open(LOG, "$errorlog") or die "Unable to open logfile:$!\n"; 
    while (<LOG>) { 
     my ($line) = $_; 
     chomp($line); 
     if ($line =~ m/\d\d-\d\d \d\d:\d\d:\d\d ERROR /) 

非常感谢您的回复。

回答

6

测试该文件是否为空。

if (-s $filename) { 
    # The file is not empty 
} 

更多细节:

## if the file is not empty 
if (-s $errorlog) { 
    ## Open the file and assign the reference to variable LOG 
    ## in case of failure, stop the program -- die 
    ## with error message "Unable to open logfile:<FILE NAME>\n" 
    open(LOG, "$errorlog") or die "Unable to open logfile:$!\n"; 

    ## While not end of file 
    while (<LOG>) { 
     ## read next line into local variable `line` 
     my ($line) = $_; 

     ## remove clutter from it (http://perldoc.perl.org/functions/chomp.html) 
     chomp($line); 

     ## if the line looks like "11-05 01:01:12 ERROR" 
     ## Regular expression used, probably to test for a date 
     ## after which string `ERROR` follows 
     if ($line =~ m/\d\d-\d\d \d\d:\d\d:\d\d ERROR /) 
+0

听起来不错!谢谢你 – shagi

+3

它是[这里描述](http://perldoc.perl.org/functions/-X.html) – jm666

+0

@shagi,接受答案该怎么办? :-)点击空的勾号,它会变成绿色 – Antonio