Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl 网络连接不良的错误处理_Perl_Mechanize - Fatal编程技术网

Perl 网络连接不良的错误处理

Perl 网络连接不良的错误处理,perl,mechanize,Perl,Mechanize,我使用WWW:Mechanize连接和浏览一个网站,但有时我的网络连接超时,整个应用程序崩溃。我一直在研究各种选择,但不知道如何进行。我不仅希望防止应用程序崩溃,还需要建立一些逻辑来绕过这个问题,或者标记该项目以稍后再试 总的来说,我有一个相当大的功能,它遍历网站结构,填写几个字段,然后上传一个文件。如果网络出现故障,任何时间点都可能出现问题 目标:将出现网络故障。。。所以我需要内置错误处理来处理它。如果在执行任何方法时出现超时问题,我希望它以递增的延迟重试该操作三次。如果它仍然失败,我希望它运

我使用WWW:Mechanize连接和浏览一个网站,但有时我的网络连接超时,整个应用程序崩溃。我一直在研究各种选择,但不知道如何进行。我不仅希望防止应用程序崩溃,还需要建立一些逻辑来绕过这个问题,或者标记该项目以稍后再试

总的来说,我有一个相当大的功能,它遍历网站结构,填写几个字段,然后上传一个文件。如果网络出现故障,任何时间点都可能出现问题

目标:将出现网络故障。。。所以我需要内置错误处理来处理它。如果在执行任何方法时出现超时问题,我希望它以递增的延迟重试该操作三次。如果它仍然失败,我希望它运行我拥有的另一个perl模块的函数,该模块重置NIC并执行一些网络测试

这就是我的想法。。。 选项1:关闭自动检查,然后在每次调用mech方法时进行手动验证。这将导致大量的代码复制和粘贴,如果可能的话,我希望避免这个选项

选项2:在整个代码中运行eval块中的所有语句,然后在每个语句之后检查状态

选项3:有一个函数可以接受我想要执行的mech对象和动作。这将在一个位置执行所有错误处理,并返回更新的mech对象,以便应用程序的其余部分继续。例如,不执行 $mech->get('thesite')

我会做类似的事情

crash();

sub crash {
my $mech = WWW::Mechanize->new(agent => 'Mozilla/5.0 (compatible; MSIE 10.0;     Windows NT 6.2; WOW64; Trident/6.0)', autocheck => '0', timeout=>'200' );
$mech->ssl_opts( verify_hostname => 0);
    my $url = 'https://imaginarynonexist23123123entname.com';
    $mech = &mech_go($mech,"->get($url)");
}

sub mech_go {
my $mech = shift;
my $arg = shift;
my $run ='$mech'.$arg;
eval ( '$run' );
print Dumper ($mech);
}
我无法让它运行,->get方法从未运行过。你们能帮忙吗?有没有更好的方法来处理这个问题

应该是
eval($run)


使用


如果有人需要的话,我想发布最终的代码

sub mech_go {
   my $mech = shift;
   my $method = shift;

   my $status;
   my $count = 0;
   $mech->{'status'} = 0;
   until ( $mech->{'status'} == 200 || $count == 6 ) {
    print "Count: $count \t $mech->$method(@_)\n";
    eval { $mech->$method(@_) };
    $count++;
    sleep 2;

   if ($mech->{'status'} == 200) {
    return $mech;
   }

   if ($count == 4) {
          vpn->stop_vpn(); #custom module
          print "Looks like VPN crapped out, restarting, waiting for 120 seconds\n";
          sleep 120;
          vpn->start_vpn_main();  #custom module
     } 
  }

if ($count == 5) {
   return 0; #I ran into problems here, so had to adjust this to $mech and do validation on the calling function
   }

return 0;
}

酷,就是这样!非常感谢。但是我不能说我遵循了代码,for循环做了什么?另外,您能否告知(或发送链接)该对象是如何返回的?仅仅返回$mech有什么不对?对于loop:您想尝试三次,所以需要一个循环。返回$mech:如果这是您想要做的,那么就这样做,但我不明白为什么。1) 调用者已经有
$mech
,2)您将无法获得您调用的方法返回的内容。Hi Ikegami,谢谢,最后一个问题是关于如何调用函数的。mech_go($mech,get=>$url);我以前没见过这个。如何添加更多参数,例如,如果必须调用$mech,比如$mech->set_字段('name',$name));或者像$mech->follow_链接(text=>$department)?再次感谢
foo=>…
只是说
'foo',…
sub mech_go {
   my $mech = shift;
   my $method = shift;
   for (1..2) {
      my $rv;
      if (eval {
         $rv = $mech->$method(@_)
         1  # No exception
      }) {
         return $rv;
      }
   }

   return $mech->$method(@_);
}
sub mech_go {
   my $mech = shift;
   my $method = shift;

   my $status;
   my $count = 0;
   $mech->{'status'} = 0;
   until ( $mech->{'status'} == 200 || $count == 6 ) {
    print "Count: $count \t $mech->$method(@_)\n";
    eval { $mech->$method(@_) };
    $count++;
    sleep 2;

   if ($mech->{'status'} == 200) {
    return $mech;
   }

   if ($count == 4) {
          vpn->stop_vpn(); #custom module
          print "Looks like VPN crapped out, restarting, waiting for 120 seconds\n";
          sleep 120;
          vpn->start_vpn_main();  #custom module
     } 
  }

if ($count == 5) {
   return 0; #I ran into problems here, so had to adjust this to $mech and do validation on the calling function
   }

return 0;
}