0

我需要使用 Net::SSH 远程检索文件,但如果该文件远程不存在,我的脚本就会终止。我怎样才能停止这种情况并让脚本继续执行?

eval {  
    my $login_output = $ssh->login($user, $pass);

    foreach $device (keys %{$deviceHashRef})
    {   
                    my $transfer_output = $scpe->scp("$host:/home/portal/runtime/portal_daemon/data/journal/2012.07/refrigeration.case.$device.hourly", '/tmp/poop/input');
    }
};

if($@)
{       
    print Dumper $@;
}

谢谢,

汤姆

4

1 回答 1

1

如果将调用包装在 eval {} 中,它将捕获 die(),并且您可以处理错误。

 my $transfer_output = eval { $scpe->scp("$host:/home/portal/runtime/portal_daemon/data/journal/2012.07/refrigeration.case.$device.hourly", '/tmp/poop/input') };
 if ($@) {
   my $err = $@;

   # you can parse the error message out of $err and build an error message
   if ($err =~ /not found/) {
      logger->err('the input file was not found on the remote server');
   }
   else {
     logger->err("file copy encountered a problem: $err");
   }
 }
于 2012-08-22T15:59:12.497 回答