Chinaunix首页 | 论坛 | 博客
  • 博客访问: 836320
  • 博文数量: 253
  • 博客积分: 6891
  • 博客等级: 准将
  • 技术积分: 2502
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-03 11:01
文章分类

全部博文(253)

文章存档

2016年(4)

2013年(3)

2012年(32)

2011年(184)

2010年(30)

分类: Python/Ruby

2011-10-18 14:56:02

  1. foreach my $person (qw/ fred wilma betty barney dino pebbles /) {
  2.   eval {
  3.     open FILE, "<$person"
  4.       or die "Can't open file '$person': $!";
  5.     my($total, $count);
  6.     while (<FILE>) {
  7.       $total += $_;
  8.       $count++;
  9.     }
  10.     my $average = $total/$count;
  11.     print "Average for file $person was $average\n";
  12.     &do_something($person, $average);
  13.   };
  14.   if ($@) {
  15.     print "An error occurred ($@), continuing\n";
  16.   }
  17. }
When a normally fatal error happens during the execution of an eval block, the block
is done running, but the program doesn’t crash.



阅读(351) | 评论(0) | 转发(0) |
0

上一篇:signals

下一篇:grep: Picking Items from a List

给主人留下些什么吧!~~