【发布时间】:2010-09-12 10:17:21
【问题描述】:
这就是我的 Perl 代码在 monitoring a Unix folder 中的样子:
#!/usr/bin/perl
use strict;
use warnings;
use File::Spec::Functions;
my $date = `date`; chomp $date;
my $datef = `date +%Y%m%d%H%M.%S`; chomp $datef;
my $pwd = `pwd`; chomp $pwd;
my $cache = catfile($pwd, "cache");
my $monitor = catfile($pwd, "monme");
my $subject = '...';
my $msg = "...";
my $sendto = '...';
my $owner = '...';
sub touchandmail {
`touch $cache -t "$datef"`;
`echo "$msg" | mail -s "$subject" $owner -c $sendto`;
}
while(1) {
$date = `date`; chomp $date;
$datef = `date +%Y%m%d%H%M.%S`; chomp $datef;
if (! -e "$cache") {
touchandmail();
} elsif ("`find $monitor -newer $cache`" ne "") {
touchandmail();
}
sleep 300;
}
在每次分配后做一个
chomp看起来不太好。有什么办法可以做“自动选择”吗?我是 Perl 新手,可能没有以最佳方式编写此代码。欢迎提出任何改进代码的建议。
【问题讨论】:
-
记住
chomp可以获取列表也很有用:chomp($foo, $bar, $fubb)。