【发布时间】:2019-04-23 17:34:01
【问题描述】:
我想阅读不断更新的日志文件。如果我得到特定的模式,那么我应该能够发送我能够做的邮件。
use strict;
use warnings;
my $line;
my $to = 'abc@abc.com';
my $from = 'xyz@abc.com';
my $subject = 'Connection Pool Issue';
my $message = 'There is connection pool issue. Please check Logs for more details';
open my $fh, '<', 'error.txt';
my @file = <$fh>;
close $fh;
foreach my $line (@file) {
if ($line =~ /The connection is closed./)
{
open(MAIL, "|/usr/sbin/sendmail -t");
print MAIL "To: $to\n";
print MAIL "From: $from\n";
print MAIL "Subject: $subject\n\n";
# Email Body
print MAIL $message;
close(MAIL);
print "Email Sent Successfully\n";
last;
}
}
我不想从文件处理程序 0 读取文件,这意味着从起始位置。
我希望从当前文件处理程序位置读取文件。 它不应包含已读的行。
请提出建议。 谢谢
【问题讨论】:
-
提示:不要不必要地使用全局变量!使用
open(my $MAIL, ...)而不是open(MAIL, ...)
标签: perl file-handling