读 《C程序员精通Perl》http://book.douban.com/subject/1232075/ 3.4节 笔记
#!/usr/bin/perl
use strict;
use warnings;
my $total = 0;
while (1) {
my $line = <STDIN>;
if (not defined($line))
{
print "come to file end\n";
last;
}
chomp($line);
if ($line eq "") #必须放到not defined后边进行判断,因为到了文件尾,
#line的值为undef,使用此值的话,会导致出现Use of uninitialized的告警
{
print "come to blank line\n";
last;
}
my @check_info = split /\t/, $line;
$total += $check_info[2];
printf "%-8s: %-20s %6.2f Total: %5.2f\n", $check_info[0], $check_info[1], $check_info[2], $total;
}
运行结果:
[root@localhost perl_practice]# cat check.file
01/01/01 a 100
01/01/02 a 100
01/01/03 a 100
01/01/04 a 100
01/01/05 a 100
01/01/06 a 100
[root@localhost perl_practice]# ./check.pl < check.file
01/01/01: a 100.00 Total: 100.00
01/01/02: a 100.00 Total: 200.00
01/01/03: a 100.00 Total: 300.00
01/01/04: a 100.00 Total: 400.00
01/01/05: a 100.00 Total: 500.00
01/01/06: a 100.00 Total: 600.00
come to file end
[root@localhost perl_practice]#