這是我的Perl代碼看起來像monitoring a Unix folder:我們有Perl中的autochomp嗎?
#!/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
之後每一項任務看起來並不好。有沒有辦法做一個「autochomp」?我是Perl新手,可能沒有以最好的方式編寫代碼。歡迎任何關於改進代碼的建議。
記住'chomp'可以採用一個列表:'chomp($ foo,$ bar,$ fubb)'也是有用的。 – FMc 2010-09-12 12:59:18