2015-07-06 132 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